Thursday, May 8, 2008

Rails: ActionMailer with TLS (SSL)

Starting to work with Ruby on Rails, ActionMailer was quite a relief. Writing code for mass mailing was just a matter of configuration. You just need to configure your application mailer to use a given smtp server account to forward emails generated in the application. The common problem I faced was that ActionMailer does not support TLS, i.e. it cannot be configured to use an smtp account on a server that uses SSL for authentication.

The typical configuration in such a case is (using an smtp server that doesn't require SSL fro authentication) :

config.action_mailer.smtp_settings = {
:address => 'smtp.mailserver.com',
:port => 123,
:domain => "your domain name",
:authentication => :login,
:user_name => "account@mailserver.com",
:password => "account_password"
}

Or maybe you could use an smtp server that doesn't require authentication at all, disregarding that it will be considered as spam by
default by most respectful email providers:

config.action_mailer.smtp_settings = {
:address => 'smtp.mailserver.com',
:port => 123,
:domain => "your domain name",
:authentication => :plain
}

That's it for the problem. We need to use the great ActionMailer, with a respectful smtp server that requires SSL at the same time. The solution is the magical plugin action_mailer_tls. You just download and install the plugin, and add one line to the smtp settings:

config.action_mailer.smtp_settings = {
:address => 'smtp.mailserver.com',
:port => 123,
:domain => "your domain name",
:authentication => :login,
:user_name => "account@mailserver.com",
:password => "account_password",
:tls => true
}

That's it. Now you can generate mails and use that smtp server with TLS to forward your emails. Pretty handy, right?


4 comments:

hammady said...

Haitham, good research.
However, it seems that this plugin has now been obsoleted, I don't know why, but it always gives 404 not found when searching for its source!
I tried another patch and it worked for me!

Soleone said...

Thanks a lot. Worked liked a charm surprisingly!

SUzB said...

Still great help

Marvin Mendez said...

Hi, i will like to know if you could help me ??? I can send emails by gmail account, however i will like to do it by my smtp server