ActionMailer Examples: Responses, Welcome Greetings, Sendmail, Templating and Performance

Using Rails ActionMailer The Right Way

By providing the full name in the @from field, you avoid having emails say they were sent from congratulations:

def new_registration(user)
@subject = ‘Welcome!’
@recipients = user.email
@from = ‘Ruby on Rails Blog <congratutions@domain.com>’
@body[”user”] = user
end

Now the email will show that it’s from Ruby an Rails Blog. This passed some simple tests with yahoo and gmail with no messages being sent to the spam folder. Everything comes through perfectly.

Using Sendmail with Rails Actionmailer

In environment.rb to use sendmail, you just need these 2 lines:

ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.raise_delivery_errors = true

Speed / Performance

ar_mailer allows you to store messages into the database for later delivery by a separate process, ar_sendmail. - AR_MAILER

One Template Fits all Example

Here are all the pieces of sending a generic (ONE template fits all) email from Rails. In this example, I am using an ISP that I pay for and a free SMTP server running on Windows for testing. Use the generator in RadRails to create the MyMailer class: script/generate mailer MyMailer This produces a “my_mailer.rb” model file in the project. =============== add to bottom of config/envionment.rb ===============

config.action_mailer.delivery_method = :smtp
ActionMailer::Base.server_settings = {
 :address => "smtp.hostdepot.com",
 :port => 25,
 :domain => "mydomain.com",
 :authentication => :login,
 :user_name => "mike@mydomain.com",
 :password => "mypassword"
}

======== inside the my_mailer.rb model file =================

def one_email(to_addr, from_addr, msg_subject, msg_body)

    @from = from_addr
    @recipients = to_addr
    @subject = msg_subject
    @body["email_body"] = msg_body

  end

========= inside the one_email.rhtml view file ================

<%= @email_body %>

========= the call inside the organization_controller ======== …during a post of the forgot-my-password form…

send_login_reminder(org)

============== the called method =========================

def send_login_reminder(org)

     msg_body = "Your login information:\n\n"
     msg_body = msg_body + "User Id: #{org.user_id}\n"
     msg_body = msg_body + "Password: #{org.user_password}\n\n"
     msg_body = msg_body + "Product Support"

     MyMailer.deliver_one_email(org.user_email_address,
"michael@mydomain.com",
               "MyProduct login reminder", msg_body)

# For testing the email without sending it,
# uncomment these lines and change "deliver" above to "create"
#      email = MyMailer.create_one_email(org.user_email_address,
"michael@mydomain.com",
#                "MyProduct Login reminder", msg_body)

#      render(:text => "<pre>" + email.encoded + "</pre>")

  end

Responding to signups

I suggest creating a Contact model that inherits from AR (or, if you don’t want to store contacts in the DB, use the active-form plugin) and a ContactNotifier class which inherits from ActionMailer. Then in your controller do

@contact = Contact.new(params[:contact])
ContactNotifier.deliver_signup_thanks(@contact)

OR Create models/account_notifier.rb

class AccountNotifier < ActionMailer::Base
 def signup_thanks(contact)
   ...
   do whatever you want with the information
   ...
 end
end

Call it like this: AccountNotifier?.deliver_signup_thanks(@contact) Creat the following in views: views/account_notifier/signup_thanks.rhtml

Domain Specific Templating

* Creating ActionMailer templates and putting them in the view directory (CRMonRails does this) * Create email templates and store them in the DB. * A list of models/fields should be drop-down enabled so they can easily say something like this:

Dear <% order.customer_first_name %>,

Thank you for ...

Regards,

<% user.full_name %>