sending mail from terminal after a script

I am new to Terminal.

I created a backup script which runs every morning on my iMac with OS 10.6.6. The script is launched successfully by Launchd from /Library/LaunchDaemons.

I would like to have the script send me an email once the backup job is done. I saw a few information about commands that can be added at the end of a script, like mail or mailx (for instance mail -s "Mysubject" myname@gmail.com or telnet smtp.gmail.com). I tried to run any of these lines from Terminal without success. I always get some kind of timeout of a sort.

I believe that I may have to configure my iMac in a way to be able to use these commands but I am not sure how and where to start.

Also, I am using Google Apps (on top of having also a Gmail.account). Not sure if I have to open some ports to make "mail" work.

Thanks for any kind of help or hints,

Lanceloz

iMac MBP, Mac OS X (10.6.6)

Posted on Mar 19, 2011 4:20 PM

Reply
13 replies

Nov 9, 2013 7:27 AM in response to Mark3D

Mark3D: This is probably a rather different question than the OP, FWIW... Particularly given you're probably using Cocoa based on that NSTask spawn reference. If you are working in Cocoa, I'd probably use a framework and not spawn off some bash or Perl or other scripted code and deal with the somewhat sketchy error handling that can result from the NSTask approach... For some available code, see the ED Frameworks, this discussion, the libmailcore framework, and maybe this, this or maybe this over at StackOverflow There are probably other frameworks around, and I'd be very surprised if there wasn't some generic C code around for SMTP submissions. I'd also suggest availing yourself of the Apple developer forums for programming questions, or the Apple mailing lists. The Apple mailing list archives are a gold-mine of code, too. While we do see some programming questions around here, but there are far more in these other areas.

Mar 19, 2011 4:46 PM in response to lanceloz2

Welcome to the forums.

Bash is a fairly standard command environment, so you can cast a wide search for resources and examples; there's a whole lot of stuff available.

In this case, Google finds various examples of this command, including [this|http://theos.in/shell-scripting/send-mail-bash-script> and [this|http://stackoverflow.com/questions/5155923/sending-a-mail-from-a-linux-sh ell-script], and there are various other more advanced examples and resources, including a script that mails itself (see example 16-45) within the [Advanced Bash Scripting|http://tldp.org/LDP/abs/abs-guide.pdf] document.

If you're just getting going with bash, the Advanced Bash Scripting document might be too much, and the O'Reilly books on Bash and the Bash Cookbook that might be helpful to learning the environment.

Mar 20, 2011 8:40 AM in response to Pete Corelio

Yes, sendEmail is very simple to use.
Here's a sample that I have at the end of a bash script:

/usr/local/bin/sendEmail -q -o message-charset=utf-8
-f "Tony <tony@gmail.com>"
-t tony2@gmail.com
-s smtp.gmail.com:587
# -xu: username for SMTP authentication
-xu mygmailaddress@gmail.com
# -xp: password for SMTP authentication
-xp mygmailpassword
-u "My Script Completed"
-m "My Script completed at: $(date)"

Nov 9, 2013 1:14 AM in response to Tony T1

sendEmail works pretty well, but there are a couple of bugs.


1)

First of all, the version 1.5.6 by Brandm Zehm (thank you Brandom!) had a bug at the timeout parameter, easily fixable. The following command must have the "Timeout" parameter capitalized, and not lowercase (timeout, as I found). Fix it this way.


$SERVER = IO::Socket::INET->new( PeerAddr => $conf{'server'},

PeerPort => $conf{'port'},

LocalAddr => $conf{'bindaddr'},

Proto => 'tcp',

Autoflush => 1,

Timeout => $conf{'alarm'},


Then I added "-o timeout = 15" to the command line, and e.g. in case of wrong SMTP, the program properly exits at the timeout, after 15 seconds.


2)

Another bug I was not able to fix yet (please help) is that in case of wrong port, no mather the timeout, the program hangs up forever. For example I tried to send and email from my gmail account, using the SMTP smtp.gmail.com and the port 465 (which works fine with Entourage) and program hanged up. Using the port 25 everything worked well. I am puzzled. Is anybody who might help us here? Thank you.

Nov 9, 2013 5:31 AM in response to Tony T1

What I mean is that if the user doesn't know the right port and tries to set an arbitrary port, e.g. 465, sendEmail hangs up forever. No matter whether the timeout is, e.g. 5 seconds. To avoid the hang up, I launch the procedure in a thread that invokes sendEmail through an NSTask. If the program hangs up, I let the user press a button to interrupt the NSTask. Then a NSTaskDidTerminateNotification calls my method which cleans everything. What a mess. So, if the timeout worked well even in case of a wrong port, returning after 5 seconds, I could have avoided all of this mess.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

sending mail from terminal after a script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.