How-to send email with a Google account form Ruby On Rails

I have seen many articles in the web that explain how to send an email with a Google account from a Ruby On Rails application.
However, I believe there are several faulty or incomplete examples around.
That’s why I decided to share a working solution that I used in various projects.

The main problem with Gmail (or with an address linked to Google Apps) is that prompts for an SSL connection.
As often happens with Ruby On Rails, the solution is quite simple.
You just need to install the gem tlsmail with the command:

1

gem install tlsmail

After that, just set the parameters for the desired mailbox, for example within the mail.rb initializer or where you think is better for you, taking care to add the line

1

require 'tlsmail'

and to set the :tls => true parameter.

Here’s the full snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#Configure mail ony for production mode
if RAILS_ENV == 'production'
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:domain => "your-domain.com",
:address => 'smtp.gmail.com',
:port => 587,
:tls => true,
:authentication => :plain,
:user_name => 'address@your-domain.com',
:password => 'your-password'
}
end

You can see how, using the gem tlsmail, configure your Google mail account become very simple.