Configure sSMTP or Postfix to send email via SendGrid on CentOS 6.3 ec2

SendGrid Postfix Rat

I often launch CentOS images on ec2, specifically the RightImage_CentOS_6.3_x64_v5.8.8.5_EBS (ami-3b7bfc52). It’s a great clean CentOS image if you need to run tests or install Kaltura on a clean machine.

A common requirement is to have an email sending service, preferably postfix that will be stable and handle long queues of emails (to support for email notifications or alerts).

According to this thread on the AWS forums, mainly to block spam, ec2 servers are by default blocked from directly sending mail over port 25. So you’re mainly left with two options:

I preferred SendGrid for simplicity and since their free tier provides free 200 emails/day.
To signup, visit: https://sendgrid.com/user/signup, new accounts are usually enabled within 5 hours.

Configure sSMTP to relay via SendGrid

The default mail service on CentOS will be ssmtp. If you need to get an email working quickly, and don’t expect significant email queues, that might be a good option. ssmtp is abandoned since 2011, so you might want to jump to the postfix section if you care for a more stable solution.

vim /etc/ssmtp/ssmtp.conf

Then edit/add the following settings  (replace with your SendGrid user and pass):

mailhub=smtp.sendgrid.net:587
AuthUser=sendgridusername
AuthPass=sendgridpassword
UseSTARTTLS=YES

To test, execute:

echo TEST | mail -v -s "Test Mail" yourname@domain.com

 

Configure Postfix to relay via SendGrid

A better option is to install Postfix, and set it to relay emails via SendGrid.

Installing Postfix:

yum install postfix cyrus-sasl-plain cyrus-sasl-md5

Configuring main.cf:

vim /etc/postfix/main.cf

At the end of the file, add the following lines (replace with your SendGrid user and pass):

smtp_sasl_auth_enable = yes 
smtp_sasl_password_maps = static:yourSendGridUsername:yourSendGridPassword 
smtp_sasl_security_options = noanonymous 
smtp_tls_security_level = may
smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt
#get the path to your distro certificate at: http://mercurial.selenic.com/wiki/CACertificates
start_tls = yes
header_size_limit = 4096000
relayhost = [smtp.sendgrid.net]:587

Run the following commands (which will initialize the mail aliases db and restart Postfix):

newaliases
/etc/init.d/postfix restart

Lastly, configure MTA to choose Postfix by default (select the Postfix option and press enter):

alternatives --config mta

Testing Postfix

# This should show the pid Postfix is running on
service postfix status
# This should show a LISTENING line with port 25 on localhost/127.0.0.1
netstat -an | grep 25
# Should show levels 3, 4 and 5 set to on (making sure Postfix is set to auto load)
chkconfig --list postfix

Send a test mail:

echo TEST | mail -v -s "Test Mail" yourname@domain.com

Check the log file for any errors:

tail -f /var/log/maillog

If you see no errors (and you shouldn’t) in the log, your message should successfully arrive at yourname@domain.com.