Sending mail with Mail, full function example and regex question

Hello everybody!

I just registered here after coming across MacScripter more than once while googling applescript issues I was having, and this site looked like the real deal, but because I couldn’t post right away I solved most of my own questions in the meantime. I’m not new to applescript, but only recently starting to “get it” if you know what I mean.

My only remaining question is how do I best, or at all, use regex in applescript to, for instance, check for a valid email address syntax :slight_smile:

Anyway, I was making a standard function to automate sending mail with Mail, I commented it with an example. There’s also some lengthy noob-type explanation in there, because I tend to keep overlooking the same simple facts sometimes, but I thought I’d just post it like this as my initial contribution, and because there are probably more people like me out there who might get started better with some full fledged examples like this.

Use it anyway you like, and if someone has tips or comments, or would like to dis my coding style, that would be cool :cool:



-- sends an email message using "Mail"
-- written and tested on Mac OS X 10.5.8, Mail 3.6, script Editor 2.2.1
-- to do: set signature, check email addresses for validity



-- example input values

-- string values
-- note: leave empty using: ""
-- note: email_sender (the "From:" header) needs to be the address of an existing account in your mail application (like: myname@mydomain.com), if you put anything else it will use your main account
set email_sender to "myaccount@myprovider.com"
set email_subject to "The subject line of the message!"
set email_content to "Dear reader, although with these example settings this message will probably not get very far, chances are it will still get read by a few folks."

-- string list values
-- note: leave empty using: {}
-- note: when any attachments are listed here they are automatically spaced a few linebreaks below the message text
set email_attachment_list to {"My Harddisk:users:Me:Documents:The family weekend:image_123.jpg", "My Harddisk:users:Me:Documents:The family weekend:image_234.jpg"}

-- string/record list values
-- note: leave empty using: {}
-- note: you can leave out the recipient_name for a record, and you can mix plain values with records in the list as shown in the examples
-- note: empty or faulty address entries will be discarded and not make it into the actual email that is sent
set email_to_recipient_list to {"myfriend@hisprovider.com", {recipient_name:"John Doe", recipient_address:"john.doe@doeworld.com"}}
set email_cc_recipient_list to {"myfriendsgirlfriend@hisprovider.com", "myaunt@thefamilysite.com"}
set email_bcc_recipient_list to {{recipient_name:"Miss Mine", recipient_address:"mygirlfriend@socialsite.net"}, {recipient_name:"Miss Tery", recipient_address:"mygirlfriendsgirlfriend@socialsite.net"}}



-- example use, with parameters from example input values as shown above
fn_mail(email_sender, email_subject, email_content, email_attachment_list, email_to_recipient_list, email_cc_recipient_list, email_bcc_recipient_list)


