Skip to content

Instantly share code, notes, and snippets.

@youandhubris
Last active December 19, 2023 18:58
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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
@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!

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