Configuring Postfix to Relay Emails via Google Apps

, a 2-minute piece by Dev Mukherjee Dev Mukherjee

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:

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 /etc/postfix/main.cf:

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:

/.+/    sender@yourdomain.com

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" <sender@yourdomain.com>

Lastly ensure you've restarted Postfix.

Next Up: a 3-minute piece by Dev Mukherjee Dev Mukherjee

New Year's Resolutions for 2016

Read more