Send email to each message selected using applescript

Hi,

I have a script which will send a single email to a number of recipients but I wanted it to send individual emails to the selected messages. I also wanted the script to extract the first name from the sender field. The problem I’ve had with that is the sender field reads something like “Joe Blogs via PayPal” I’d be happy with “Joe Blogs” if I could remove the “via PayPal”. I found a script to remove words from text

set replyName to {}
set replyNametext to ""
set replyName to "Joe Blogs via PayPal"
set haystack to replyName
set needle to " via PayPal"
set replacement to ""
search_replace(haystack, needle, replacement)
 
 
on search_replace(haystack, needle, replacement)
          set old_delimiters to AppleScript's text item delimiters
          set AppleScript's text item delimiters to needle
          set temp_list to every text item of haystack
          set AppleScript's text item delimiters to replacement
          set return_value to temp_list as text
          set AppleScript's text item delimiters to old_delimiters
          return return_value
end search_replace

But I can’t get that to work in the main script.

This is my main script:


set thesenders to {}
set thesenderstext to ""
set replyName to {}

tell application "Mail"
	set themessage to the selection
	repeat with i from 1 to the number of items in themessage
		set thesender to (extract address from (the reply to of (item i of themessage)))
		set replyName to (extract name from (the reply to of (item i of themessage)))
		
		if thesenders does not contain thesender then
			set thesenders to {thesender}
			set replyName to {replyName}
		end if
		
	end repeat
	
	
	set AppleScript's text item delimiters to ", "
	set thesenderstext to thesenders as rich text
	set AppleScript's text item delimiters to ""
	set newMessage to make new outgoing message
	set theSubject to "GBP instructions"
	set myrclipboard to the clipboard
	
	set theContent to "Dear " & replyName & ", 
 
 
Thanks so much for your email. ...
 
 
Regards ..."
          set newMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
          set theAttachment1 to POSIX file "/Users/Jim/qrcode.13177607.png"
          set theAttachment2 to POSIX file "/Users/Jim/qrcode.13177658.png"
          set theAttachment3 to POSIX file "/Users/Jim/qrcode.13177665.png"
          tell newMessage
                    set visible to true
                    make new to recipient with properties {address:thesenderstext}
                    make new attachment with properties {file name:theAttachment1} at after the last paragraph
                    make new attachment with properties {file name:theAttachment2} at after the last paragraph
                    make new attachment with properties {file name:theAttachment3} at after the last paragraph
          end tell
          set toAddress to thesenders as rich text
          tell newMessage
                    make new to recipient with properties {address:toAddress}
                    --  send
          end tell
 
          activate
end tell
 

I realise this is probably a bit of a Frankenstein monster of a script which probably contains lots of redundant features but I’m a total novice to scripts. Please could you help.

Thanks.
Jim

Model: MacBook Pro
Browser: Safari 536.30.1
Operating System: Mac OS X (10.8)

Hi,

Firstly, not sure if your second script would work. This line:

set thesenders to {thesender}

I think should be:

set end of thesenders to thesender

You trying to concatenate thesender to the end of the list (thesenders).

Just before that part you would call your replace text subroutine (the first script) to change the name of the sender.

gl,
kel

You’re trying to send one message or many depending on what’s in the message? Is there more to your second script like another repeat loop?

Sorry kel, the script I posted does work in that it sends an email with the right text. The problem is I would like the bit after "Dear " to read “Dear Joe,” rather than “Dear Joe Blogs via PayPal,”
“Joe Blogs via PayPal” being extracted from the sender field.

Granted the script I posted doesn’t do that it displays the email address at the moment. In fact the script I use at the moment pastes from the clipboard the recipients name but that means me copying manually for each email in the list.

I would also like it repeat for every email message selected. I have fudged that script together from others I got on line. I am obviously not an expert.

Thanks

Hi Jim,

There is a lot of unnecessary things in the script.

