First of all you need to install and configure Postfix to Use Gmail SMTP on Ubuntu.
Install all necessary packages:
$ sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
If you do not have postfix installed before, postfix configuration wizard will ask you some questions. Just select your server as Internet Site and for FQDN use something like mail.example.com
Then open your postfix config file:
$ sudo -H gedit /etc/postfix/main.cf
and add following lines to it:
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
You might have noticed that we haven’t specified our Gmail username and password in above lines. They will go into a different file. Open/Create:
$ sudo -H gedit /etc/postfix/sasl_passwd
And add following line:
If you want to use your Google App’s domain, please replace @gmail.com with your @domain.com.
Fix permission and update postfix config to use sasl_passwd file:
$ sudo chmod 400 /etc/postfix/sasl_passwd
$ sudo postmap /etc/postfix/sasl_passwd
Next, validate certificates to avoid running into error. Just run following command:
$ cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem
Finally, reload postfix config for changes to take effect:
$ sudo /etc/init.d/postfix reload
Check if mails are sent via Gmail SMTP server
If you have configured everything correctly, following command should generate a test mail from your server to your mailbox.
echo "Test mail from postfix" | mail -s "Test Postfix" email@example.com
To further verify, if mail sent from above command is actually sent via Gmail’s SMTP server, you can log into Gmail account USERNAME@gmail.com with PASSWORD and check "Sent Mail" folder in that Gmail account. By default, Gmail always keeps a copy of mail being sent through its web-interface as well as SMTP server. This logging is one strong reason that we often use Gmail when mail delivery is critical.
Error: "SASL authentication failed; server smtp.gmail.com"
You need to unlock the captcha by visiting this page
You can run test again after unlocking captcha.
You need to use following syntax of
to send emails, note that if you want to send attachment file via
command it's not support or it's better I say I can not send my attached file via
command, instead you can use
command line, it's very useful. and in
command you have to type attachment arguments after the email address. I test it and works fine.
you can install
mutt via this command:
$ sudo apt-get install mutt
mail -s "TestSubject" firstname.lastname@example.org -a "UserReport.txt" < MessageBody.txt
mutt -s "TestSubject" email@example.com -a "UserReport.txt" < MessageBody.txt
UserReport.txt is your attachment file,
MessageBody is text/file of your body of email,
TestSubject is your email subject.
-s flag is used for "Subject" and
-a flag is used for "Attachment file"