Configuring an infrastructure container to send email using Postfix and Sendgrid

July 08, 2015 - by Jason Schmidt

There are a few quick and easy ways to setup outbound email from your infrastructure container running SmartOS. You can use Google's free SMTP server (provided you are only planning on sending to gmail accounts), you can use Google with your actual account credentials (which means you need to put your credentials on that container), or you can use a third party service as your relay. The last is the route I took to setup outbound email on a Ghost blog that I configured for my son.

Sendgrid allows you to send up to 400 emails per day from their free tier account, which was more than enough for what I was planning. The only outbound email the blog sends is password resets, updates from fail2ban, and any other administrative-type traffic.

Setup is fairly simple. Step one is to sign up and have your account verified and provisioned, which should take about five minutes. Once you're done with that you need to configure SmartOS to relay through Sendgrid's SMTP server.

To do this, you're first going to need to install a Cyrus SASL plugin for authentication. As of this writing, the plugin you need is the "plain" plugin:

# pkgin up
# pkgin install cy2-plain

Now you need to edit the postfix configuration file, which is:

/opt/local/etc/postfix/main.cf

Now you simply add the following at the bottom of your configuration file (taking care to replace the placeholders with your username and password):

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:yourSendGridUsername:yourSendGridPassword
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
header_size_limit = 4096000
relayhost = [smtp.sendgrid.net]:587

And then restart the postfix service:

# svcadm restart postfix

Now you should be able to test:

# mailx -s "Testing" my.email@test.net
test
test
.
EOT

You should receive your email, and you can also check the log file to confirm that the email has been passed off to the relay:

# tail /var/log/maillog
2015-07-06T13:39:36+00:00 localhost postfix/pickup[31604]: [ID 197553 mail.info] 59E0DB1BF: uid=0 from=<root>
2015-07-06T13:39:36+00:00 localhost postfix/cleanup[38122]: [ID 197553 mail.info] 59E0DB1BF: message-id=<20150706133936.59E0DB1BF@72e8a09d-e215-673f-8bcc-bb1dbe707295.localdomain>
2015-07-06T13:39:36+00:00 localhost postfix/qmgr[31605]: [ID 197553 mail.info] 59E0DB1BF: from=<root@72e8a09d-e215-673f-8bcc-bb1dbe707295.localdomain>, size=418, nrcpt=1 (queue active)
2015-07-06T13:39:37+00:00 localhost postfix/smtp[38124]: [ID 197553 mail.info] 59E0DB1BF: to=<my.email@test.net>, relay=smtp.sendgrid.net[198.37.144.212]:587, delay=0.87, delays=0.03/0.02/0.68/0.14, dsn=2.0.0, status=sent (250 Delivery in progress)
2015-07-06T13:39:37+00:00 localhost postfix/qmgr[31605]: [ID 197553 mail.info] 59E0DB1BF: removed