Introduction:
This guide will help you to use a Gmail account as a free SMTP server on
your Ubuntu-Linux server 14.04. Once configured, all emails from your
server will be sent via Gmail. This method will be useful if you have
many sites on your server and want them all to send emails via Gmail’s
SMTP server.
There are many reasons why you would want to configure Postfix to send
email using an external SMTP provider such as Google Apps (Gmail),
Mandrill, SendGrid, Amazon SES, or any other SMTP server. One reason is
to avoid getting your mail flagged as spam if your current server’s IP
has been added to a spam list.
Prerequisites
Before starting this tutorial, you should have:- Debian 7 installed on your Linode
- Your fully qualified domain name (FQDN)
- A valid username and password for the SMTP mail provider, such as Google Apps, Mandrill, or SendGrid
- Make sure the libsasl2-modules package is installed and up to date:
sudo apt-get install libsasl2-modules
open the
/etc/postfix/main.cf
file with your favorite text editor:
#vim /etc/postfix/main.cf
Make sure that the myhostname parameter is configured with your server’s FQDN:
myhostname = mail.xm.shv
Configuring SMTP Usernames and Passwords
#vim /etc/postfix/sasl_passwd
Add your destination (SMTP Host), username, and password in the following format:
[mail.isp.example]:587 username:password
example with gmail: [smtp.gmail.com]:587 xm.alert@gmail.com:password
Create the hash db file for Postfix by running the
postmap
command:
#postmap /etc/postfix/sasl_passwd
If all went well, you should have a new file named
sasl_passwd.db
in the /etc/postfix/
directory.Securing Your Password and Hash Database Files
The/etc/postfix/sasl_passwd
and the /etc/postfix/sasl_passwd.db
files created in the previous steps contain your SMTP credentials in plain text.For security reasons, you should change their permissions so that only the root user can read or write to the file. Run the following commands to change the ownership to root and update the permissions for the two files:
| #chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
#chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
Open the
#vim /etc/postfix/main.cf
Update the relayhost parameter to show your external SMTP relay host. Important: If you specified a non-default TCP port in the
then you must use the same port when configuring the relayhost parameter. |
# specify SMTP relay host
relayhost = [mail.isp.example]:587
At the end of the file, add the following parameters to enable authentication:
/etc/postfix/main.cf
# enable SASL authentication
smtp_sasl_auth_enable = yes
# disallow methods that allow anonymous authentication.
smtp_sasl_security_options = noanonymous
# where to find sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# Enable STARTTLS encryption
smtp_use_tls = yes
# where to find CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Testing Postfix
The fastest way to test your configuration is to send an email to any unrelated email address, using themail
command:echo "body of your email" | mail -s "This is a Subject" -a
"From: you@example.com" recipient@elsewhere.com
Alternatively, you can use Postfix’s own sendmail implementation,
by entering lines similar to those shown below:
sendmail recipient@elsewhere.com
From: you@example.com
Subject: Test mail
This is a test email
Troubleshooting
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://easyengine.io/tutorials/linux/ubuntu-postfix-gmail-smtp/
-https://www.linode.com/docs/email/postfix/postfix-smtp-debian7