Postfix can be configured to relay outbound emails trough SendGrid service by adding few lines to postfix configuration.
First create API key in SendGrid service. Go to API keys, crate new API key and set access permission to “Restricted Access” and only allow “Mail send” as we wont be needing any other access rights.
Copy API key password for later use.
Edit postfix configuration file (i.e /etc/postfix/main.cf) and add following lines:
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
Also edit mynetworks line to include hosts or networks allowed to send emais trough postfix server.
mynetworks = 192.168.2.0/24, 10.11.11.0/24, 10.12.12.12/32
Next we will create file for storing login information to SendGrid.
Create a file located at /etc/postfix/ sasl_passwd and type in:
Secure file by limiting access rights: sudo chmod 600 /etc/postfix/sasl_passwd
Postmap file by running: sudo postmap /etc/postfix/sasl_passwd
Now restart postfix sudo service postfix restart
Thats it now you able to relay trough SendGrid service.
Remember to activate DKIM and whitelist necessary IP addresses in SendGrid.