Question
How to configure Postfix to send emails using SendGrid?
Answer
Note: The steps below describe how to configure integration with 3rd-party service and should be performed on the server’s administrator risk. Or consider this task to be handled by Plesk Professional Services
Warning: any customization made in Postfix configuration can be overwritten by Plesk update (for example, in case of bug fix, when configuration files are updated), Plesk upgrade or by mchk
utility, which resets default mail server configuration.
Google Cloud Instance
- Connect to the server using SSH.
- Define which MTA is installed on the server and switch to Postfix if necessary.
- Create a backup of original
/etc/postfix/main.cf
configuration file and open it with a text editor:# cp -a /etc/postfix/main.cf{,.original}
# vi /etc/postfix/main.cf - Adjust it in accordance to the Google documentation instructions (Run Postfix on your instance section, starting from Step 3):
- check if there are the following parameters exist and comment them out by adding hashtag sign in the beginning of the line:
default_transport = error
relay_transport = errorCommented out directives
#default_transport = error
#relay_transport = error - add the SendGrid SMTP service as relayhost:
relayhost = [smtp.sendgrid.net]:2525
Note: That port 2525 is used, because the outbound connections on ports 25, 587 are not allowed on Compute Engine.
- add the following lines at the end of the file:
smtp_tls_security_level = encrypt
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
header_size_limit = 4096000
smtp_sasl_security_options = noanonymousAfter the parameters set, make sure that there are no duplicate directives that can break the configuration:
# cat /etc/postfix/main.cf | grep “relayhost|smtp_tls_security_level|smtp_sasl_auth_enable|smtp_sasl_password_maps|header_size_limit|smtp_sasl_security_options”
Comment out/remove duplicate directives if there are some.
-
generate the SASL password map:
# echo [smtp.sendgrid.net]:2525 yourSendGridUsername:yourSendGridPassword >> /etc/postfix/sasl_passwd
where:
yourSendGridUsername is your SendGrid username.
yourSendGridPassword is your SendGrid password. -
use the postmap utility to generate a .db file:
# postmap /etc/postfix/sasl_passwd
-
verify that .db file has been created:
# ls -l /etc/postfix/sasl_passwd*
-rw——- 1 root root … /etc/postfix/sasl_passwd
-rw——- 1 root root … /etc/postfix/sasl_passwd.db -
set the proper permissions on .db file:
# chmod 600 /etc/postfix/sasl_passwd.db
-
remove the file containing SendGrid credentials, as it is no longer needed:
# rm /etc/postfix/sasl_passwd
-
reload Postfix service to apply changes:
# service postfix restart
- check if there are the following parameters exist and comment them out by adding hashtag sign in the beginning of the line:
Configure SendGrid for regular server
- Connect to the server using SSH.
- Define which MTA is installed on the server and switch to Postfix if necessary.
- Create a backup of original
/etc/postfix/main.cf
configuration file and open it with a text editor:# cp -a /etc/postfix/main.cf{,.original}
# vi /etc/postfix/main.cf - Adjust it in accordance to the SendGrid documentation instructions (Run Postfix on your instance section, starting from Step 3):
- add the SendGrid SMTP service as relayhost:
relayhost = [smtp.sendgrid.net]:587
- add the following…
- add the SendGrid SMTP service as relayhost: