Configuring Postfix to relay emails via Google Apps
Postfix is a popular and easy to configure mail server. It ships as the default mail server on many Unix like operating systems. Google for Work are enterprise services offered by Google to run services like corporate Email.
Web applications often need to send email notifications. Your application should be a good Internet citizen and route emails through your domain's SMTP server.
Rather than contact an external SMTP server every time your application needs to send mail, it's preferable queue them locally and let the local mail server worry about delivery.
Postfix performs this task beautifully, here's a recipe.
Configure the Gmail SMTP relay to:
- relay mail explicitly form declared IP addresses
- only allow sending email from addresses from your domain
Update Postfix's main configuration at
/etc/postfix/main.cf to relay via Gmail's SMTP servers:
relayhost = smtp-relay.gmail.com:25
Gmail's SMTP servers will expect the sender address to be a valid email address on your domain.
Postfix allows you to rewrite the sender address and SMTP headers before it forwards the email onto Gmail.
Change the following directives in
sender_canonical_classes = envelope_sender, header_sender sender_canonical_maps = regexp:/etc/postfix/sender_canonical_maps smtp_header_checks = regexp:/etc/postfix/header_check
Add the following line in
/etc/postfix/sender_canonical_maps. This instructs Postfix to replace the sender address for all outgoing mail to one you nominate:
Adds any rules to fix SMTP headers in
/etc/postfix/header_check, in this instance replacing who the email's from to be the same as the sender:
/From:.*/ REPLACE From: "Your Name" <firstname.lastname@example.org>
Lastly ensure you've restarted Postfix.