LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices

Reply
 
Search this Thread
Old 07-25-2008, 07:00 PM   #1
r.stiltskin
Member
 
Registered: Oct 2003
Location: USA
Distribution: xubuntu
Posts: 186

Rep: Reputation: 21
(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.)

Last edited by r.stiltskin; 07-25-2008 at 11:18 PM.
 
Old 07-25-2008, 08:35 PM   #2
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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?
 
Old 07-25-2008, 11:17 PM   #3
r.stiltskin
Member
 
Registered: Oct 2003
Location: USA
Distribution: xubuntu
Posts: 186

Original Poster
Rep: Reputation: 21
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.

-------------------------------------------------------------------------
USING MAIL + MSMTP TO SEND EMAILS FROM A TERMINAL PROMPT OR FROM A SCRIPT:
-------------------------------------------------------------------------
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:
Code:
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:
Code:
# .msmtprc    ##configuration file for msmtp

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

#account default : verizon
notes:
- 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):
Code:
~$ 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:
Code:
system("echo \"Body of the message\" | mail -s \"Subject line\" recipient1@address1 recipient2@address2");

Last edited by r.stiltskin; 07-25-2008 at 11:52 PM. Reason: corrected list of packages installed & uninstalled
 
Old 07-25-2008, 11:50 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
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.
 
Old 07-26-2008, 12:00 AM   #5
r.stiltskin
Member
 
Registered: Oct 2003
Location: USA
Distribution: xubuntu
Posts: 186

Original Poster
Rep: Reputation: 21
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.

Last edited by r.stiltskin; 07-26-2008 at 12:01 AM.
 
Old 07-26-2008, 12:08 AM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
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.
 
Old 07-26-2008, 12:30 AM   #7
r.stiltskin
Member
 
Registered: Oct 2003
Location: USA
Distribution: xubuntu
Posts: 186

Original Poster
Rep: Reputation: 21
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?
 
Old 07-26-2008, 12:36 AM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
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.

Last edited by Mr. C.; 07-26-2008 at 12:38 AM.
 
  


Reply

Tags
commandline, email, mail, msmtp, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to send email... bfloeagle Linux - General 5 10-13-2009 10:20 AM
php script don't send email with qmail pk_kala Linux - Server 8 05-02-2009 08:33 AM
How to send an email within a script??? johnsanty Programming 19 09-02-2006 12:43 PM
write a shell script to send email yenonn Programming 4 02-11-2005 10:06 PM
script to send email message meluser Programming 2 03-26-2003 12:35 AM


All times are GMT -5. The time now is 09:07 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration