My two-part Reply using To script

I often receive mail from vendors addressed to unique email addresses I've given them. I may want to reply from that unique address rather than my regular address. This two-part program solves that, using OS X Mail and a simple server-based PHP script. The PHP script can also be useful for other one-time sending purposes.

First, the PHP script. Call this "emailsender.php" (or anything else you'd like) and place it on your web server. Be SURE to password-protect the folder you're placing this into, else anyone in the world would be able to use your script to send junk mail. I repeat, PASSWORD PROTECT THIS SCRIPT!

//quick app to send a message to anyone from anyone.
//used to unsubscribe to mailings sent to odd accounts.
//print "emailto = $emailto<p>";

import_request_variables("gpc");
if ($hiddenstart == "yes")
{
$emailheaders = "From: " . $emailfrom;
mail ($emailto, $subject, $body, $emailheaders);
print "message sent!<p>";
}

?>

<form action="emailsender.php" method="post">
<br>FROM: <input type="text" size="50" name="emailfrom" value="<? print $emailfrom; ?>">
<br>TO: <input type="text" size="50" name="emailto" value="<? print $emailto; ?>">
<br>SUBJ: <input type="text" size="50" name="subject" value="<? print $subject; ?>">
<br>BODY: <textarea name="body" rows="20" cols="60"><? print $body; ?></textarea>
<input type="hidden" name="hiddenstart" value="yes">
<br><input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>

Then, the AppleScript code. Save this as a script in /Library/Scripts/Mail, and call it something like "MSC Reply Using to" (I prepend all my scripts by MSC so I know they're scripts I've written). Be sure to set the property at the top of the script to point to your PHP script (the one above). Open this script in Script Editor

--quick script to call my mailer URL

property thescripturl : "http://www.yoursite.com/yourpath/emailsender.php" --where your script is

using terms from application "Mail"
on perform mail action with messages theSelectedMessages
tell application "Mail"
if (count of theSelectedMessages) is equal to 0 then
display dialog "Please select a message in Mail first, then run this script again."
else if (count of theSelectedMessages) is equal to 1 then
try
set eachMessage to item 1 of theSelectedMessages
set theSource to source of eachMessage
set theSubject to subject of eachMessage
set theTo to the address of to recipient of eachMessage
set theFrom to the sender of eachMessage
set theContent to the content of eachMessage as string --flakes out on HTML email
set thenewURL to thescripturl
set thenewURL to thenewURL & "?emailfrom=" & theTo
set thenewURL to thenewURL & "&emailto=" & theFrom
set thenewURL to thenewURL & "&subject=" & theSubject
set thenewURL to thenewURL & "&body=" & theContent

my getTheURL(thenewURL)
on error errText number errNum
display dialog errText & " " & (errNum as string)
end try
else
display dialog "Please select only one message in Mail first, then run this script again."
end if
end tell
end perform mail action with messages
end using terms from

on getTheURL(theURL)
tell application "Safari"
activate
open location theURL
end tell
end getTheURL