Bootstrap FreeKB - Postfix (Email) - Relay emails through SendGrid
Postfix (Email) - Relay emails through SendGrid

Updated:   |  Postfix (Email) articles

Some email recipients (such as hotmail and outlook) will refuse emails that are sent from a server that has a dynamic IP address. In this example, when attempting to send an email to jane.doe@hotmail.com, the Postfix email logs identify that Hotmail refused the email because the email was sent from a server with a dynamic IP address.

[root@server1 ~]# tail /var/log/maillog
Aug 16 10:05:49 example postfix/smtp[6660]: 894C4BA14E8: to=, relay=mx1.hotmail.com[65.68.12.214]:25, delay=119, delays=117/0.03/2.2/0.25, dsn=5.0.0, status=bounced (host mx1.hotmail.com[65.68.12.214] said: 550 DY-001 (SNT004-MC1F35) Unfortunately, messages from 76.45.12.78 were not sent. Please contact your Internet service provider. You can tell them that Hotmail does not relay dynamically-assigned IP ranges. You can also refer your provider to http://mail.live.com/mail/troubleshooting.aspx#errors. (in reply to MAIL FROM command))

 

 

Relaying emails through SendGrid resolves this issue.


 

Add the following line to /etc/postfix/main.cf to relay emails through SendGrid.

relayhost = [smtp.sendgrid.com]:587

 

Include the following in /etc/postfix/main.cf.

AVOID TROUBLE

There are similar directives, one that starts with smtp_ and the other that starts with smtpd_. For SendGrid you must use the smtp_ directives.

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

 

Create the sasl_passwd file:

~]# touch /etc/postfix/sasl_passwd

 

Add the following line to the sasl_passwd file. Username must be "apikey".

[smtp.example.com]:587 apikey:your_sendgrid_api_key

 

Use the postmap command to create or update the sasl_passwd.db file from the lines in the sasl_passwd file.

~]# postmap /etc/postfix/sasl_passwd

 

Ensure that only root can read and write these files.

~]# chown root:root /etc/postfix/sasl_passwd
~]# chown root:root /etc/postfix/sasl_passwd.db
~]# chmod 600 /etc/postfix/sasl_passwd
~]# chmod 600 /etc/postfix/sasl_passwd.db

 

The strings command can be used to confirm that the sasl_passwd.db files looks good.

~]# strings /etc/postfix/sasl_passwd.db
apikey:SA.14Hj6fh1RBSXoatMt4f5hg.zOhdOFKuitjqOtWatZXmGefxDMFDWIIElNhe7vb78z9
[smtp.example.com]:587

 

The ps command can be used to determine if your system is using init or systemd. If PID 1 is init, then you will use the service command. If PID 1 is systemd, then you will use the systemctl command.

If your system is using systemd, use the systemctl command to restart postfix.

systemctl restart postfix
systemctl status postfix

 

If your system is using init, use the service command to restart postfix.

service postfix restart
service postfix status

 

On the Postfix server, use the mailx command to send an email to someone.

echo test | mailx -v -s test -S from=john.doe@example.com -S smtp=smtp://smtp.sendgrid.net:587 -S smtp-auth-user=apikey -S smtp-auth-password=your_sendgrid_api_key jane.doe@example.com

 

Something like this should be displayed.

Rv9TIMnKMVk glh660@hotmail.com
Resolving host smtp.sendgrid.net . . . done.
Connecting to 167.89.115.53:587 . . . connected.
220 SG ESMTP service ready at geopod-ismtpd-2-6
>>> EHLO ip-172-31-19-227.ec2.internal
250-smtp.sendgrid.net
250-8BITMIME
250-PIPELINING
250-SIZE 31457280
250-STARTTLS
250-AUTH PLAIN LOGIN
250 AUTH=PLAIN LOGIN
>>> AUTH LOGIN
334 VXNlcm5hbWU6
>>> YXBpa2V5
334 UGFzc3dvcmQ6
>>> U0EuMTRIajZmaDFSQlNYb2F0TXQ0ZjVoZy56T2hkT0ZLdWl0anFPdFdhdFpYbUdlZnhETUZEV0lJRWxOaGU3dmI3OHo5
235 Authentication successful
>>> MAIL FROM:<john.doe@example.com>
250 Sender address accepted
>>> RCPT TO:<jane.doe@example.com>
250 Recipient address accepted
>>> DATA
354 Continue
>>> .
250 Ok: queued as BXWPqeRaTP-q-ZnZ60I2_g
>>> QUIT
221 See you later

 

/var/log/maillog should have relay=smtp.sendgrid.net.

May 12 19:59:10 mail.example.com postfix/smtp[27836]: E56D660173: to=<jane.doe@example.com>, relay=smtp.sendgrid.net[167.89.123.149]:587, delay=21, delays=20/0.13/0.24/0.24, dsn=2.0.0, status=sent (250 2.0.0 OK FC/59-29375-E5A56195)

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 19fb5e in the box below so that we can be sure you are a human.