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: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.comsudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
Now open your postfix config file:
vim /etc/postfix/main.cf
and add the following lines to it: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.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
Now let's create the sasl_passwd file and put the username & password.
And add following line:vim /etc/postfix/sasl_passwd
If you want to use your Google App’s domain, than replace[smtp.gmail.com]:587 USERNAME@gmail.com:PASSWORD
@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
Reload postfix config for changes to take effect:cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem
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 errorError: “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.
0 comments:
Post a Comment