Ruby on Rails SMTP Mailer – sending mail

This is a topic that has been covered a few times but took me a while to pickup from a couple of different sources. A note about my tech posts, these are generally designed as references rather than tutorials so they will move fast and assume some basic knowledge.

Environment Settings

The first point of call is setting up your environment variables to tell Ruby your SMTP credentials. Add the following to the bottom of your conf/environment.rb file:


# SMTP configuration
config.action_mailer.delivery_method = :smtp

ActionMailer::Base.server_settings = {
:address => YOUR_MAIL_SERVER,
:port => 25,
:user_name => YOUR_USERNAME,
:password => YOUR_PASSWORD,
:authentication => :login
}

Replacing YOUR_MAIL_SERVER with the address of your mail server, typically mail.DOMAIN.com or smtp.DOMAIN.com. Replace YOUR_USERNAME and YOUR_PASSWORD with the username and password of a valid mail user on the server, the mails will appear to come from that person to your recipients.

Generate the mailer

Next we’ll generate the mailer class, using the very handy generate command. From the root directory of your project run:

script/generate mailer postoffice

This will generate a new class, postoffice.rb under the app/models directory containing the class called Postoffice. This class is responsible for building up email content and setting recipients. For each email you wish to send, you will typically create a method in this class. You can then automatically call Postoffice.deliver_METHOD_NAME and through some Ruby magic it will be sent. So, let’s say we want an email after someone has registered for your new game-changing site. We’ll create a register method which takes an email address and the name of the user in postoffice.rb:


class Postoffice < ActionMailer::Base

def register(email, username)
@recipients   = email
@from         = "mysender@mydomain.com"
headers         "Reply-to" => "mysender@mydomain.com"
@subject      = "Welcome to MyDomain"
@sent_on      = Time.now
@content_type = "text/html"

body[:username]  = username
body[:email] = email
end

end

Adding some templates

This method should self explanatory. The only point to make is the last two lines, they make the values username and email available in the email template files we are about to create. We create two files under app/views/postoffice, register.text.plain.erb and register.text.html.erb. Obviously one is for plain text email clients, the other for HTML. It is best practice to have both formats, there are still plenty of people who only read plain text (mainly sysadmins in my experience, I always though that was ironic). So as an example, register.text.html.erb could like like:


<p>
Welcome <%= @username %>,
You've just joined MyDomain.com, you must be significantly smarter than the average populous.
</p>

So, the final stage, how do we call the method to send the email. Well, in the main register method of your app (not the one we created above), at the point when you want to send the email place:

Postoffice.deliver_register(email, username)

where email and username are variables you have set up of course, referring to the email and username of the recipient of your email. One final note, you will need postfix running to test this locally. To start postfix on your machine (assuming you’re developing on a Mac), run:

sudo postfix start

at a terminal. Hope that helps.

3 responses to “Ruby on Rails SMTP Mailer – sending mail”

  1. Maryjane says:

    I am working on getting my smtp email delivery and this was very helpful. I was able to test my setup using the script/console (InstantRails):
    ruby script/console
    Postoffice.deliver_register(“email”,”username”)
    I could see the message being delivered with the correct settings in the console screen used for the ruby script/server command.
    The error I am seeing shows in the script/console screen:
    “554 5.7.3 Unable to initialize security subsystem\r]n”
    Net::SMTPFatalError: 554 5.7.3 Unable to initialize security subsystem

    I do not know enough about the Net::SMTP to decipher the error. Does this have to do with the server setup or is it a setting I am missing?

  2. Maryjane says:

    Solved the SMTPFatal Error: 554 5.7.3…
    I had installed the openrain_action_mailer_tls previously while trying to get the email working before I hit this fantastic site. I uninstalled it because some SMTP servers do not do well with tls. I had to change the :authentication to :login … I had experimented with :plain but it did not work. Voila! we are emailing!
    Thanks so much!
    Maryjane

  3. wicheda says:

    I’m glad the site helped. Always good to get real comments rather than the deluge of spam 😉

Leave a Reply

Your email address will not be published. Required fields are marked *