E-mail Delivery Service Server Side Configuration: Sendmail + Sendgrid

Overview

Here are steps to configure your server for use with SoftLayer’s E-mail Delivery Service as a SmartHost with Sendmail. The example below was performed on a bare install of Centos 6.5 and Ubuntu 14.

Pre-Configuration

You will need to install the following packages for sendmail to properly use Sendgrid as a smart host.

RHEL/Centos

yum install cyrus-sasl-plain sendmail sendmail-cf

Ubuntu/Debian

apt-get install libsasl2-modules sendmail sendmail-cf heirloom-mailx

Configuration

The first step is to add your Sendgrid username and password to the file /etc/mail/access

AuthInfo:smtp.sendgrid.net "U:YOUR_SENDGRID_USER" "P:YOUR_SENDGRID_PASSWORD" "M:PLAIN" 

You will then need to run the following command to generate the /etc/mail/access.db database map.

makemap hash /etc/mail/access.db < /etc/mail/access

The next step is to edit the /etc/mail/sendmail.mc file to use Sendgrid as our Smarthost.

RHEL/Centos

Comment out the line that reads 'dnl define(`SMART_HOST', `smtp.your.provider')dnl' in the sendmail.mc file and add a new line with the following code:

define(`SMART_HOST', `smtp.sendgrid.net')dnl

FEATURE(`access_db')dnl

define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl

define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl

Ubuntu/Debian

At the bottom of the file place the following code above the line that reads 'MAILER_DEFINITIONS'

define(`SMART_HOST', `smtp.sendgrid.net')dnl

FEATURE(`access_db')dnl

define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl

define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl

Re-generate sendmail.cf

The sendmail.mc file is a collection of macros that can be expanded into the real (and more complex) sendmail.cf config file. To make your changes accessible to sendmail, we need to re-generate sendmail.cf using the m4 command:

m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

Restart the Sendmail

service sendmail restart

Test using the command line mail utility

echo "Sendgrid and Sendmail" | mail -s "mail subject here" you@yourdomain.com