Saving email messages

A while ago, there was a thread regarding how to save email messages.

Since this is something I actually wind up needed on a pretty regular basis (I don’t like to use Mail.app or my IMAP server as a reference storage; I prefer to save the email data in files in the applicable folder for the topic), I decided to throw together a script that will handle what I need.

Below is some AppleScript which do what I need, which is:
For each selected message in Mail.app:
If the message has no attachments, the message content is stored in a file on the Desktop. The filename is the subject of the message with extension “.txt”. Any characters that mess up the Finder (eg spaces, colon, and slash) are replaced with underscores.
If the message has attachments, a folder is created on the Desktop with a name similarly derived from the message subject, and in that folder the message content is saved as per above and each attachment is written in to a file in that folder as well.

I attached the script to a hotkey in Keyboard Maestro to be active only when Mail.app is the active application.

You are welcome to use/modify as you see fit, and hopefully pass on back to me any suggestions or modifications in case they are something I might want to use.

I may look to add in saving the message content in PDF format as well…depending on whether I wind up needing that or not.


set desktopPath to (path to desktop) as string

tell application "Mail"
	
	set selectedMessages to the selection
	
	repeat with thisMessage in selectedMessages
		
		-- Get data from thisMessage
		set messageText to the content of thisMessage
		set messageSubject to the subject of thisMessage
		set theAttachments to the mail attachments of thisMessage
		
		-- Parse the date into a useful format for filenames and folders
		set messageDate to the date sent of thisMessage
		set y to the year of messageDate
		set m to the month of messageDate
		set d to the day of messageDate
		set _t to (y * 10000 + m * 100 + d) as string
		set messageDateString to rich text 1 thru 4 of _t & "." & rich text 5 thru 8 of _t
		
		-- Fix special characters to create the base name for files and folders
		set fixedFileName to do shell script "echo " & (quoted form of messageSubject) & " | sed -e 's![ /:]!_!g'"
		set baseName to messageDateString & "-" & fixedFileName
		
		-- Create a folder for the files if there are attachments
		set outputFolder to desktopPath
		if (the count of theAttachments) is greater than 0 then -- Create subfolder for output
			tell application "Finder" to set outputFolder to make new folder at desktopPath with properties {name:baseName}
			set outputFolder to outputFolder as string
		end if
		
		-- Write the content of the message to a file in the output folder
		try
			set theFile to (outputFolder as string) & baseName & ".txt"
			set openFile to open for access file theFile with write permission
			write messageText to openFile
			close access openFile
		on error
			try
				close access openFile
			end try
			return 0
		end try
		
		-- Save attachments	
		if (the count of theAttachments) is greater than 0 then
			repeat with thisAttachment in theAttachments
				set attachmentName to the name of thisAttachment
				set thisAttachmentFile to (outputFolder as string) & attachmentName
				save thisAttachment in file thisAttachmentFile
			end repeat
		end if
		
	end repeat
	
end tell
1 Like

Thanks for the script!

Just one thing, can you change “as stringh” to “as string” ?
(saves others the compile error :slight_smile:)

I popped in and fixed it to save time :slight_smile: