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


Then edit /etc/postfix/header_checks

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

Then edit /etc/postfix/generic

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 "" -s "Subject of your email message"

Leave a Reply

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