AppleScript - extract email address from custom mail header

Hello -


I use a modified version of an AppleScript that I found on the net to respond to incoming messages that contain either a "Disposition-Notification-To" header or "Return-Receipt-To" header. For some senders, the email address content for these custom headers doesn't match the address in the From header or Reply-To header.


How do I extract the sender and address from a custom header?


Thanks!

Ashok


P.S. My current script is pasted below ...



using terms from application "Mail"

on perform mail action with messagestheMessagesfor ruletheRule

try

repeat with theMessage in theMessages

if the source of theMessage contains "Disposition-Notification-To" or the source of theMessage contains "Return-Receipt-To" then

set theSubject to subject of theMessage

set theRecipient to "Username <my@email.address>"

-- extract From and Reply-To names and addresses

set theFromSender to extract name fromsender of theMessage

set theFromAddress to extract address fromsender of theMessage

set theReplyTo to extract name fromreply to of theMessage

set theReplyToAddress to extract address fromreply to of theMessage

if theFromAddress = theReplyToAddress then

set theSender to extract name from sender of theMessage

set theAddress to extract address fromsender of theMessage

else

set theSender to extract name from reply to of theMessage

set theAddress to extract address fromreply to of theMessage

end if

display dialog "Respond to Delivery Confirmation Request?" & return & return & "Sent By: " & theFromAddress & return & return & "Respond To: " & theAddress buttons {"Send Response?", "Cancel"} default button 2 with title "Delivery Confirmation Request" with icon note

-- create a new message addressed to theSender at theAddress

set theDate to date sent of theMessage

set nowDate to current date

set theBody to "Your message" & return & return & " To: " & theRecipient & return & " Subject: " & theSubject & return & " Sent: " & theDate & return & return & "was read on " & nowDate & ". Thank you." & return

set newMessage to make new outgoing message with properties {subject:"Return Receipt: " & theSubject, content:theBody & return & return}

tell newMessage

makenewto recipientat end of to recipientswith properties {name:theSender, address:theAddress}

sendnewMessage

end tell

end if

end repeat

end try

end perform mail action with messages

end using terms from


Mac mini (Late 2012), OS X Mountain Lion (10.8.4)

Posted on Aug 28, 2013 7:53 AM

Reply
2 replies

Aug 28, 2013 8:31 AM in response to Ashok Aiyar

I use a modified version of an AppleScript that I found on the net to respond to incoming messages that contain either a "Disposition-Notification-To" header or "Return-Receipt-To" header. For some senders, the email address content for these custom headers doesn't match the address in the From header or Reply-To header.


How do I extract the sender and address from a custom header?

You need to understand that these field are comments. They are set to whatever values the actual sender wishes. The actual sender need not be who you see in the From field. The internet architecture doesn't require that they be verified as correct. Fortunately, people are mostly well behaived and internet email works.


You will have to decide what fields you wish to examine and how to handle discrepancies.


Robert

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript - extract email address from custom mail header

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.