READ: https://devanswers.co/configure-postfix-to-use-gmail-smtp-on-ubuntu-16-04-digitalocean-droplet/
Prerequisites
If your Gmail account uses 2-Step Verification, you must create an application specific password.
If you’re not using 2-Step Verification, please ensure that your Gmail account is configured to allow less secures apps.
Remove sendmail
|
1 |
sudo apt-get remove sendmail |
1. Install Postfix
|
1 2 |
sudo apt-get update sudo apt install -y postfix mailutils |
Let’s update the package database first. On the first Postfix configuration screen, select OK by pressing TAB and ENTER. Select Internet Site and press ENTER. System mail name should be your domain name eg. example.com, press ENTER.
My mail name is teddy@teddy.localdomain
<NOTE>
PROBABLY THESE BELOW COMMAND NOT NEEDED:
I also need to install php-mail and php7.2-mail
|
1 |
sudo apt install php-mail and php7.2-mail |
modify php ini IN
|
1 |
sudo gedit /etc/php/7.2/fpm/php.ini |
OR FOR APACHE2 WEB SERVER
|
1 |
sudo gedit /etc/php/7.2/apache2/php.ini |
UNCOMMENT ‘sendmail_path’ THEN CHANGE TO
|
1 |
sendmail_path = /usr/sbin/sendmail -t -i |
modify
|
1 |
sudo gedit /etc/hosts |
like this:
|
1 2 3 |
127.0.0.1 teddy.localdomain teddy localhost #127.0.0.1 localhost #127.0.1.1 teddy |
Restart services
|
1 2 |
sudo service php7.2-fpm restart sudo service nginx restart |
OR FOR APACHE2 WEB SERVER
|
1 |
sudo service apache2 start |
</NOTE>
Configure Postfix
Edit the Postfix configuration file.
|
1 |
sudo gedit /etc/postfix/main.cf |
Add the following to the end of the file.
|
1 2 3 4 5 6 |
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 |
Save file and exit.
Create Password and DB Files
Create the sasl_passwd file which will store our credentials.
|
1 |
sudo gedit /etc/postfix/sasl_passwd |
Insert the following:
|
1 |
[smtp.gmail.com]:587 username@gmail.com:password |
Replace username and password with your own. Save file and exit. (Press CTRL + X, press Y and then press ENTER)
Create a hash database file for Postfix with the postmap command.
|
1 |
sudo postmap /etc/postfix/sasl_passwd |
There should now be a file called sasl_passwd.db in the /etc/postfix/ directory.
For added security, we will only allow root user to read and write to sasl_passwd and sasl_passwd.db
|
1 2 |
sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db |
Sign Certificate
Now we are going to create the certificate.
|
1 |
cat /etc/ssl/certs/thawte_Primary_Root_CA.pem | sudo tee -a /etc/postfix/cacert.pem |
There should now be a certificate file called cacert.pem in /etc/postfix
Send a Test Mail
We’ll now send a test email message. Make sure to replace test@example.com with your own email address.
|
1 |
echo "Test Email message body" | mail -s "Email test subject" advcha@yahoo.com |
Don’t forget to check your spam folder.
If you still haven’t received any mail, check the mail error log.
|
1 |
sudo tail /var/log/mail.log |
NOTE:
TO USE GMAIL SMPT, YOU NEED TO ACTIVATE (SET IT ON) ‘Less Secure app’ ON YOUR GMAIL ACCOUNT. LOGIN TO YOUR GMAIL THEN OPEN THIS LINK: https://www.google.com/settings/u/2/security/lesssecureapps
IT’D REDIRECT YOU THE ‘Less Secure app’ SETTING.