Skip to content

Instantly share code, notes, and snippets.

@youandhubris
Last active October 5, 2024 17:32
Show Gist options
  • Save youandhubris/9e292822e3db8f91df93234db092906e to your computer and use it in GitHub Desktop.
Save youandhubris/9e292822e3db8f91df93234db092906e to your computer and use it in GitHub Desktop.
AppleScript to send e-mail, using Apple's Mail, with multiple recipients, cc, bcc and attachments
tell application "Mail"
set theFrom to ""
set theTos to {}
set theCcs to {}
set theBccs to {}
set theSubject to ""
set theContent to ""
set theSignature to ""
set theAttachments to {}
set theDelay to 1
set theMessage to make new outgoing message with properties {sender:theFrom, subject:theSubject, content:theContent, visible:false}
tell theMessage
repeat with theTo in theTos
make new recipient at end of to recipients with properties {address:theTo}
end repeat
repeat with theCc in theCcs
make new cc recipient at end of cc recipients with properties {address:theCc}
end repeat
repeat with theBcc in theBccs
make new bcc recipient at end of bcc recipients with properties {address:theBcc}
end repeat
repeat with theAttachment in theAttachments
make new attachment with properties {file name:theAttachment as alias} at after last paragraph
delay theDelay
end repeat
end tell
# macOS 10.12+ know bug
# https://stackoverflow.com/questions/39860859/setting-message-signature-of-outgoing-message-using-applescript
# set message signature of theMessage to signature theSignature
send theMessage
end tell
@mgguinne
Copy link

Great stuff, I was looking for how to do this and this just hit the spot nicely 👍

@FloWi
Copy link

FloWi commented Aug 6, 2020

Thanks a lot for sharing your solution.
Have you found some documentation about the api of the apps that you can control via applescript?
e.g. where to find "things I can tell the mail-application" ;-)
I wasn't able to dig anything up by googling :-/

@youandhubris
Copy link
Author

On the Script Editor application, go to Window > Library and you'll find the reference for Apple's applications.
I have no idea why they decided to call it "Library" instead of Reference, but it's Apple... So... You know... : )

On said Library window, if you open Mail, the first column gives you the reference for Mail's and Mail's Framework but also some common classes and commands for all applications and text processing. For example, by selecting Mail on the first column you can then see the send command and the outgoing message class, which I used here.

Hope it helps! Have fun!

@FloWi
Copy link

FloWi commented Aug 7, 2020

Thanks a lot, @youandhubris! It is well hidden indeed :)

@walshman23
Copy link

@FloWi
Copy link

FloWi commented Aug 8, 2020

🙏

@ratpH1nk
Copy link

theDelay is in seconds? milliseconds? thanks!

@terryjw03
Copy link

I am not sure why but I encounter syntax error pointing at "make new outgoing message with properties"
The error message: Syntax Error: expected end of line, etc. but found identifier.
would you kindly help? thank you

@youandhubris
Copy link
Author

@terryjw03, can you share your edit?

@jonnyceee77
Copy link

Hi There,

Thanks for writing this script. I'm a newbie (last coding experience was Fortran in the '90s!) to AppleScript and coding.

I need all of these functionalities, but want to reference the data in a CSV. The attachments would be in a desktop folder- the name of each attachment would be in the CSV along with the other records for each email recipient. Can you show me exactly how I would reference each column in the CSV (to fill in recipient name, attachment name, etc). Other scripts I have found require me to define which column goes with which data.

I'm also trying to add a desktop folder to look in, but I keep getting -1728 errors.

Thanks for any help!

@jmklinck
Copy link

I am new to applescript. I would like to create a script that reads an address file and sends a message with an attachment to each email address individually. I have seen this done with a list in the script. The other example adds all toAddresses to a single message.

The following script works, but has all addresses on the address line. What am I doing wrong? And, thanks in advance for the help.

set theAttachment to "Macintosh HD:Users:ME:Desktop:Newsletters:Spring2021:spring2021.pdf"
-- tab separated file. FirstName \t LastName \t emailaddress
-- one entry per line
set theAdds to "Macintosh HD:Users:ME:Desktop:Emailer:NewsLetterEmails"
set theSubject to "Newsletter for Spring 2021"
set theSender to "newsletter@ME"
set theFileReference to open for access theAdds
set theFileContents to read theFileReference using delimiter return
close access theFileReference
set text item delimiters to tab
tell application "Mail"
repeat with i from 1 to count of theFileContents
set theLine to text items of item i of theFileContents
set firstname to item 1 of theLine
set lastname to item 2 of theLine
set emailadd to item 3 of theLine
set msg to "Dear " & firstname & ",

Attached is the latest Newsletter. Please send responses or suggestions or requests to

newsletter@ME

If you prefer to receive a print version of the newsletter or you wish to have the newsletter sent to a different address or you do not wish to receive future newsletters, please send a message to newsletter@ME

"
set msg to make new outgoing message with properties {visible:true, subject:theSubject, content:msg, sender:theSender}
tell msg
make new to recipient at end of to recipients with properties {address:emailadd}
end tell
tell content of msg
make new attachment with properties {file name:theAttachment as alias} at after last paragraph
end tell
delay 2
send msg
end repeat
end tell

@gh-isoar
Copy link

theDelay is in seconds? milliseconds? thanks!

Seconds. And to be clear, the delay is to provide time for attaching an attachment to complete. So, if attaching a list of attachments is unstable, trying increasing the delay.

@Timbone53
Copy link

I am totally new to Applescript and coding in general and I have question:

How can I add attachments? If I add the file path to theAttachments I get an error message telling me that the file path cannot be converted in type alias.

Thanks in advance.

@fabiofidanza
Copy link

Cheers!

@commiepinko
Copy link

commiepinko commented May 17, 2024

With macOS 10.10 "Yosemite," AppleScript enabled the ability to write handlers with optional parameters by providing default values. This eliminates the need to pass empty or zero values for parameters that aren't actually used.

Send_Mail_Message given theSubject:"Armageddon", theContent:"Now that I have your attention…"

to Send_Mail_Message given theSender:theSender : "defaultF@fromaddress.com", theRecipients:theRecipients : {"defaultF@toaddress.com"}, theCcs:theCcs : {}, theBccs:theBccs : {}, theSubject:theSubject : "", theContent:theContent : "", theSignature:theSignature : "", theAttachments:theAttachments : {}, theDelay:theDelay : 1

	tell application "Mail"

		set theMessage to make new outgoing message with properties {sender:theSender, subject:theSubject, content:theContent, visible:false}

		tell theMessage
			repeat with theTo in theRecipients
				make new recipient at end of to recipients with properties {address:theTo}
			end repeat
			repeat with theCc in theCcs
				make new cc recipient at end of cc recipients with properties {address:theCc}
			end repeat
			repeat with theBcc in theBccs
				make new bcc recipient at end of bcc recipients with properties {address:theBcc}
			end repeat
			repeat with theAttachment in theAttachments
				make new attachment with properties {file name:theAttachment as alias} at after last paragraph
				delay theDelay
			end repeat
		end tell

		if theSignature ≠ "" then
			set message signature of theMessage to signature theSignature
		end if

		send theMessage

	end tell
	
end Send_Mail_Message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment