Can someone help me figure out how to add a custom header to already-received messages in Mail.app? Something like “X-My-Header: Some Content”. I’ve played around a bit and can’t seem to figure it out. Thanks!
Are you wanting to modify the subject line or the contents of the mail message itself? Are you interested in creating some kind of rule to process all mail messages, or just certain pieces that fit a specfic criterion?
Neither the subject nor the message body, actually. Rather, I want to add another header (like To, Cc, Subject, and various others) with my own custom content. I know this can be done, as some plug-ins (like the latest prerelease of MailTags) do it. The question is how.
I’d like to apply it to messages I select myself, though often it will be applied to all messages in a given folder.
Hi Schvenk,
You can set custom headers in Mail’s preferences under the viewing tab. See the popup menu. You can switch between settings using Mail’s application property ‘header detail’. To switch to custom, something like this:
set header detail to custom
I always leave my setting at default. You might want to store the current setting first, then when you’re done restore the setting to what it was.
gl,
kel: Thanks…but that only allows me to view headers that already exist for the message, doesn’t it? I am looking for a way to add a header to the message itself.
Hi Schvenk,
I managed to add a custom header to an incoming mail. I suppose you would want to do this to an outgoing mail, right? I think you could probably do the same thing to an outgoing mail.
Here’s how it was done. Firstly, I have a different version of Mail, so things might be a little different. In this path:
“Macintosh HD:Users:kel:Library:Mail:POP-kel@my.pop.thingy:INBOX.mbox:mbox”
INBOX.mbox is a package. If you open up the package, the file “mbox” contains the messages in your inbox. Earlier, I had added a custom header, using Mail’s preferences as previously posted, and named it X-MyHeader. Now, I duplicated the file “mbox” to the desktop. After dropping it on TextEdit, I looked for my test email message in the text and added the header on its own line:
X-MyHeader: some text here
I then ran Mail and the test message contained the custom header.
Now we need to look if there’s a similar file for outgoing email or maybe one in the Drafts mailbox.
Edited: Important, almost forgot to say that the package INBOX.mbox wouldn’t take dropped files, so I ran the following script to move the modified file there.
set f to choose file
set mbox to “Macintosh HD:Users:kel:Library:Mail:POP-kel@my.pop.thingy:INBOX.mbox:”
tell application “Finder” to move f to mbox
Save the script for later use to swap back the original “mbox” file.
gl,
It didn’t work. I added the custom header to the first message in the drafts mailbox. Had to switch to inbox and back to drafts to update the message. Looked at the headers and the custom header was there. Sent the message. It came back without the custom header when I retrieved it. Don’t know why it’s not working. Here’s how I changed the header. Remember that I’m using a different version of Mail and OSX, so you probably have to update the script. I think that ‘drafts mailbox’ was changed to something else. Look in Mail’s dictionary. Also, the reference to the file might have changed location. Keep a duplicate of the old file. Here’s the script thatt didn’t work if you want to look at it.
tell application "Mail"
set a to first account
set d to account directory of a
end tell
set mbox_ref to ¬
(((d as POSIX file) as string) & ":" & "Drafts.mbox:mbox") as alias
-- get text and modify header of first message
set old_h to read mbox_ref
if old_h is "" then return
set my_header to "X-MyHeader: hello"
set lf to ASCII character 10
set last_header to "X-Apple-Mail-Signature:"
set user_tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {last_header}
set temp_list to text items of old_h
set item 1 of temp_list to ((item 1 of temp_list) & my_header & lf)
set new_h to temp_list as string
set AppleScript's text item delimiters to user_tid
-- write modified text to file
set ref_num to (open for access mbox_ref with write permission)
try
set eof ref_num to 0
write new_h to ref_num
close access ref_num
on error err_mess
close access ref_num
display dialog err_mess buttons {"OK"} default button "OK"
return false
end try
-- update drafts mailbox before sending? tried it
-- send doesn't work; booooo
-- why? don't know
I wonder if Mail just looks at default headers when sending. Or maybe I’m modifying the wrong file. Anyway, if I come up with something, I’ll write back. Need to do more research.
gl,
Hi Schvenk,
I just discovered something. When composing a new message, Mail creates a file named something like “incoming_mail” in the drafts mailbox. Maybe if you modify the header of that message … Probably won’t work, because there isn’t the same file when sending a saved message. Darn!
gl,
Hi,
Update: sent a custom header sent from Netscape and retrieved through Mail. I think there’s something else that needs to be done with Mail to add the header. Still searching.
gl,
Hi Schvenk,
Success. I’m simplifying the script and trying to find what the default headers are.
Later,
Hi Schvenk,
This will take longer than I thought it would. Parsing dictionaries with ‘do shell script’ ‘defaults read’ is a bit complex, because it returns string values without quotes. Anyway, here’s an example that gets an X-MyHeader to work in Mail.
set d to “‘{ "X-MyHeader" = "hello"; }’;”
set c to "defaults write com.apple.mail UserHeaders " & d
do shell script c
You probably don’t have a UserHeaders key in the plist, but as a warning, this overwrites the UserHeaders dictionary. You can check if it exists with:
do shell script “defaults read com.apple.mail UserHeaders”
or do it in Terminal. It will error if there is no UserHeaders key. For more info on ‘defaults’, see man defaults in Terminal.
gl,
kel: Thank you so much for all the effort! I wanted to add that it’s not important (at least for my purposes) to apply headers to outgoing mail, incoming mail, or mail stored locally. (Well, it is for incoming but I’m hoping to find a way to do that server-side.) The primary focus is on adding headers to mail already received by the IMAP server. So actually, if you come across an easy way to do it in an app other than Mail that’s fine too - since I’m modifying messages on the IMAP server Mail should pick up the changes.
Hi Schvenk,
I don’t know about server side stuff, but it seems to me that you wouldn’t want to use AppleScript.
gl,