-- function version 1
-- note: running this script with the example values will discard the example attachments, as they probably do not exist on your machine
-- note: if you want to check this without actually sending the message, find "-- send it off" below and put two dashes "--" in front of "send email_message" to disable sending (like this: "--send email_message") so you can test with adresses and attachments without spamming people during the process
-- note: find "-- create message" below and set visible to false at the end of the "set email_message to" command to have mail send the message in the background without showing the message window
on fn_mail(email_sender, email_subject, email_content, email_attachment_list, email_to_recipient_list, email_cc_recipient_list, email_bcc_recipient_list)
	
	
	tell application "Mail"
		
		
		-- prepare input
		try
			
			-- strings
			set email_sender to email_sender as string
			set email_subject to email_subject as string
			set email_content to email_content as string
			
			-- lists
			set email_attachment_list to email_attachment_list as list
			set email_to_recipient_list to email_to_recipient_list as list
			set email_cc_recipient_list to email_cc_recipient_list as list
			set email_bcc_recipient_list to email_bcc_recipient_list as list
			
		on error
			
			return false
			
		end try
		
		
		-- check attachment list
		set new_email_attachment_list to {}
		
		repeat with email_attachment in email_attachment_list
			
			-- do not keep empty items
			if length of (email_attachment as string) > 0 then
				
				-- keep only existing files
				try
					
					set email_attachment to email_attachment as alias
					
					set end of new_email_attachment_list to email_attachment
					
				end try
				
			end if
			
		end repeat
		
		set email_attachment_list to new_email_attachment_list
		
		
		--adjust content for inline attachments
		if length of email_attachment_list is not 0 then
			
			set email_content to email_content & return & return as string
			
		end if
		
		
		-- check recipient lists
		set all_email_recipient_lists to {email_to_recipient_list, email_cc_recipient_list, email_bcc_recipient_list}
		set new_all_email_recipient_lists to {}
		
		-- loop through lists
		repeat with email_recipient_list in all_email_recipient_lists
			
			
			set new_email_recipient_list to {}
			
			-- loop through list entries
			repeat with email_recipient in email_recipient_list
				
				
				set new_email_recipient to {}
				
				
				-- set checkpoints
				set recipient_name_is_valid to false
				set recipient_address_is_valid to false
				
				
				-- check for recipient_name as record property
				try
					
					set new_email_recipient_name to email_recipient's recipient_name as string
					
					set new_email_recipient to new_email_recipient & {recipient_name:new_email_recipient_name}
					
					set recipient_name_is_valid to true
					
				on error
					
					-- no usable name record, default to empty name record
					set new_email_recipient to new_email_recipient & {recipient_name:""}
					
					set recipient_name_is_valid to false
					
				end try
				
				
				-- check for recipient_address as record property
				try
					
					set new_email_recipient_address to email_recipient's recipient_address as string
					
					set recipient_address_is_valid to true
					
				on error
					
					-- no address!
					-- note: if the list value check below succeeds, this will be set back to true
					set recipient_address_is_valid to false
					
				end try
				
				
				-- check for recipient_address as list value when no usable record values for name and address are found
				if not recipient_name_is_valid and not recipient_address_is_valid then
					
					try
						
						set new_email_recipient_address to email_recipient as string
						
						set recipient_address_is_valid to true
						
					on error
						
						-- no address!
						set recipient_address_is_valid to false
						
					end try
					
				end if
				
				
				-- see if it's not empty
				-- note: ideally this should be a check for a valid email address
				if not length of new_email_recipient_address > 0 then
					
					-- no address!
					set recipient_address_is_valid to false
					
				end if
				
				
				-- keep it
				if recipient_address_is_valid then
					
					set new_email_recipient to new_email_recipient & {recipient_address:new_email_recipient_address}
					
					set end of new_email_recipient_list to new_email_recipient
					
				end if
				
				
			end repeat
			
			set end of new_all_email_recipient_lists to new_email_recipient_list
			
			
		end repeat
		
		set all_email_recipient_lists to new_all_email_recipient_lists
		
		set email_to_recipient_list to item 1 of all_email_recipient_lists
		set email_cc_recipient_list to item 2 of all_email_recipient_lists
		set email_bcc_recipient_list to item 3 of all_email_recipient_lists
		
		
		-- create message
		-- note: signature & id property not used here
		-- note: visible property determines if the message window is shown in the application
		set email_message to make outgoing message with properties {sender:email_sender, subject:email_subject, content:email_content, visible:true}
		
		
		-- set attachments inline
		tell email_message
			
			repeat with email_attachment in email_attachment_list
				
				--set email_attachment to email_attachment as alias
				
				make new attachment with properties {file name:email_attachment} at after the last word of the last paragraph
				
			end repeat
			
		end tell
		
		
		-- set "to" recipients
		tell email_message
			
			repeat with email_to_recipient in email_to_recipient_list
				
				make new to recipient at end of to recipients with properties {name:email_to_recipient's recipient_name, address:email_to_recipient's recipient_address}
				
			end repeat
			
		end tell
		
		
		-- set "cc" recipients
		tell email_message
			
			repeat with email_cc_recipient in email_cc_recipient_list
				
				make new cc recipient at end of cc recipients with properties {name:email_cc_recipient's recipient_name, address:email_cc_recipient's recipient_address}
				
			end repeat
			
		end tell
		
		
		-- set "bcc" recipients
		tell email_message
			
			repeat with email_bcc_recipient in email_bcc_recipient_list
				
				make new bcc recipient at end of bcc recipients with properties {name:email_bcc_recipient's recipient_name, address:email_bcc_recipient's recipient_address}
				
			end repeat
			
		end tell
		
		
		-- send it off
		send email_message
		
		
	end tell
	
	
end fn_mail