Monday, January 16, 2006

Command Line "mail" with SMTP

Back in the olden days*, when you wanted to send email from a Unix terminal, it went something like this:

$ mail
Subject:  Here's my email

Mom said we should talk more often.  So I'm sending you this email.

And Joe would eventually get the email. (The ^D is Control-D, used to denote an end of file.) Now the nice thing about the old "mail" command is that it would accept anything from standard input. So if I wanted to send Joe a really long email I could put it in a big file, and pipe it into the mail command:

$ cat bigfile.txt | mail -s "Dear Joe"


$ mail -s "Dear Joe" < bigfile.txt

(The -s indicates that the next entry is the subject line of the email, the subject itself is in quotes to keep the command shell from thinking it's a bunch of options.)

This is fine so long as your local computer handles its own mail. However, these days home users mostly deal with ISPs, who have their own mail systems, which we usually access with an email client that understands the Simple Mail Transfer Protocol. These days, your email clients are usually graphical ones like Evolution, Thunderbird, Eudora, or Mozilla, but the all-you-can-eat text editor, Emacs, also can send mail via SMTP.

The program that can't send mail via SMTP is the old Unix "mail" command. It isn't the fault of "mail" itself, it's the fault of your Linux box. "mail" looks for a program called "sendmail", or its replacement, to send mail through the system. Now most sendmail-like programs assume that your computer, or one on your network, is your mailserver. They don't speak to your ISP via SMTP.

Why do I care? Well, a friend recently asked me if he could generate a file on his computer and send it to another friend without him having to do anything else. On a system with "mail" working, it's easy. Suppose the command "genbignovel" creates a large text file "pulitzer.txt". Then to write the novel and send it to Joe, who's on the Pulizter committee, I could create a small text file:

mail < pulitzer.txt

which sends the file to Joe as soon as it's generated. If I'm using Evolution, Thunderbird, etc., then I have to send the file as an attachment, which means a bunch of mouse-clicks, at the very least.

So what we need is a replacement for sendmail which understands SMTP. A Google search reveals many, but the most popular one seems to be msmtp. I decided to try it out, so I downloaded the file, installed it using checkinstall, and tried to figure out how to set it up.

It's not too hard, though most people seem to use msmtp with mutt rather than mail. First, you need to create a file to tell msmtp which SMTP server you want to use, and the information needed to access that account. This is done through a file called ~/.msmtprc, i.e., a file named .msmtprc that lives in your home directory. Here's mine, with a few obvious changes:

account default
auto_from off
auth on
password xyzzymouse
tls on

This obviously works on gmail, but you should be able to modify it for any SMTP server. You can look at the msmtp manual to see what everything means.

Now how do I get "mail" to use msmtp? Simplicity itself. mail uses a command file called ~/.mailrc. Create it, if you don't have one yourself, and add a line:

set sendmail=/usr/local/bin/msmtp

if you put msmtp in /usr/local/bin.

And now mail should work. It does from here, anyway.

Good God, how many posts of mine start with "back in the olden days?" Must remember that not everyone sent their first email in 1983.


Sriram S said...

Very nice article...