Send Mail from AppleScript

I once figured out the various ways to send mail using the *nix mail and sendmail commands. Then I lost that information, so after fooling around for three hours, here is what appears to me to be the best solution:

on mailAway(theReply) -- uses mail, not sendmail
   local MailSubject, MailBody, shellScript, theResult
      set MailSubject to quoted form of ("Report from " & (computer name of (system info)))
      set MailBody to quoted form of ("Script Results" & linefeed & theReply)
      set shellScript to ("echo " & MailBody & " | mail -s " & MailSubject & ¬
          space & kSendTo) as string
      set theResult to (do shell script (shellScript))
   on error errmsg number errNum
      set theResult to ("Error " & errNum & space & errmsg)
   end try
   return theResult
end mailAway

The best resource I found was this Mac OS X Hints page.

To get the results mailed to you, use:

mail -s 'The Mail Log' < /var/log/mail.log
google_pluslinkedingoogle_pluslinkedinby feather

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.