Sending emails from an AppleScript - scripting Apple Mail best option?

Dear MacScripters,

I have AppleScripts that collect data from Weather Station software and send automated emails on weather conditions. Since OS-X has the Mail application, I started by scripting that to do send the messages. However, this is overkill for the messages I’m sending and is inconvenient for folks who don’t normally use Mail.

For my needs, something like Perl’s Net::SMTP module is a better match for my needs. However, there doesn’t seem to be any AppleScript equivalents. The only AppleScript add-ons for doing email like the XMAIL OSAX extension seem hopelessly abandoned.

Is there something else beside Apple Mail that can be used for my needs, or is that the best mousetrap for AppleScripters these days?

Thanks in advance for your replies! :slight_smile:

Cheers, Edouard :slight_smile:

Unix has a mail client appropriately called mail through which you could send emails via "do shell script “mail etc.” See it’s man page – it’s not too horrendous. (I confess that I’ve never used it – I use mail. To avoid problems with rich text for simple messages, I set it to "Use Plain Text. Under the “Composing” toolbar button of Mail’s preferences there’s a Message Format drop down that defaults to rich text, but you can set Plain Text.

Dear Adam and MacScripters,

Well there’s the first problem right there. A name like that makes sense - could it possibly work? :o

Actually that would work perfectly well for me, but I assume that requires the OS-X machine to have proper configuration as a mail server. Most OS-X machines aren’t set up to actually send mail to the outside world, that requires a sendmail, postfix, or other suitable SMTP server and these days, require cooperation of the ISP that the Mac is connected to.

As I understand it. Most ISP SMTP servers require some sort of authentication to prevent spamming. So I would need an SMTP client that can deal with such things.

Ironically, there is a command line tool for Mac that supposedly can do this sort of thing. It is called mtcmail and is a freeware from MacTechnologies Consulting.. It claims it can do all these things, but doesn’t even come with a readme file. For something that can supposedly even access the keychain - what do these folks expect me to do - guess? :rolleyes:

It is more than I can do to write such a thing in AppleScript or Objective-C. So I was hoping someone had run into the same brick wall, but had managed to find a way around it somehow.

Thanks for your help anyway. Apple Mail still gets the job done.

Cheers, Edouard :slight_smile:

In the terminal, run the executable with --help appended and you’ll get this:

Dear Adam and MacScripters,

Thanks for sharing that. After having some dinner, I also decided to try running mtcmail in the hope there was a help system there. So I found the help info as you describe. It is just that given the amount of hacking these days - that isn’t my preferred way to find instructions!!

Yes, it does look like a promising way to send email from an AppleScript. I’ll put it through its paces and see if I can make it easy enough to use for the weather software community I’m supporting.

Thanks for your help! :slight_smile:

Cheers, Edouard

Hi Edouard

Did you ever get this to work? I am trying to so something similar?

Thanks

Dear kiwilegal and MacScripters,

Yes it does work and is reasonably convenient. You need to create a temporary file with the message text so that MtcMail can access it.

Here is the method I created to do this. It clearly depends on other routines that I can send to you if curious, but this is the part that actually uses MTCmail.

on MTCMailmessage(scriptName, emailSubject, messageText)
	-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	-- Subroutine to use the MTCmail program to send out an email.  
	-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	global fromAddress, toAddress, smtpServer, smtpPort, emailLogin, emailAuthentication
	local errText, errNum, commandResult, isError
	set errText to "Error message not available" -- *** Attempt to avoid undefined values from AppleScript interface ***
	set errNum to "No Number"
	set commandResult to ""
	set isError to false
	
	-- Get path to folder containing application.  Support files are expected to be in same folder.
	set appHomeFolder to getContainingFolder()
	
	-- Generate a temporary filename to hold email text
	set emailFile to generateMailFile(scriptName)
	
	-- Create complete UNIX path to data state directory for WeatherCat scripts.
	set emailFilePath to POSIX path of (appHomeFolder & "WeatherCat Script Library:WeatherCat Script Data Backups:" & emailFile)
	
	writeEmailText(scriptName, messageText, emailFilePath)
	
	set mtcmailPath to POSIX path of appHomeFolder & "mtcmail/mtcmail"
	
	-- /mtcmail -f 'lwcadmin@canebas.org' -t 'elagache@mac.com' -s 'test' -m 'smtp.comcast.net' -p 587 -U elagache -K -C -X tlsv1 < usage_help.txt 
	
	set mtcmailCommand to "'" & mtcmailPath & "'" & " -f '" & fromAddress & "' -t '" & toAddress & "' -s '" & emailSubject & "' -m '" & smtpServer & "' -p " & smtpPort
	
	if (emailAuthentication ≠ "") then
		set mtcmailCommand to mtcmailCommand & " -U '" & emailLogin & "' -K -X " & emailAuthentication
	end if
	
	set mtcmailCommand to mtcmailCommand & " -C  -w 0 -F '" & emailFilePath & "'"
	
	try
		set commandResult to do shell script (mtcmailCommand)
	on error errText number errNum -- If error obtain text and error number for logging.
		set messageText to "Error in sending MtcMail: " & errText & " (" & errNum & ")"
		set isError to true
		logEvent(scriptName, messageText)
	end try
	
	if (not ("successfully" is in commandResult)) then
		set messageText to "Error in sending MtcMail: " & commandResult
		logEvent(scriptName, messageText)
	end if
	
	try
		do shell script ("rm '" & emailFilePath & "'")
	end try
	
	if (isError) then
		return (errText)
	else
		return (commandResult)
	end if
end MTCMailmessage

If it would be useful for you to have the whole thing, send me PM and I’ll email the library file to you.

Cheers, Edouard :slight_smile:

Hi Edouard

Thanks for all this. I am almost sorted on the problem but using sendEmail. Much assisted by McUsr on another post: http://macscripter.net/viewtopic.php?pid=154596#p154596

Just stuck on converting text to something that Windows will recognise.

Cheers