Skip to content

Instantly share code, notes, and snippets.

@youandhubris
Last active May 17, 2024 01:40
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
@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