Configure Postfix as gmail SMTP relay

First of all you will need certain packages:

apt-get install postfix ssl-cert mailutils libsasl2-modules

Then you need to create /etc/postfix/

daemon_directory = /usr/lib/postfix

myorigin =
mydomain =
myhostname =

smtp_generic_maps = hash:/etc/postfix/generic
header_checks = regexp:/etc/postfix/header_checks

relayhost = []:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd_gmail
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_enforce_tls = yes
disable_dns_lookups = yes

message_size_limit = 35651584

smtp_destination_rate_delay = 60
relay_destination_rate_delay = 60

inet_protocols = ipv4
mynetworks =

alias_maps = hash:/etc/aliases

Then edit /etc/postfix/sasl_passwd_gmail

[]:587 [email protected]:YOURPASSWORD

Then edit /etc/postfix/header_checks

/^To:.*@hostname.localnet/ REDIRECT [email protected]
/^From:(.*[>]*)/ PREPEND Reply-To:$1

Then edit /etc/postfix/generic

[email protected] [email protected]
[email protected] [email protected]
[email protected] [email protected]

Then execute following commands:

postmap /etc/postfix/generic
postmap /etc/postfix/sasl_passwd_gmail
cat /etc/ssl/certs/Equifax_Secure_CA.pem >/etc/postfix/cacert.pem
ln -s /etc/postfix/aliases.db /etc/aliases.db #remove /etc/aliases.db if exists
postalias /etc/postfix/generic
postalias /etc/aliases
/etc/init.d/postfix restart

And finally send your test email:

echo "body of your test email message" | mail -r "[email protected]" -s "Subject of your email message" [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.