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

daemon_directory = /usr/lib/postfix

myorigin = yourdomain.com
mydomain = yourdomain.com
myhostname = yourdomain.com

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

relayhost = [smtp.gmail.com]: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 = 127.0.0.1

alias_maps = hash:/etc/aliases

Then edit /etc/postfix/sasl_passwd_gmail

[smtp.gmail.com]:587 yourname@yourmail.com:YOURPASSWORD

Then edit /etc/postfix/header_checks

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

Then edit /etc/postfix/generic

root@yourdomain.com yourname@yourdomain.com
root@hostname.localnet yourname@yourdomain.com
sudoer@hostname.localnet yourname@yourdomain.com

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 "yourname@yourdomain.com" -s "Subject of your email message" someother@emailaddress.com

Leave a Reply

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