Code: Select all
<?php
/*
Email auto-responder script. Save in a file named responder.php
The script reads an IMAP mail box and auto-responds to all emails where the to address matches an address in the addressMatch array.
All messages checked by the script are marked as read and the script will only respond to unread messages. This prevents duplicate responses.
Optionally, can set a flag to only send one response per received email address.
-> Controlled by entries in the responder.dat file, which the script will create for you.
-> To reset to send everyone a response again, simply delete the responder.dat file.
You must review and change as needed all the variables in the section below.
*/
// This section contains the variables which are set to use the auto-responder script
$server = "mail.yourdomain.com";
$user = "XXXXXX";
$password = "XXXXXX";
$subjectPrefix = "Automatic Response to: "; // response subject prefix - attaches to the front if the original subject
$addressMatch = array("[email protected]","[email protected]"); // email address array of the to email addresses you want processed
$responseMessage = "The auto-response message goes here"; // your auto-response message
$returnAddress = "[email protected]"; // return address for the autoresponse - use an address NOT in $addressMatch or the script will not run
$returnLimit = "N"; // must be Y to limit the auto-responses to one per email address
// End of user variables section - do not modify anything below this line unless you know what you are doing
$path = "responder.dat"; // path to the file that contains addresses that have already received responses - used if $returnLimit = "Y"
if (!preg_match("(^[-\w\.]+@([-a-z0-9]+\.)+[a-z]{2,4}$)i", $returnAddress)) die('Return address is not valid');
if (in_array($returnAddress,$addressMatch)) die('Cannot use this email address as a return address');
$addressList = "";
if (file_exists($path)) $addressList = file_get_contents($path);
$host = "{".$server.":143"."}"."INBOX";
$msgStream = imap_open($host, $user, $password);
$check = imap_mailboxmsginfo($msgStream);
$number = $check->Nmsgs;
function getheader($msgStream, $msgNumber) {
global $addressMatch;
$mailHeader = imap_headerinfo($msgStream, $msgNumber);
$headerArray = array();
$to = $mailHeader->to;
foreach ($to as $id => $object) {
$test = $object->mailbox . "@" . $object->host; // to address
if (in_array($test,$addressMatch)) {
$headerArray[0] = "match";
$headerArray[1] = $object->personal; // to personal
$headerArray[2] = $object->mailbox . "@" . $object->host; // to address
}
}
$from = $mailHeader->from;
foreach ($from as $id => $object) {
$headerArray[3] = $object->personal; // from personal
$headerArray[4] = $object->mailbox . "@" . $object->host; // from address
}
if ($mailHeader->Unseen == 'U' || $mailHeader->Recent == 'N') { $headerArray[5] = "new"; } else { $headerArray[5] = "old"; }
$headerArray[6] = $mailHeader->subject;
return $headerArray;
}
$msgNumber = "1";
while ($msgNumber <= $number) {
$headerArray = getheader($msgStream, $msgNumber);
$body=imap_body($msgStream,$msgNumber);
if ($headerArray[0] == "match") {
if (($returnLimit !== "Y" OR ($returnLimit == "Y" AND substr_count($addressList,$headerArray[4]) == "0")) AND $headerArray[5] == "new" ) {
$addressList .= $addressList . "|" . $headerArray[4];
$subject = $subjectPrefix . $headerArray[6];
$headers = "From: <$returnAddress>\n";
$headers .= "Reply-To: <$returnAddress>\n";
mail($headerArray[4],$subject,$responseMessage,$headers,"-f$returnAddress");
}
}
$msgNumber ++;
}
imap_close($msgStream);
if ($returnLimit == "Y") file_put_contents($path,$addressList);
?>