Test email from command line

When you set up email forwarding or configure a new MTA, you will want to confirm it is working before relying on it for alerts. A quick test is all you need.

Send a test message

mail -s Test myEmail@gmail.com

If the mail command is not available, install it with apt-get install mailutils. Then check your inbox.

If it does not arrive

Run mailq to see if the message is stuck in the queue. The mail log at /var/log/mail.log will also show the delivery attempt and any errors.

Enjoy!

Send system emails using gmail as smtp server

I wrote before about sending system emails using sendmail as a smarthost. Here is an alternative approach using Gmail SMTP servers — often simpler to set up for home servers and small VPS.

The software that makes this easy is called ssmtp. It is a lightweight MTA that forwards mail to a configured SMTP relay, perfect for this use case.

Installation and configuration

Install ssmtp:

apt-get install ssmtp

Edit /etc/ssmtp/ssmtp.conf:

root=myAccount@gmail.com
mailhub=smtp.gmail.com:587
hostname=myHostname.com
UseSTARTTLS=YES
AuthUser=myAccount
AuthPass=myPassword
FromLineOverride=YES

And set your address in /etc/ssmtp/revaliases:

root:myAccount@gmail.com:smtp.gmail.com:587

That is it. From now on, system emails are delivered through your Gmail account. If you use two-factor authentication, generate an app-specific password instead of using your main password.

Enjoy!

Linux: How to forward system emails

By default, most Linux systems deliver system emails — cron output, monitoring alerts, security notices — to the local root mailbox. If you never check it, those messages pile up and you might miss something important.

The simplest way to deal with this is to forward them to an external address. The most common method is the /etc/aliases file. Add a line like:

root: you@example.com

Then run newaliases to rebuild the alias database. Local mail will be forwarded to the external address via your MTA.

For per-user forwarding, create a .forward file in the user’s home directory with your email address in it. This doesn’t need an alias rebuild and works even if you don’t have sudo access.

Enjoy!

How to configure sendmail as a SMART HOST

Sometimes I need to test the email functionality of a PHP website on my local machine. The best and easiest way I found so far is using sendmail as a smart host.

A smart host is a piece of software that routes email to an intermediate mail server rather than directly to the recipient’s server. The only thing you need is the SMTP address of your internet service provider. For example, my ISP is Virgin Media and their SMTP server is smtp.virgin.net.

First of all you have to install sendmail. On Ubuntu:

sudo apt-get install sendmail

Then open your configuration file:

sudo vi /etc/mail/sendmail.mc

and append or modify the following line to define your ISP SMTP server:

define(`SMART_HOST',`smtp.virgin.net')

Remove the “dnl” word at the end if there is one. Now login as root and regenerate the sendmail config file:

m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

and restart sendmail:

service sendmail restart

Now your local machine should be able to send emails. The easiest way to test is from the PHP interactive shell:

php -a
mail('myemail@domain.com','TEST','WORKING?!?');

Enjoy… :-)