How do I get the email body only (no headers) in Mail.app

I’m working on the Mail portion of a little Text-to-Speech AppleScript app and I need to strip the headers out of the message body before speaking the email. Does anyone know how to get just the body content of an email from Mail.app?

I’m using this right now:

set theBody to (source of oneMessage as text)

and it gives me full headers along with the message body.

Thanks,
–Alex

If you select at least one message in Mail, does this do what you want?

tell application "Mail"
	set selectedMessages to selection
	set theBody to (content of item 1 of selectedMessages)
end tell

“content” works great for non-HTML messages or HTML message that include a text-only version. I was hoping to support HTML only messages (HTML stripped) as well.

This will give you the entire source of the e-mail including all headers and HTML tags:

tell application "Mail"
	set selectedMessages to selection
	set theBody to (source of item 1 of selectedMessages as text)
	
	say theBody with default_voice and waiting until completion
	
end tell

This will give you the text content of the e-mail without the headers and HTML tags:

tell application "Mail"
	set selectedMessages to selection
	set theBody to (content of item 1 of selectedMessages as text)
	
	say theBody with default_voice and waiting until completion
	
end tell

If there is a “remove headers” script floating around out there, I’d love to use it.

Ok, with thanks to Andreas Amann I’ve got this working:

tell application "Mail"
   set selectedMessages to selection
   set theBody to (source of item 1 of selectedMessages as text)

   set theBodyList to {}
   set AppleScript's text item delimiters to {"

"}
   set theBodyList to text items 2 thru (number of text items in theBody) of theBody
   set AppleScript's text item delimiters to ""
   set theBody to ""
   repeat with snippet in theBodyList
      set theBody to theBody & snippet
   end repeat
   
   say theBody with default_voice and waiting until completion
   
end tell

i have a similar question … but related to text-based attachments. for some reason i don’t understand, if attachments are ‘text’, mail.app displays them within the body of the email rather than as attachments. i would like to write an applescript to save the text attachment part to a file (maybe default it to '~/mail_attachments).

here is the full ‘raw source’ of the ‘header/body’ info … would anyone know how i could script this up and use it within mail.app as a ‘rule’?

(the email body text is first … the text attachment is second … it’s different fromt he first as it has the ‘Content-Description’ and ‘name=‘file’’ descriptors … i’d like to be able to preserve the file name if possible)

many thanks!

–Singular_of_Boars_243_000
Content-Type: TEXT/plain; charset=us-ascii
Content-MD5: c+e5pmCsS2k8iHKeP30fBQ==

I will look at it. Here it is.

–Singular_of_Boars_243_000
Content-Type: TEXT/plain; name=“the_file_i_want.txt”; charset=us-ascii; x-unix-mode=0644
Content-Description: the_file_i_want.txt
Content-MD5: giiC/skAHQWR0V6IS5abag==
file content …
file content …
file content …

I have written an AppleScript which works either on selected messages or triggerd as a rule-action.

You can get it at the AppleScript discussions on apple.com:

:arrow: http://discussions.info.apple.com/WebX?14@216.p2J8a9walXi.755980@.3bc21887/8

From the code you can see, that it only will try to extract messages when

multipart/mixed

is the Content-Type as found in the headers (since this is a must for messages with attachments).

In order to only catch inlined text attachments, you should be able to do this by adding some more code in the form of

if "some text here" is in source of message