Handling changes to PayPal ‘Notification of Payment Received’ Emails

Alt Text

If you are a PayPal seller you may have noticed that as of 24 January 2012 the ‘Notification of Payment Received’ email is now sent from the address ‘service@paypal.com’ rather than from the buyers email address. This was hugely inconvenient to us as we had set up an automated reply email based on the senders address.

This change is likely here to stay (although I have sent them my feedback, and am a little annoyed that no warning was given). We are Mac users and have created an AppleScript to get around this issue. The AppleScript is run by Mail Rules that we have set up. We wanted to share our AppleScript with you if you’re in the same boat and want a quick solution.

A quick rundown on what the code does: When a Mail Rule is triggered by an incoming payment received email, the script runs. It extracts the buyers email address from the incoming email, generates a new outgoing email to the buyer, sets the Subject and Content, and sends the email.

Here’s my disclaimer: although I am a developer, I’m embarrassed to say that this is my first AppleScript! It’s not pretty but it does the job. No doubt in the coming days/weeks we will fine-tune it. The script assumes that the format of PayPal’s email to you is the same as the layout that we get (as long as the buyers email address is displayed in the content of the email within parenthesis then it should be ok). Also, this isn’t a tutorial or a how-to on how to set up Mail Rules.

Steps:

  1. Create your AppleScript using the AppleScript Editor from utilities.
  2. Here’s the code below, paste this into your editor. You will need to read through the code and change the Subject and Content accordingly. Then compile and save it.
  3. If you were already using Mail Rules to automate your reply to the buyer, then just replace the “Reply to Message” action with a “Run Applescript” action and browse to the AppleScript you have just saved. See screenshot below. You should now be good to go, be sure to test it thoroughly!

AppleScript

Code to copy:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        tell application "Mail"
            repeat with eachMessage in theMessages
                -- We are going to extract the buyers email address from within the "()" in the email content
                set theContent to content of eachMessage
                try
                    set AppleScript's text item delimiters to {"(", ")"}
                    set contentParts to (every text item in theContent) as list
                    set AppleScript's text item delimiters to ""
                    set emailOnly to item 2 of contentParts
                    -- The recipient of the email will be the buyers email address
                    set recipientAddress to emailOnly
                    -- Set your own Subject here
                    set theSubject to "Your Email Subject Here"
                    -- Set your own Content here
                    set theSendContent to "Your Email Content Here"
                    -- Create new Email using the subject and content set up above
                    set theMessage to make new outgoing message with properties {subject:theSubject, content:theSendContent, visible:true}
                    tell theMessage
                        -- Set the recipient of the Email
                        make new to recipient with properties {address:recipientAddress}
                        -- Send the message
                        send
                    end tell
                end try
            end repeat
        end tell
    end perform mail action with messages
end using terms from

Update

11 Sep 2012

Mail Rules, AppleScript not working after Upgrade to Mountain Lion

Update

1 Aug 2013

We ran into some issues with the script when we upgraded to Mac OS X 10.8. We have made various changes to get it working consistently, here is the new version:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        repeat with eachMessage in theMessages
            set emailFrom to extract address from reply to of eachMessage
            try
                set recipientAddress to emailFrom
                set theSubject to "Your Subject Here"
                set theSignature to signature "Your Signature"
                set theSendContent to " "
                set theSender to "your email address here"
                set theMessage to make new outgoing message with properties {subject:theSubject, content:theSendContent, sender:theSender, visible:true}
                tell theMessage
                    make new to recipient with properties {address:recipientAddress}
                    set message signature to theSignature
                    send
                end tell
            end try
        end repeat
    end perform mail action with messages
end using terms from

A couple of points about the code:
1. We have switched to using the ‘reply-to’ email address of PayPal messages as this now contains the buyer’s email, rather than having to get it from inside the email content.
2. We now store all of our email “content” in a Mail signature, as this gives us more freedom to format the email fonts, paragraphs etc.
3. We have switched to using Imap email protocol, there seemed to be a difference in behaviour going from POP to Imap, so your code may need further tweaking if using POP.

8 thoughts on “Handling changes to PayPal ‘Notification of Payment Received’ Emails

  1. Thank you!
    Do you know of a way to specify the email’s sender (the email address that sends the email, such as “sales@company.com”

    • Hi Peter, yes you can specify the sender in the following line of the code:
      set theMessage to make new outgoing message with properties {subject:theSubject, content:theSendContent, sender:”sales@company.com”, visible:true}

    • I am experiencing a big inconsistency problem with this Applescript approach. It is run correctly in about 50% of the cases. But the rest of the times it never gets executed or at least not correctly. It’s as if the Applescript has not yet fetched the full subject heading and therefore fails to parse the buyer’s email address.

      If I select the message (after it has been fully downloaded) and hit Apply Rules the script it always runs correctly. Same thing if I, from my iPhone, set the message status to Unread and move it out of and back into the inbox.

      Do you have hang thoughts?

      Thanks,
      Peter

  2. I could not get your script to include the signature. Troubleshooting revealed that the signature needs to be set by Mail and not by the Message. My script below:

    using terms from application “Mail”
    on perform mail action with messages theMessages for rule theRule
    tell application “Mail”

    repeat with eachMessage in theMessages
    set theAccount to name of account of mailbox of eachMessage
    — We are going to extract the buyers email address from within the “()” in the email content
    — set theContent to content of eachMessage
    set emailFrom to extract address from reply to of eachMessage
    try
    set recipientAddress to emailFrom

    set theSubject to “Subject to include”
    set theSignature to “My signature’s name”
    set theSendContent to “”

    set theSender to “me@server.com”
    set theOutMessage to make new outgoing message with properties {sender:theSender, subject:theSubject, content:theSendContent}
    tell theOutMessage
    — set visible to true
    — Set the recipient of the Email
    make new to recipient at end of every to recipient with properties {address:recipientAddress}

    — Send the message
    end tell
    set the message signature of theOutMessage to signature theSignature
    send theOutMessage

    end try

    move eachMessage to mailbox “my Mailbox” of account theAccount

    end repeat
    end tell
    end perform mail action with messages
    end using terms from

    • Our signature is set up in Mail preferences, and is not linked to any specific Mail account. A couple of differences that I can see are that you have the ‘tell application “Mail”‘ command still there, whereas we have removed this from our new script. Also, when setting the signature our line is ‘set theSignature to signature “Signature Name”‘ which does differ from your line, and this may make a difference.