-   Ubuntu (
-   -   send email from a script (

r.stiltskin 07-25-2008 08:00 PM

(SOLVED) send email from a script
I've been using nail and msmtp to enable a script to send simple emails.

But the nail package has been dropped from Ubuntu Hardy. It seems to have been put back in Intrepid but I'd rather stick with the LTS version (Hardy). Is there a version of nail available that I can install in Hardy? Or an easy replacement? (So far every other mail program I've looked at is far too complicated to set up.)

billymayday 07-25-2008 09:35 PM

Doing a bit of a search suggests to me that nail has been renamed mailx. Does that exist in the Hardy repos?

Otherwise, why not just use the "mail" command?

r.stiltskin 07-26-2008 12:17 AM

I believe that mailx is something else -- Ubuntu Intrepid has both mailx and nail -- but I don't know what the differences are. The main reason I was using nail was that at some point a few years ago I found a fairly clear, simple explanation of how to use nail + msmtp to send simple emails from the command line. In general, the documentation for the various MUAs and MTAs seem overwhelmingly complicated and poorly explained. It's amazing how simple this turns out to be.

Anyway, I had already installed smail (and uucp) and uninstalled exim4. The man pages for smail were just as bewildering as the others -- that's why I was looking for nail. (Apparently the mail command is provided by either mailx or smail, because after I had this all working, I uninstalled both mailx and smail -- and tried to send an email but got an error -- bash: /usr/bin/mail: No such file or directory -- so I reinstalled smail, uucp and mailx.)

But thanks for your suggestion to use the mail command -- it may seem obvious to you but to the uninitiated it's anything but obvious. That turns out to be the easy solution & I guess mailx, smail and nail are all unnecessary for this simple application. The manpage for msmtp actually tells how to write both configuration files.

For anyone else struggling with this (and for me next time I forget how to do it), here is a summary. This is, I believe, the minimum necessary for barebones functionality, which is probably enough for many applications.

mail (/usr/bin/mail) is provided by mailx or smail (smail requires mailx and uucp), and msmtp provides the interface to the isp that forwards the mail. So:
~$ sudo apt-get install msmtp smail uucp
(mailx will be installed as a dependency)

In your home directory, create a ~/.mailrc file containing just:

set sendmail=/usr/bin/msmtp
Create a config file for msmtp; mine is ~/.msmtprc but I think a systemwide /etc/msmtprc file would work as well. For my Verizon account this file consists of:

# .msmtprc    ##configuration file for msmtp

#account    verizon
from        [from address to appear on the email]
auth        login
tls        off
user        [username]
password    [************]

#account default : verizon

- The lines "account verizon" and "account default : verizon" are commented out -- they seem to be unnecessary if only 1 account is listed in the file.
- The [from address ...] can be whatever you want to appear on the email; it does not have to be your isp account address.
- The line "auth login" is to be copied exactly. This "login" is NOT your username.
- You'll have to determine the appropriate entries for the host, auth, and tls lines, depending on your isp.

To send an email manually from a terminal prompt, type a first line consisting of the command, the subject and one or more recipient addresses (no commas):

~$ mail -s "the subject line" [first recipient] [second recipient] [...]
followed by <enter>.
Then type the body of the message. This can be on multiple lines (<enter> will not terminate the message).
Finally, to terminate the message, type <enter> at the end of the last message line, and then type ctrl-D at the beginning of the next line.

To send an email from, say, a perl script, include a line similar to this in the script:

system("echo \"Body of the message\" | mail -s \"Subject line\" recipient1@address1 recipient2@address2");

Mr. C. 07-26-2008 12:50 AM

SMTP mail is complex - no two ways about it. There is excellent documentation available, but you have to know where to look. It is also very important to understand the SMTP protocol, and these are described in RFCs. It simply cannot be simplified, and MTA documentation should not undertake the role of trying to describe the SMTP protocol + extensions.

Use what works best for you.

r.stiltskin 07-26-2008 01:00 AM

On the other hand, one shouldn't have to be a fully-qualified Postmaster just to spit out an occasional 1-line alert as part of a cron job. I shouldn't have had to waste hours puzzling this out. I hope it saves someone else some time.

Mr. C. 07-26-2008 01:08 AM

How often you send mail, or how large the content doesn't change the fact that you *are* acting as a postmaster by setting up a mail sending machine.

Take a similar analogy: you build your own telephone following some HowTo, and expect it to just work by plugging it into the phone network. It doesn't matter that you are sending only occasional, very brief calls. You still have to follow the specs, get the right voltages, have the correct wiring, etc.

What you do instead is pay for somebody else's expertise when you purchase a phone. You can do likewise with an email appliance. But once you go the Do It Yourself route, you really have to do it yourself. Nobody forced you to "waste hours" - it was your choice to do it on the cheap.

r.stiltskin 07-26-2008 01:30 AM

Interesting analogy, but the fact remains that ultimately I needed only those two very simple config files and a single line of code -- hardly rocket science. Clearly this doesn't qualify me to manage an organization's email system, but that wasn't my goal. Why shouldn't simple documentation be available to accomplish simple tasks?

Mr. C. 07-26-2008 01:36 AM

BECAUSE there are an infinite number of simple tasks that may be accomplished; documenting each and every one requires HUGE amounts of documentation. You just have to find what's available; and when you don't, folks like you help write something up.

Your recipe won't work for everyone; some have to go find the MTA, build it, pull in build kits, etc. Its not as trivial as you make it.

You're focused on your own world and needs - keep perspective, there are 8 billion others out their, each with their own ideas, needs, goals.

All times are GMT -5. The time now is 04:44 AM.