do shell script "mail"

I need help understanding the command line utility “mail.” I would like to use it in one of my scripts, and a sender’s address really isn’t ALL that important, but can anyone explain me how to use it. The MAN page is way too long for me to understand. Can someone help?

What is it that you actually want to do?

If it involves sending email to an external email account (i.e. one not on the local machine), then you probably do not want to use mail. The only way it “knows” to send email is to pass it along to the local sendmail program (which comes from the Postfix package on standard Mac OS X systems, but might be from Sendmail or some other Mail Transfer Agent package). Unless you have Postfix configured to forward email out to the Internet (unlikely), then using mail will not work to send email to external email addresses.

If you want to send “Internet email”, then you probably want a program like msmtp, nbsmtp, ssmtp, or sendemail. I have not used any of those, but they are all available through MacPorts. To use one of those programs, you generally have to tell it about your SMTP server (e.g. the one you might have configured in Mail.app, including any required username or password) and then feed it the mail message. The actual command line options will vary depending on which SMTP client you choose.

On the other hand, if you really do want to send local email to another user account on the same system (i.e. the other user has an Mail User Agent that can handle fetching mail from the local mail spool (e.g. mail)), then you could use mail. Note that Mail.app can import the mbox format mail spool file, but it can not generally be configured to regularly fetch mail from the local mail spool.

All I want it to do is send a confirmation message. I hope my information helps. Like, “Your conversion has been successfully completed.” For, let’s say, a timed converter, so you look at your phone’s email and find that your conversion is complete.

I’m working on something similar. I am using the sendEmail perl script linked above.

I placed the sendEmail script file in the resources folder of my script bundle, so it’s packaged with my main script that needs it. The file could be kept anywhere, but I like having it portable with the actual Applescript that uses it.


-- Uses sendEmail perl module - from http://www.caspian.dotconf.net/menu/Software/SendEmail/
set sendMail to POSIX path of (path to resource "sendEmail")  
set emailResult to (do shell script quoted form of sendMail & " -f senderemail@example.com -t \"Your Name <recipientemail@example.com>\" -u SUBJECT_LINE_HERE -m EMAIL_MESSAGE_HERE -s mail.OUTGOING_EMAIL_SERVER.com:25")


Fill in your particulars and you’re all set. You may need to use a different port than 25, or not use one at all, but it should be the same as how your email client is set up.

It’s great, because I no longer need to worry which email client the script’s user uses, or has open. I plan to use it to send confirmation emails with result data in it. Also, in some cases I may use it to send me a text message for more urgent errors, etc (I don’t always check my mail on my phone) - most cel carriers allow you to send an email directly as a text message.

Looks good. I hope I could improve it even more. Thanks. :smiley:

I just took a few things and… presto! I have an email sender:

property login : {name:"", password:"", smtp:""}

on run
	asklogin()
	set dialog to (display dialog "What email do you want to send your message to?" default answer "friend@example.com" buttons {"Cancel", "Reset Settings", "OK"} default button "OK" cancel button "Cancel")
	if button returned of dialog as string is "Reset Settings" then
		set login to {name:"", password:"", smtp:""}
		error number -128
	end if
	set sender to text returned of dialog
	set subject to quoted form of text returned of (display dialog "What do you want the subject to be?" default answer "")
	if subject is not "''" then set subject to " -u " & subject
	set message to quoted form of text returned of (display dialog "What is your message?" default answer "Hello. How are you doing?")
	try
		set returned to do shell script quoted form of POSIX path of (path to resource "sendEmail") & " -f \"" & (system info)'s long user name & "<" & login's name & ">" & "\" -t " & sender & subject & " -m " & message & "  -s " & login's smtp & " -o username=" & login's name & " -o password=" & (login's password)
	on error e
		display dialog e buttons {"OK"} default button 1 giving up after 30
		error number -128
	end try
	display dialog "Sent successfully:" & return & return & returned
end run

on asklogin()
	if name of login is "" then set name of login to text returned of (display dialog "What is your email address you want to send from?" buttons {"Cancel", "OK"} default button "OK" default answer "myemail@example.com" with title "Your emial")
	if password of login is "" then set password of login to text returned of (display dialog "What is your email's password?" buttons {"OK"} default button 1 default answer "mypass132word" with title "Your password")
	if smtp of login is "" then set smtp of login to text returned of (display dialog "What is your email's SMTP server address? (SMTP ONLY)" & return & "Example: gmailaddress@gmail.com = smtp.gmail.com" buttons {"OK"} default button 1 default answer "smtp.example.com" with title "SMTP Server address")
end asklogin

How nice!

I think yester years hassles with setting up sendmail is gone. If my memories are right postfix ships at least with Leopard+ almost preconfigured out of the box As do a caching DNS server … :D.
But there are still many concerns setting up your own mail server, lik the vulnerability of being exploited by spammers.
In summa it cost’s some time, work and study to get a proper solution.
( - But it took only about an hour getting back my caching dns-server :smiley: )

If the mail is only to yourself and not to external clients, I believe there are ways to make Mail.app interact with
sendmail/postfix pretty easily.

Still, it struck me while reading this thread how it easy it could be to execute an osascript which takes parameters and passes them to the run handler of the script. The script then just addressess Mail.app, creates mail message and sends mail through the mail account of your scripted choice without further ado.
Then you would have wysiwyg configuration of your system, so you know where to look when something breaks.
Escpecially if you have a proper way to send error messages to the console, or
use growl notify.

Best regards

McUsr

We are drowning in information but starved for knowledge.
– John Naisbitt, Megatrends