[RoR][Sysadmin] ActiveMailer sending via GMail

On the previous Kansai Rails Conference in Asiyagawa somebody asked me about using GMail (smtp+ssl or tls) with Rails ActiveMailer. Didn't find solution until now but 'dza-dza-dzaaaaaaan' (hahaha i'm not so smart, all credits going to entombedvirus ):
Send email with ActionMailer through TLS only SMTP server

So for Debian (Ubuntu):

  • Install msmtp (light SMTP client with support for server profiles)
$ sudo apt-get install msmtp
  • Create/edit ~/.msmtprc
account gmail
host smtp.gmail.com
auth on
user <your_account>@gmail.com
password <your_password>
tls on
tls_starttls on
from <your_account>@gmail.com
maildomain gmail.com
account default : gmail
  • Stop msmtp complaining about permissions
$ chmod 600 ~/.msmtprc
  • Add on the bottom of yourrailsapp/config/environment.rb (and comment another ActionMailer::Base.delivery_method settings if exists) :
ActionMailer::Base.delivery_method = :msmtp

module ActionMailer
  class Base
    def perform_delivery_msmtp(mail)
      IO.popen("/usr/bin/msmtp -t -C /<path_to>/.msmtprc -a gmail --", "w") do
 |sm|
        sm.puts(mail.encoded.gsub(/\r/, ''))
        sm.flush
      end
    end
  end
end

Do not forget to fix the exact path to your .msmtprc in the source above.