[HOW TO] configure Postfix to use Gmail SMTP on Fedora/ Linux/ Ubuntu

I have just created a script to monitoring website. It send the email to me when any website is down. Script store in my local, thus I think to build a MTA in my laptop.

Here I show the outlet for task list:
1. Configure MTA local use Google Mail
2. Create cron job to run script every boot time.

1. Configure MTA local use Google Mail
I have chosen Postfix as MTA. A few time ago, I configured Postfix, so now I do it fast. However, I wonder what will come if I use Google as relay host? I like this is good ideas. A moment later, I found a method to do.

Then open postfix configuration file:
tien localhost ~ $sudo vi /etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

As you see, I did not store user name & password in above lines. They will store in different file. Open/Create
tien localhost ~ $sudo vi /etc/postfix/sasl_passwd

And add following line:
[smtp.gmail.com]:587    USERNAME@gmail.com:PASSWORD

If you want to use our Google App's domain, please replace @gmail.com with your @domain.com

Set permission sasl_passwd file:
tien localhost ~ $sudo chmod 400 /etc/postfix/sasl_passwd

Update postfix configuration to use sasl_passwd file:
tien localhost ~ $sudo postmap /etc/postfix/sasl_passwd

Next, validate certificates to avoid running into error. Just run following command:
tien localhost ~ $sudo cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

Finally, please reload postfix
tien localhost ~ $sudo /etc/init.d/postfix reload

All is done. Now you can test

tien localhost ~ $echo "Test mail from postfix" | mail -s "Test Postfix" you@example.com

2. Create cron job to run script every boot time.
Use special strung called @reboot to run job at startup (boot time)
$crontab -e -u tien

@reboot sh /home/tien/Script/webmonitor/mosite.sh

3. Troubleshooting
Google Mail SMTP server has a limit of 500 email per day. So use wisely!

Error: “SASL authentication failed; server smtp.gmail.com”
You need to unlock the captcha by visiting this page https://www.google.com/accounts/DisplayUnlockCaptcha
You can run test again after unlocking captcha

Reference: https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/

Thank you for reading this article, please a comment if you are interested.

Tiến Phan - R0039

Knowledge is Endless

Sharing for Success


0 nhận xét:

Post a Comment