Command line mail

Q In a recent issue there was a mention of a program that could use my ISP's SMTP server to send emails from the CLI. Can you remind me what its name was?

A Any of the standard mail transfer agents, such as Postfix or Sendmail, can do this, but you probably don't want to install a full-blown MTA for this simple task. The standard mail program will do this too, but it requires a local MTA. If all you want to do is send mail via your ISP's mail server, the best program for the job is ssmtp ( Although this is a Debian program, Fedora, Mandriva and Ubuntu also have it in their repositories, and Gentoo installs it as part of the core system package set. However, this is one of those programs that you must set up before you can use it, by editing /etc/ssmtp/ssmtp.conf as root. The key setting is mailhub, which must be set to the name of your ISP's SMTP server. If you want to send mail via a port other than the standard 25, you need to append this to the address, as in

Other options you may need to set, depending on your ISP's setup, are UseTLS for secure communication with the server and AuthUser/AuthPass if you are required to log in before sending mail. A simple rule of thumb is that whatever you need to change for the defaults in your normal mail client needs to be set here. Using ssmtp is the same as using sendmail. In fact, it also installs a sendmail program, so any program that expects to send via a local MTA can send via your ISP with ssmtp. Sending mail via ssmtp is done by feeding it the mail on standard input and giving the destination address on the command line.

/usr/sbin/ssmtp -t <<EOF
From: My Name <>
To: Your Name <>
Date: $(date -R) Subject: Just a test
This is a test of ssmtp

In this case, the destination address is included in the headers sent to ssmtp - the -t option enables this. Everything between <<EOF and EOF is fed to the program's standard input, so it's much easier than a bunch of echo lines. At other times, you may want to send the output of a program or script by email, in which case you may use

myprogram | ssmtp

The program called mail, which needs an MTA to run, provides more options for sending mail, such as including the subject on the command line. Since ssmtp emulates sendmail, you can use this with ssmtp if you want the extra features. This requires no extra setting up - just install the mailx package for your distro and read the man page for the extra options - but for starters try

myprogram | Mail -s "Output from myprogram"

Follow us on or Twitter

Username:   Password: