Mailjet and Postfix Code Sample

Postfix Installation

Here is the the Postfix configuration to send via relay, depending on the sender.

In this example, all outgoing emails are sent directly to Mail eXchangers (MX), except when from is * or which are going through Mailjet.

Caution, for Postfix, a sender is not the From: but the sender envelope passed to sendmail (in the 5th mail() argument: or in PHP config php.ini : sendmail_path = /path/to/sendmail -t -i

In /etc/postfix/ (remove relayhost):

smtp_sender_dependent_authentication = yes
sender_dependent_relayhost_maps = hash:/etc/postfix/sender_relay
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

In /etc/postfix/sender_relay, list addresses that must go through a relay:

In /etc/postfix/sasl_passwd, provide credentials for each address listed in /etc/postfix/sender_relay: apikey:secretkey apikey2:secretkey2

Don't forget the following commands:

cd /etc/postfix
chmod 600 sasl_passwd
chown root:root sasl_passwd
postmap sasl_passwd sender_relay
postfix reload