Is the replyname always in the form “Joe Blogs via PayPal” where the first two words is the name of the person? If you want to parse the name from the strings they have to have something in common (e.g. first two words, words before “via”, etc.).

Edited: and I forgot, just the first word (name)?

gl,
kel

Thanks, yes, I know there are is a lot of unnecessary bits in the code, I’m not really worried about redundant bits what I want is to be able to have many emails that reply to many people at once with their own personal email and addressed to their first name from the sender “Frist Second via PayPal”. Yes, it is always in that format.
This will save me a lot of time, or at least make the recipient feel they have had more time spent on them than I can afford.

Thanks

Then, all you need to do is get the first or the first two words:

set thesender to "Joe Blogs via PayPal"
set thesender to word 1 of thesender

or for the first two words:

set thesender to "Joe Blogs via PayPal"
tell thesender
	set thesender to (word 1 & space & word 2)
end tell

gl,
kel

If you’re talking about donations, it’s good habit to afford a little time to respond personally (my 2 ¢)

You can also extract the name with


set thesender to "Joe Blogs via PayPal"
if thesender ends with "via PayPal" then
	set thesender to text 1 thru -12 of thesender
end if

One thing though, if you always need just the first two words, then you don’t need the test.

Here’s an example

set replyName to "Joe Blogs via PayPal"
set firstName to word 1 of replyName
set lastName to word 2 of replyName
set the message to "Hello " & firstName & ",

" & "The text"

Hope it helps.

Ive been trying to get rid of the corrections in AppleScript Editor. It meeses up my scripts a lot. The last script should be:

set replyName to "Joe Blogs via PayPal"
set firstName to word 1 of replyName
set lastName to word 2 of replyName
set the_message to "Hello " & firstName & ",

" & "The text"

It doesn’t matter here though, because message is not a key word.

Just extras,

gl,
kel

Thanks again for your reply kel but my mail script doesn’t contain “Joe Blogs via Palpal” the ‘my attempt at a work around’ did for sake of example but my actual script contains a variable. As I said, I’m really busy. This isn’t just a play thing. I really do need to save time. I have way too many emails that I have to reply to to be able mess around with typing stuff. Sorry no all that was in reply to your message. I really don’t have enough time in the day to do everything and really would like to be able to give people the feeling that I have got all the time in the world. Reality bites.

Thanks kel,

You certainly gave me the tools to solve one problem. Now if only I can get it to do all 100 of the selected emails.
Anyone able to help with that? :slight_smile:
Thanks for your input.
New script:

set thesenders to {}
set thesenderstext to ""
tell application "Mail"
	set themessage to the selection
	repeat with i from 1 to the number of items in themessage
		set theSender to (extract address from (the reply to of (item i of themessage)))
		if thesenders does not contain theSender then
			set thesenders to {theSender}
		end if
		
		set replyName to (extract name from (the sender of (item i of themessage)))
		set firstName to word 1 of replyName
	end repeat
	
	
	set AppleScript's text item delimiters to ", "
	set thesenderstext to thesenders as rich text
	set AppleScript's text item delimiters to ""
	set newMessage to make new outgoing message
	set theSubject to "GBP instructions"
	set myrclipboard to the clipboard
	set theContent to "Dear " & firstName & ", 

Thanks so much for your order of email     
 
                                        
                                         
  "
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
	set theAttachment1 to POSIX file "/Users/Jim/qrcode.13177607.png"
	set theAttachment2 to POSIX file "/Users/Jim/qrcode.13177658.png"
	set theAttachment3 to POSIX file "/Users/Jim/qrcode.13177665.png"
	tell newMessage
		set visible to true
		make new to recipient with properties {address:thesenderstext}
		make new attachment with properties {file name:theAttachment1} at after the last paragraph
		make new attachment with properties {file name:theAttachment2} at after the last paragraph
		make new attachment with properties {file name:theAttachment3} at after the last paragraph
	end tell
	set toAddress to thesenders as rich text
	tell newMessage
		make new to recipient with properties {address:toAddress}
		-- send
	end tell
	
	activate
