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.

The Y Combinator (Slight Return)

Tiger got to hunt, Bird got to fly; Lisper got to sit and wonder, (Y (Y Y))? Tiger got to sleep, Bird got to land; Lisper got to tell hims...