Send Email Using External SMTP Server on Ubuntu Server
In a previous article, I showed you how to send automated email backup to your email address so you can easily backup your WordPress site using Linux command line and you don’t have to install WordPress plugin for that purpose.
Heirloom-mailx is a MTA that can be configured to use external email server such as Gmail to send emails from your Linux Server. On Debian and CentOS/RHEL server it works like a charm. But if you are using Ubuntu server, you may encouter difficulty in making it work. This article will explain how to use sSMTP to send emails through external SMTP server.
Installing and Configuring sSMTP
sSMTP is very simple SMTP server. on Ubuntu you can install sSMTP with the following command:
sudo apt-get install ssmtp
Open configuration file:
sudo vi /etc/ssmtp/ssmtp.conf
Configure the following lines with your Gmail credentials
root=YourAccount@gmail.com mailhub=smtp.gmail.com:587 AuthUser=YourAccount@gmail.com AuthPass=YourGmailPassword UseTLS=YES UseSTARTTLS=YES
Sending Email with the mail command
sSMTP works with many MUAs such as heirloom-mailx. Install heirloom-mailx using the following command:
sudo apt-get install heirloom-mailx
Now you can use the mail commmand to send email through Gmail, like this:
echo "mail body" | mail -a path-for-attachment-file -s "mail subject line" [email protected]
Cron Mail Settings
By default cron will send an email to the user that owns the crontab file when a task encouter some problem. And you can find that by executing mail command on your server. However, because we configured sSMTP to relay email through Gmail, Gmail can not send that email to the user on your machine if you machine does not have a full-fledged mail server such as postfix installed.
What we can do is we can add a MAILTO variable in the crontab file like this:
You can put it at the beginning of the crontab file. Then save and close the crontab file. Now Gmail can find the email address. And you will receive cron report in the email address specified to MAILTO variable.