Configure postfix to relay mails using SendGrid

To use postfix to send email using sendgrid, you first you need to install postfix on your server. Edit main.cf

vi  /etc/postfix/main.cf

Find

relayhost =

Delete the line. Go to end of the file and add following

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_security_level = encrypt
header_size_limit = 4096000
relayhost = [smtp.sendgrid.net]:587

Create API Key in SendGrid

Next you need to create an API key in sendgrid. To do this, go to Settings > API Key.

https://app.sendgrid.com/settings/api_keys

SendGrid API key

Click “Create API Key” button.

Sendgrid create api key

For this test, i just give the new API key Full Access. On next page you will get API key. Copy it and keep it safe as you won’t be seeing it again.

sendgrid api key created

Now you have the SendGrid.com API key, that you can use with postfix.

Add API Key to postfix

Edit file

vi /etc/postfix/sasl_passwd

Add

[smtp.sendgrid.net]:587 apikey:YOUR_API_KEY_HERE

Here is what i have

root@honVm:~# cat /etc/postfix/sasl_passwd
[smtp.sendgrid.net]:587 apikey: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
root@honVm:~#

Now run

postmap /etc/postfix/sasl_passwd

To test email, run

postsuper -d ALL
systemctl restart postfix
> /var/log/mail.log
echo "Hello" | mail -s "test" [email protected]
tail -f /var/log/mail.log

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *