Monday, February 25, 2008

Using Google to send mails from Rails

I was using 'sendmail' for sending mails from my web applications. The apps was sending mails for welcome messages, comment notifications, activation mails, send to a friend and all other features depending on sending emails to the application user. But recently, I was deploying a webapp on a server in which sendmail is not configured correctly. I didn't have time and wouldn't like to invest any time on system administration then, so I searched for another solution for my webapps to send mails through Google. I found ssmtp. It is a simple tool doing a simple task, sending mails. However it does not receive mail, expand aliases or manage a queue. It is just forwarding mails to the mailhost. SSMTP requires replacing 'sendmail'.

There is nothing required from Rails app to use ssmtp, actually it was transparent. Just make sure that the ActionMailer is configured to use sendmail.
ActionMailer::Base.delivery_method = :sendmail
SSMTP Installation
In Ubuntu I use apt-get to install the package. I am enabling all the sources on my ubuntu, so you may need to do so. In RPM based distribution like Centos in my case i used this link to download the required RPM.

on Centos
sudo wget ftp://mirror.switch.ch/mirror/epel/5/i386/ssmtp-2.61-11.4.el5.i386.rpm
sudo rpm -ivh ssmtp-2.61-11.4.el5.i386.rpm
on Ubuntu.
sudo apt-get install ssmtp

SSMTP Configuration

SSMTP configuration files lie in /etc/ssmtp so you need to edit the conf file and fill it with the your gmail account.
mailhub=smtp.gmail.com:587
AuthUser=my_gmail_account@gmail.com
AuthPass=my_gmail_password
UseSTARTTLS=YES
There is one feature you will get by using Google as your SMTP server, it's its sent mail folder which archive every mail sent by your application, this feature being a bless or damn is up to your needs.


Related links:

1 comment:

dave said...

thanks for the blog post. Your post along with others helped me figure out my email problems.

How can you work with a JSON value if you know nothing about it?

I have talked about the difficulty of typing certain JSON values coming from some APIs. The JSON is just very complicated. When I do that,...