Search This Blog

Sunday, October 9, 2011

Rails 3 : Mailer


Hey fellas, Here's a quick view to convert your rails 2.x mailer to rails 3.x
 
There wont be any change in the path of views files. It wil reside in app/views/user_mailer.

The user_mailer.rb [wch was in models] is now moved to app/mailers
 
The mailer setting are moved to configuration/initializers/mail_setup.rb

Here is the changes that has to be done for sending mails:

In 2.X: UserMailer.deliver_notify_phishing_rsa(phishing, email, address)
In 3.X: UserMailer.notify_phishing_rsa(phishing, email, address).deliver

In user_mailer.rb, there is change in syntax for objects/parameters that needs to be sent to the email body.

In 2.X: @body['phishing'] = phishing
In 3.X: @phishing = phishing

In Views, we can use the following to provide a link in the email body:

<%= link_to "Edit Phishing", edit_phishing_url(@phishing, :host => "www.example.com") %>

OR

<%= edit_phishing_url(@phishing, :host => "localhost:3000") %>


Also, We can attach files with the following syntax inside user_mailer.rb:

attachments["phishingreport.pdf"] = File.read("#{Rails.root}/public/uploads/phishingreport.pdf")

For inline attachments:

attachments.inline['myimage.jpg'] = File.read('/path/to/myimage.jpg')

<%= image_tag attachments['myimage.jpg'].url %> #in views