Sending email through Gmail SMTP with Merb

Monday 11/3/2008  –  Category: Uncategorized

I was having email delivery problems while using Postfix (emails were getting caught by spam filters) so I decided to try using Gmail SMTP to send stuff off.

Initially I tried to follow a tutorial like this to get Postfix to relay to Gmail but there was a lot of footwork involved setting up certificates, etc...and I couldn't get it to work at the end.

Finally, after searching around the Merb wiki, I found that it's really a lot more simple:

Gmail SMTP

Install tls mail to enable SSL support (required by Gmail).

$ gem install tlsmail

Then configure Merb Mailer to work with Gmail

in config/init.rb:

Merb::BootLoader.after_app_loads do
  dependency 'tlsmail'

  # Activate SSL Support
  Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)

  # Configure Merb Mailer
  Merb::Mailer.config = {
    :host   => 'smtp.gmail.com',
    :port   => '587',
    :user   => 'user@gmail.com',
    :pass   => 'pass',
    :auth   => :plain
  }
end

Then to send mail:

m = Merb::Mailer.new :to => 'foo@bar.com',
                     :from => 'bar@foo.com',
                     :subject => 'Welcome to whatever!',
                     :text => partial(:sometemplate)
m.deliver!

Leave a Reply