Friday 12 February 2016

How to send mail on ubuntu or linux system

Configure Postfix on Ubuntu to Use Gmail SMTP

or

How to send mail on ubuntu 

This post is about how to setup postfix or how to send a mail on ubuntu linux server. If you face any problem, share it with us in the comments section.
This is a usecase of sending mail from ubuntu system.

Setup

Let's install the required packages:


sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
If you had postfix already setup than, you can skip the process while installing postfix. A wizard will open up, just select your server as Internet Site and for FQDN use something like mail.example.com

Now open your postfix config file:


vim /etc/postfix/main.cf
 
and add the 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
These lines are simple to understand, we are simply specifying few settings like we will be using GMAIL and it would be password protected, the file having username & password is /etc/postfix/sasl_passwd, the certificate file is /etc/postfix/cacert.pem and we are using the tls encryption.

Now let's create the sasl_passwd file and put the username & password.


vim /etc/postfix/sasl_passwd
And add following line:


[smtp.gmail.com]:587    USERNAME@gmail.com:PASSWORD
If you want to use your Google App’s domain, than 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

Let's, validate certificates now to avoid running into error. If you don't  have the file Thawte_Premium_Server_CA.pem  is not there than you can get one from your SSL provider or you can create your self signed certificate from here.

Run following command


cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem
Reload postfix config for changes to take effect:


sudo /etc/init.d/postfix reload

Check the setup by sending the mail

If you have done everything correctly, than now you would be able to get a mail in your inbox from ubuntu after running the following command.


echo "Test mail from ubuntu" | mail -s "Testing Setup" you@example.com 

After running the above command you should see a mail in the inbox of you@example.com and to verify if it has been sent via the user specified by us in sasl_passwd file, check the sent mail of that account.

Note: Gmail’s SMTP server has a limit of 500 emails per day. So use wisely!

Getting Error?

You might encounter the following error

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

Usage

You can use this utility to send mail to notify some important events like if Nginx stopped working. This post explains how to notify the Sysadmin if Nginx stopped working

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel