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

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:

[email protected]

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.

Rate this tutorial
[Total: 4 Average: 3]

One Response to “Send Email Using External SMTP Server on Ubuntu Server

  • FINALLY!!!!!!!!

    I tried 100 different services , thanks god this is the best service over all , simple 2 things and DONE!

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community ( for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here