end tell

Hi there,

I tried this little bit of code in Entourage (I’m not using Mail) and it works.

tell application "Microsoft Entourage"
	activate

	set theSelection to the selection
	repeat with thisMessage in theSelection
		display dialog ID of thisMessage as text	
	end repeat

end tell

Give this a try. It’s untested I’ve just applied the repeat with loop from above:-

set thesenders to {}
set thesenderstext to ""
tell application "Mail"
        set theSelection to the selection
	repeat with theMessage in the theSelection
	repeat with i from 1 to the number of items in theMessage
		set theSender to (extract address from (the reply to of (item i of themessage)))
		if thesenders does not contain theSender then
			set thesenders to {theSender}
		end if
		
		set replyName to (extract name from (the sender of (item i of themessage)))
		set firstName to word 1 of replyName
	end repeat
	
	
	set AppleScript's text item delimiters to ", "
	set thesenderstext to thesenders as rich text
	set AppleScript's text item delimiters to ""
	set newMessage to make new outgoing message
	set theSubject to "GBP instructions"
	set myrclipboard to the clipboard
	set theContent to "Dear " & firstName & ", 

Thanks so much for your order of email     
 
                                        
                                         
  "
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
	set theAttachment1 to POSIX file "/Users/Jim/qrcode.13177607.png"
	set theAttachment2 to POSIX file "/Users/Jim/qrcode.13177658.png"
	set theAttachment3 to POSIX file "/Users/Jim/qrcode.13177665.png"
	tell newMessage
		set visible to true
		make new to recipient with properties {address:thesenderstext}
		make new attachment with properties {file name:theAttachment1} at after the last paragraph
		make new attachment with properties {file name:theAttachment2} at after the last paragraph
		make new attachment with properties {file name:theAttachment3} at after the last paragraph
	end tell
	set toAddress to thesenders as rich text
	tell newMessage
		make new to recipient with properties {address:toAddress}
		-- send
	end tell
	
	activate
	end repeat
end tell

Oh Lordy:o it works. FANTASTIC! Thanks TecNik I’d made a couple of errors when I reposted that back but here’s the working script. essentially what you posted. You’re a star. I can’t tell you how grateful I am. :smiley:

set thesenders to {}
set thesenderstext to ""
tell application "Mail"
	set theSelection to the selection
	repeat with theMessage in the theSelection
		repeat with i from 1 to the number of items in theMessage
			set theSender to (extract address from (the reply to of (item i of theMessage)))
			if thesenders does not contain theSender then
				set thesenders to {theSender}
			end if
			
			set replyName to (extract name from (the sender of (item i of theMessage)))
			set firstName to word 1 of replyName
		end repeat
		
		
		set AppleScript's text item delimiters to ", "
		set thesenderstext to thesenders as rich text
		set AppleScript's text item delimiters to ""
		set newMessage to make new outgoing message
		set theSubject to "GBP instructions"
		set myrclipboard to the clipboard
		set theContent to "Dear " & firstName & ", 

Thanks so much for your order of email     
 
                                        
                                         
  "
		set newMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
		set theAttachment1 to POSIX file "/Users/Jim/qrcode.13177607.png"
		set theAttachment2 to POSIX file "/Users/Jim/qrcode.13177658.png"
		set theAttachment3 to POSIX file "/Users/Jim/qrcode.13177665.png"
		tell newMessage
			set visible to true
			make new to recipient with properties {address:thesenderstext}
			make new attachment with properties {file name:theAttachment1} at after the last paragraph
			make new attachment with properties {file name:theAttachment2} at after the last paragraph
			make new attachment with properties {file name:theAttachment3} at after the last paragraph
		end tell
		set toAddress to thesenders as rich text
		tell newMessage
			make new to recipient with properties {address:toAddress}
			-- send
		end tell
		
		activate
	end repeat
end tell