Ruby Rails Gmail SMTP

Vote on HN

When you want to send a quick email in a ruby script, it's easy to send it through Gmail. You don't have to worry about email deliverability, and you get a record of it in your 'Sent Box'. There were a few outdated blog posted on how to do this, but I had to make a few tweaks before it worked for me.

Update: For Ruby > 1.8.7 and Rails >= 2.2.2, you can simply specify 'enable_starttls_auto => true'. I put the following in 'config/initializers/actionmailer_gmail.rb'

ActionMailer::Base.smtp_settings = { :address => "", :port => 587, :authentication => :plain, :enable_starttls_auto => true, :user_name => "", :password => "superB1rd!" }


sudo gem install tlsmail
sudo gem install mail   # optional

Ruby Inside recommended 'smtp_tls', but that gem is not compatible with Ruby versions greater than 1.8.6. 'tlsmail' works for 1.8.6 and above.

Once you have 'tlsmail' installed you can use ActionMailer, or mikel's mail gem to build and send the message. If you don't want the extra dependency and speak mail headers, you can write the raw mail message yourself.


# in environment.rb
require 'tlsmail'
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  :address => '',
  :port => 587,
  :tls => true,
  :domain => '',  # if you use google apps
  :authentication => :plain,
  :user_name => '',  # make sure you include the full email address
  :password => '_secret_password_'



  require 'rubygems'
  require 'tlsmail'
  require 'mail'     #
rescue LoadError => e
  puts "Missing dependency #{e.message}"
  exit 1

mail = do
      from ''
        to ''
   subject "email subject line"
      body 'blog backup'  # add an attachment via add_file
Net::SMTP.start('', 587, '', '', '_secret_', :login) do |smtp|
  smtp.send_message(mail.to_s, '', '')

Bonus: Email Testing with Mailtrap

Mailtrap was mentioned in a related article on RubyInside. The way I had been testing emails on OS X was starting a local smtp server (sudo postfix start) and send test emails to Gmail. Mailtrap lets you do quick tests locally by logging the sent mail to a text file.

comments powered by Disqus