What if I tell you that this article has already existed for 400 days in my draft list (more than a year)? This probably makes you wonder how ridiculous I am to not finish it earlier. Okay, the fact is that I put too much content in one article. Now, I plan to break it down into a series of article.
In this article, you will learn how to setup a free Gmail Relay on your Ubuntu server. There maybe quite a bunch of online resources doing similar thing, but I prefer to write my own as an example and to refresh my forgetful brain. 😀
Installing Postfix
Install Postfix using the following command:
sudo apt-get install postfix
You will see this prompt at some point. Select Internet Site.
Postfix Configuration
Once the installation is finished, change your terminal’s current directory to
/etc/postfix
Configure Gmail account and password
create a file call gmail_passwd, replace the information with your credentials.
[smtp.gmail.com]:587 username@gmail.com:password
execute the following command to generate a postfix database.
sudo postmap /etc/postfix/gmail_passwd
For safely reason, you should allow only the root user can access this 2 files.
sudo chown root:root /etc/postfix/gmail_passwd /etc/postfix/gmail_passwd.db sudo chmod 0600 /etc/postfix/gmail_passwd /etc/postfix/gmail_passwd.db
Configure your Relay Server
open /etc/postfix/main.cf with whatever editor you like:
vim /etc/postfix/main.cf
add the following line to map your credentials
smtp_sasl_password_maps = hash:/etc/postfix/gmail_passwd
Enable TLS and sasl authiencation for Gmail SMTP Server
smtp_use_tls = yes smtp_sasl_auth_enable = yes
Gmail SMTP Server does not support postfix’s security options. Reset them by:
smtp_sasl_security_options =
Relayhost field means the SMTP relay host. Please put the GMail SMTP server address here. Make sure this address is identical to the one in gmail_passwd
relayhost = [smtp.gmail.com]:587
here is the complete version of the main.cf:
# See /usr/share/postfix/main.cf.dist for a commented, more complete version # Debian specific: Specifying a file name will cause the first # line of that file to be used as the name. The Debian default # is /etc/mailname. #myorigin = /etc/mailname smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu) biff = no # appending .domain is the MUA's job. append_dot_mydomain = no # Uncomment the next line to generate "delayed mail" warnings #delay_warning_time = 4h readme_directory = no # TLS parameters smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key smtpd_use_tls=yes smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache smtp_use_tls = yes smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache smtp_sasl_password_maps = hash:/etc/postfix/gmail_passwd smtp_sasl_auth_enable = yes smtp_sasl_security_options = # See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for # information on enabling SSL in the smtp client. smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination myhostname = office-local alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases mydestination = office-local, localhost.localdomain, , localhost relayhost = [smtp.gmail.com]:587 mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all
reload the configuration:
service postfix reload
check the status by:
tail -f /var/log/mail.log
check mail.err if there is any error. This file may not exist if there is no error, or it is the first time of you to configure mail related service.
tail -f /var/log/mail.err
Gmail Account Setting
Postfix may not works with the default security level of Gmail. Please turn on the “allow less secure apps” option via this link:
https://www.google.com/settings/security/lesssecureapps
Testing
mailx is a popular utility in Linux World for sending and receiving mail in terminal. Execute the following command to install it.
sudo apt-get install mailx
Now you can test your postfix configuration by sending a mail to someone. Replace to@whatever.com with a valid email address.
echo "Hello Body" | mail -s "Hello Subject" -a to@whatever.com
If it was sent successfully, you should see this in the mail.log:
Apr 16 08:41:57 office-local postfix/smtp[1274]: 1603753E: to=<xxx@whatever.com>, relay=smtp.gmail.com[64.233.189.108]:587, delay=1723, delays=1720/0.01/1.4/0.85, dsn=2.0.0, status=sent (250 2.0.0 OK 1460796117 h5sm69972904pat.0 - gsmtp) Apr 16 08:41:57 office-local postfix/qmgr[1197]: 1603753E: removed
Trouble Shooting
Apr 16 08:27:23 office-local postfix/smtp[1007]: E81E93C5: to=<xxx@whatever.com>, relay=smtp.gmail.com[64.233.189.109]:587, delay=7, delays=0.01/0.01/7/0, dsn=4.7.14, status=deferred (SASL authentication failed; server smtp.gmail.com[64.233.189.109] said: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtS?534-5.7.14 qQtdCND0kjQn0lwMS5NJc9nXMTotKJG76X83IjhjNi5jaGjyZed5WfHf_Rm0SxoaiwFAAU?534-5.7.14 tvpINwFpyp2YOKpYmEnCnlmUn33voaf7jG8lN71043p3bgcKNY8Zqk2WB-zKKEoWW58zkM?534-5.7.14 uwn5lZcHpxEJvx5HXAEf0MQcOu_EVJLEFByoP35IPcKquJM5qnI4nBVGzFD93uC6IaSxDH?534-5.7.14 DhDeKmFa94G2XQXZItNqqLtrW8Yco> Please log in via your web browser and?534-5.7.14 then try again.?534-5.7.14 Learn more at?534 5.7.14 https://support.google.com/mail/answer/78754 n74sm69735534pfa.45 - gsmtp)
your Gmail account is not allow less-secure apps to sign-in
Reference
http://mhawthorne.net/posts/postfix-configuring-gmail-as-relay.html
https://easyengine.io/tutorials/linux/ubuntu-postfix-gmail-smtp/
What’s սp to all, thе сontents present at this site are realⅼy amazing for people
experiеnce, well, keep up the nice work feⅼlows.
Brilliant read. Very rare nowadays as most people just write a quick article just to get more views. This one is different! Both informative and helpful. Thank you!