I have a contact me form on my website. A visitor fills out their name, email, subject, and message. I receive an email with the following info:
From: contact@mywebsite.com
To: contact@mywebsite.com
Subject: New submission with subject: Example Subject
Reply-To: John Doe <john.doe@example.com>
Hello,
Someone has just submitted a new form on your website.
Name: John Doe
Email: [john.doe@example.com](mailto:john.doe@example.com)
Subject: Example Subject
Content:
This is a test form submission.
–
Every email I receive is exactly like this. How would I create a macOS mail rule using an apple script to automatically reply to this email: (I know how to make the rule and select the script, I just can’t get any of the dozen scripts I’ve made to work)
From: contact@mywebsite,com
To: john.doe@example,com
Subject: Re: Example Subject
John,
Thank you for contacting me. I will respond as soon as I am able.
[automatically select and add my signature I have created for the contact@mywebsite,com address]
This is pretty straightforward, once you get the hang of Mail Rules.
The trickiest part is extracting the specific pieces of information from the incoming mail. There are several ways to do that.
Given the headers, I’d use the Reply-To as the basis for the destination, which only leaves extracting the sender’s email. The ‘easy’ way to do this would be from the Reply-To, but it could also be extracted from the message body with a little more work.
Here’s my first run. Hopefully the comments will explain my thinking, but feel free to post back with any other questions.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
using terms from application "Mail"
on perform mail action with messages messageList in mailboxes mbox for rule aRule
-- messageList is a list of messages that triggered the rule, so you have to iterate through them
repeat with eachMessage in messageList
-- get the sender's name and address
set replyToAddress to eachMessage's reply to
set senderName to extract name from replyToAddress
set senderAddress to extract address from replyToAddress
set senderFirstName to word 1 of senderName
-- get the incoming message subject
-- use a special handler to extract the relevant details
set newSubject to "Re: " & my parseSubject(eachMessage's subject)
-- now make a new email
set newMessage to make new outgoing message
-- and fill in the blanks
tell newMessage
make new to recipient with properties {name:senderName, address:senderAddress}
set sender to "contact@mywebsite.com"
set subject to newSubject
set content to senderFirstName & ",
Thank you for contacting me. I will respond as soon as I am able."
end tell
-- uncomment the next line to send the mail, otherwise it will just sit here (for testing)
-- send newMessage
end repeat
end perform mail action with messages
end using terms from
on parseSubject(theSubject)
set oldTIDs to my text item delimiters
set my text item delimiters to ":"
set newSubject to last text item of theSubject
set my text item delimiters to oldTIDs
return newSubject
end parseSubject
I did have some trouble with the signature, but if you have a default signature set for the sending account, you shouldn’t have a problem.