How to use Mail / ActionMailer 3 with GMail SMTP

Mon Mar 15 16:29:34 -0700 2010

Getting Mail (and therefore ActionMailer 3) working with GMail SMTP is crazy simple, if you know how.

The secret is actually in the Mail RDoc, but to save you looking, here you are:

Mail allows you to send emails using SMTP. This is done by wrapping Net::SMTP in
an easy to use manner.

Sending via SMTP server on Localhost

Sending locally (to a postfix or sendmail server running on localhost) requires
no special setup. Just do

Mail.deliver do
       to 'mikel@test.lindsaar.net'
     from 'ada@test.lindsaar.net'
  subject 'testing sendmail'
     body 'testing sendmail'
end

# Or:

mail = Mail.new do
       to 'mikel@test.lindsaar.net'
     from 'ada@test.lindsaar.net'
  subject 'testing sendmail'
     body 'testing sendmail'
end

mail.deliver

And your email will be fired off to your localhost SMTP server.

Sending via GMail

To send out via GMail, you need to configure the Mail::SMTP class to have the correct values, so to try this out, open up IRB and type the following:

require 'mail'
options = { :address              => "smtp.gmail.com",
            :port                 => 587,
            :domain               => 'your.host.name',
            :user_name            => '<username>',
            :password             => '<password>',
            :authentication       => 'plain',
            :enable_starttls_auto => true  }
            
Mail.defaults do
  delivery_method :smtp, options
end

The last block calls Mail.defaults which allows us to set the global delivery method for all mail objects that get created from now on. Power user tip, you don’t have to use the global method, you can define the delivery_method directly on any individual Mail::Message object and have different delivery agents per email, this is useful if you are building an application that has multiple users with different servers handling their email.

Delivering the email

Once you have the settings right, sending the email is done the same way as you would for a local host delivery default:

Mail.deliver do
       to 'mikel@test.lindsaar.net'
     from 'ada@test.lindsaar.net'
  subject 'testing sendmail'
     body 'testing sendmail'
end

Or by calling deliver on a Mail message

mail = Mail.new do
       to 'mikel@test.lindsaar.net'
     from 'ada@test.lindsaar.net'
  subject 'testing sendmail'
     body 'testing sendmail'
end

mail.deliver

Combining this with ActionMailer 3

So as ActionMailer 3 uses Mail as it’s email transport agent, all you have to do is pass the same options Hash into ActionMailer:

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => 'your.host.name',
  :user_name            => '<username>',
  :password             => '<password>',
  :authentication       => 'plain',
  :enable_starttls_auto => true  }
}

Hope that helps!

blogLater

Mikel

  1. Kamal Says:

    Thank you so much, i got this to work in few seconds. Using the “send via gmail” example
    Is there a way, i can use ruby “mail” to deliver to multiple recipients, which are in a flat file, or a yaml file.

    Thanks,

  2. FlyboyArt Says:

    Really nice writings up here but I wish you would use a print.css files that allows for a nicer layout when printing. With the coming of the iPad and being able to take PDF’s along with you anywhere (without net access) to read, I would love to be able to print-to-pdf your articles and read them at my leisure on a train or something.

    Here’s a good place to get started with a nice print.css:

    http://www.alistapart.com/articles/goingtoprint/

    Thanks!, Art

  3. Emmanuel Oga Says:

    Nice and simple. Thanks for sharing!

  4. Daniel Says:

    Getting ‘Net::SMTPAuthenticationError: 530 5.7.0 Must issue a STARTTLS command first’

    Following your instructions by the letter, but when I issue mail.deliver I get the error.

    rails c
    Loading development environment (Rails 3.0.0.beta)
    ruby-1.9.1-p378 >

    options = { :address => “smtp.gmail.com”,
    :port => 587,
    :domain => ‘your.host.name’,
    :user_name => ‘’,
    :password => ‘’,
    :authentication => ‘plain’,
    :enable_starttls_auto => true }

    options[:domain] = ‘realthing’
    options[:user_name] = ‘real name’
    options[:password] = ‘blah’

    Mail.defaults do
    delivery_method :smtp, options
    end

    mail = Mail.new do
    to ‘me@me.com’
    from ‘bob@sled.com’
    subject ‘yeah, good test’
    body ‘testing :)’
    end

    mail.deliver

    Net::SMTPAuthenticationError: 530 5.7.0 Must issue a STARTTLS command first. 15sm1219728ewy.12

    from /Users/daniel/km/sio/trunk/vendor/bundle/gems/tlsmail-0.0.1/lib/net/smtp.rb:679:in `auth_plain’ from /Users/daniel/km/sio/trunk/vendor/bundle/gems/tlsmail-0.0.1/lib/net/smtp.rb:673:in `authenticate’ from /Users/daniel/km/sio/trunk/vendor/bundle/gems/tlsmail-0.0.1/lib/net/smtp.rb:488:in `do_start’ from /Users/daniel/km/sio/trunk/vendor/bundle/gems/tlsmail-0.0.1/lib/net/smtp.rb:440:in `start’ from /Users/daniel/km/sio/trunk/vendor/bundle/gems/mail-2.1.3/lib/mail/network/delivery_methods/smtp.rb:101:in `deliver!’ from /Users/daniel/km/sio/trunk/vendor/bundle/gems/mail-2.1.3/lib/mail/message.rb:1777:in `do_delivery’ from /Users/daniel/km/sio/trunk/vendor/bundle/gems/mail-2.1.3/lib/mail/message.rb:219:in `deliver’ from (irb):23 from /Users/daniel/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands/console.rb:47:in `start’ from /Users/daniel/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands/console.rb:8:in `start’ from /Users/daniel/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands.rb:34:in `<top (required)>’ from /Users/daniel/km/sio/trunk/script/rails:10:in `require’ from /Users/daniel/km/sio/trunk/script/rails:10:in `’
  5. Sohan Says:

    Your post has been linked at the Drink Rails Blog

  6. Daniel Says:

    Alright, sorry about the spam. I wish there’d been a preview button and I would have not posted so much :/

  7. daniel Says:

    Also following the instructions in http://guides.rails.info/action_mailer_basics.html

    gives me the same errors. help appreciated (only after setting config.action_mailer.raise_delivery_errors = true).

  8. rman Says:

    Excellent code, worked right off the bat! Does anyone have any additions that would allow for looping through a list of emails?

  9. Tageendargy Says:

    Wo…Nice Blog, Nice tips, Great article!, I have recently started a blog, the information you provide on this site has helped me tremendously. Thank you for all of your time & work. Well Come Back To My Blog [url=http://www.ipadzu.net]Ipad[/url]
    http://www.ipadzu.net

  10. im Says:

    The future of ActionMailer in Rails 3 looks bright. Can you comment, though, on how to process incoming mail? As difficult as sending mail is on Rails, receiving it is even harder.

  11. B Says:

    @im – try this (from RSoC): http://github.com/titanous/mailman

Leave a Reply