Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'An AppleScript to batch-redirect email in Mail.app' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to batch-redirect email in Mail.app
Authored by: leonstafford on Sep 06, '11 02:59:14AM

I was also in the need for uploading about 175,000 emails into Gmail (upgraded to 80GB plan ;)

The scripts above no longer worked in Lion, so I mashed together a couple of scripts to get what I have been successfully using below.

I also added a utility to automatically add a set subject text for empty subject emails, else they would be waiting for user response to confirm sending emails with an empty title (annoying).

One thing also to be aware of is that you need a decent SMTP server for sending all these emails to Gmail with. Most providers, even MobileMe, have daily/hourly send limits, which are too small to be usable for such Gmail migration purposes.

I used an email account hosted on some MediaTemple, (dv) plan, web hosting, which has no daily send limits. This was the final key for me to get this all to work.

Any questions, please feel free to contact me.

OK, here is the code:

tell application "Mail"
	set AppleScript's text item delimiters to ""
	set theRedirectRecipient to "YOURGMAILADDRESSHERE@gmail.com"
	
	set theRedirectSender to "YOUR NAME HERE "
	
	
	set theMessages to the selection
	repeat with thisMessage in theMessages
		
		
		tell application "Mail"
			
			set theRedirectedEmail to redirect thisMessage with opening window
			tell theRedirectedEmail
				
				
				
				if subject of thisMessage is "" then
					
					set subject of theRedirectedEmail to "WAS EMPTY SUBJECT"
				end if
				
				
				
				make new to recipient at beginning of to recipients with properties {address:theRedirectRecipient}
				delete bcc recipients
				delete cc recipients
			end tell
			set the sender of theRedirectedEmail to theRedirectSender
			#delay 1
			send theRedirectedEmail
			
		end tell
		
	end repeat
end tell


[ Reply to This | # ]
An AppleScript to batch-redirect email in Mail.app
Authored by: ekepenne on Jan 08, '12 04:11:16AM
Hello everybody

Thanks for the tips regarding the forwarding/redirecting of emails.

I'm trying to forward specific incoming emails to my Evernote account. But before I forward the message, I wanted to add the date of the email and the desired Evernote-Notebook and Evernote-Tags to the subject. These added parameters are based on the name of the Mail rule.

Example:
- Initial subject is: This is the subject
- Email is dated: 7 January 2012
- Rule name is: @Notebook1 #Tag1 #Tag2

The new subject of the forwarded email will be:
This is the subject [7 January 2012] @Notebook1 #Tag1 #Tag2

In Evernote the message title will look like this: This is the subject [07.01.2012]
and the message will automatically be moved to Notebook1 and tagged with Tag1 and Tag2.

I used the code listed below and most of it works fine, but I have a strange issue.
The sender and subject are set correctly, but the message body and attachments are duplicated.
If I remove the line set subject of theRedirectedEmail to NewSubject, the message body and attachments is correct.
If I have 2 identical lines with this command set subject of theRedirectedEmail to NewSubject, the message body is tripled. It seems that I get a duplicated body/attachment for each "set" statement I add

Does anyone have any clue why I get this behavior?

Thank you ver much.
Regards, Eric

PS: My Mac runs OSX Lion 10.7.2.


on perform_mail_action(ruleData)
	
	-- Set parameters
	set AppleScript's text item delimiters to ""
	set theRedirectRecipient to "Evernote <MyEmailAddress@m.evernote.com>"
		
	-- Set the Notebook and tags of Evernote based on the name of the rule in Mail
	set NotebookTags to name of |Rule| of ruleData as text
	
	-- Get incoming messages that match the rule
	set selectedMessages to |SelectedMessages| of ruleData
	
	tell application "Mail"
		
		repeat with thisMessage in selectedMessages
			
			-- Get the date the message was sent
			set {year:y, month:m, day:d, hours:h, minutes:min} to thisMessage's date sent
			
			-- Prepare a date stamp " [dd.mm.yyyy] " for further use
			set dateStamp to (" [" & my pad(d) & "." & my pad(m as integer) & "." & y & "] ")
			set NewSubject to subject of thisMessage & dateStamp & NotebookTags
			
			set theRedirectedEmail to redirect thisMessage with opening window
			
			tell theRedirectedEmail
				make new to recipient at beginning of to recipients with properties {address:theRedirectRecipient}
				delete bcc recipients
				delete cc recipients
				set subject of theRedirectedEmail to NewSubject
			end tell
			
			send theRedirectedEmail
			
		end repeat
		
	end tell
	
end perform_mail_action

-- Adds leading zeros to date components
on pad(n)
	return text -2 thru -1 of ("00" & n)
end pad


[ Reply to This | # ]