How to send email with file attachments with Apple Script

If you use an application like Hazel for automation on your Mac, you can use it to send files automatically.

Sending a file attached to someones email address with Apple Script:

tell application "Finder"
	set theFilename to name of theFile -- get the filename
end tell

-- sender
set theSender to "from@domain.com"

-- recepient
set theReceiver to "to@domain.com"

-- subject
set theSubject to "See attached file: " & theFilename

-- multiline content
set theContent to "Hi xyz, 

attached to this mail, you can find: 
" & theFilename & "

Best regards
abc

"

tell application "Mail"
	set newMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:theContent}
	tell newMessage
		
		set visible to false
		set sender to theSender
		make new to recipient at end of to recipients with properties {address:theReceiver}
		try
			make new attachment with properties {file name:theFile as alias} at after the last paragraph
		on error errmess
			log errmess
		end try
		delay 1
		-- 'send' -> send mail
		-- 'save' -> save mail as draft
		send
	end tell
end tell

This sends one email for every file in Hazel that matches. If Hazel matches 3 files, 3 emails are sent.