Send a mail with shell script

Hi everyone.
Thx for your help in many times. I’ve make my first donation to Macscripter. I’n not (already) a true boss of AS, so this is the only way to help community.

So, here you are :slight_smile:
I’m away from my Mac for many times, so I think it can be intesting that it mail me everytime someone turn it on but, this is the key, without any interface. The only way that I think is to make a simple shell command in a AppleScript budle application that mail me something like… “Hello” to my mail.
But it seems to be not so easy.

Does someone know a shell command to do something like…

sendmail & " -mysmtp" & " ‘Hello’"

Thx for help in advance


Try something like:

set messTo to ""
set messFrom to ""
set messSub to "Message subject"
set messBody to "message" & return & "body"

do shell script "echo " & (quoted form of "" & messBody) & " | mail -s " & (quoted form of messSub) & " " & messTo & " -f " & messFrom

John M

That incredible!!! It works!

Thanks a lot.

Only a questions: it doesn’t ask to me my the SMTP… I’m courious: how can it decide what’s the right SMTP?


I have noted that certain characters generate an error if they are in the message body when you execute the above shell script. You should parse out or remove the offending characters. Suggested subroutine to do this is set out below.

That said, there may be a much cleverer shell script way to allow the offending characters through without generating an error and without the need to parse them out.

to parseout(stringtoparse)
	set illegals to "<>|&()`'" & (ASCII character of 47)
	repeat with i from 1 to count (illegals)
		set testletter to (text i thru i of illegals)
		set the_offset to 1
			set the_offset to offset of testletter in stringtoparse
			--display dialog the_offset as text
			if the_offset > 0 then
				if the_offset = length of stringtoparse then
					set stringtoparse to (text 1 thru (the_offset - 1) of stringtoparse)
				else if the_offset = 1 then
					set stringtoparse to (text (the_offset + 1) thru -1 of stringtoparse)
					set stringtoparse to (text 1 thru (the_offset - 1) of stringtoparse) & "" & (text (the_offset + 1) thru -1 of stringtoparse)
				end if
				exit repeat
			end if
		end repeat
	end repeat
	return stringtoparse
end parseout

I think, this is actually not necessary.
John’s script has a little quoting error, this cannot work

(quoted form of "" & messBody)

it quotes the empty string and leaves messBody as it is

this should consider also the “illegal” characters

do shell script "echo " & (quoted form of messBody) & " | mail -s " & (quoted form of messSub) & " " & messTo & " -f " & messFrom

Your computer has a mail server (I believe it’s postfix on 10.5 and maybe 10.4 too) that unix mail uses. The default configuration is to only allow email sent to local accounts on your computer. If you try to send email to someone else it won’t work. For that to work you have to set up the mail server, and that’s where you would enter your smtp address.