I need your help. I want to send different HTML texts using PHP Mailer. My PHP code works as an autoresponder.
When a person sends a message to my email address [email protected], the autoresponder should reply with HTML Text 1.
When a person sends a message to my email address [email protected], the autoresponder should reply with HTML Text 2.
What is the function for my code?
<?php
if (!isset($_GET['api_key']) || $_GET['api_key'] !== 'VALUE-OF-API') {
echo "Access denied!";
exit;
}
$unwantedEmails = array(
'no-reply',
'noreply',
'no-return',
'noreturn',
'automessage',
'auto-message',
'notification',
'support',
'postmaster',
'do-not-reply',
'donotreply',
'auto-reply',
'automated-reply',
'auto-response',
'out-of-office',
'vacation-reply',
'autoresponder',
'auto-responder',
'auto-reply',
'auto-reply-system',
'auto-message',
'auto-notification',
'automated-message',
'automated-response',
'automated-email',
'no-reply-needed',
'noreply-necessary',
'no-reply-requested',
'auto-no-reply',
'do-not-reply-automated',
'automated-no-reply',
'auto-response-only',
'automated-reply-only',
'no-reply-auto',
'auto-message-no-reply',
'noreply-automatic',
'no-reply-message',
'automated-donotreply',
'auto-no-return',
'automated-no-return',
'no-reply-auto-message',
'noreply-auto-response',
'auto-response-noreply'
);
require 'vendor/autoload.php';
use PhpImap\Mailbox;
use PHPMailer\PHPMailer\PHPMailer;
// Verbindungsinformationen zum IMAP-Server
$hostname = '{imap.mail.me.com:993/imap/ssl/novalidate-cert}';
$username = '[email protected]';
$password = 'xxx';
$mailbox = new Mailbox($hostname, $username, $password);
$answeredEmailsFile = 'answered_mails.txt';
$mailsIds = $mailbox->searchMailbox('ALL', SORTDATE, true);
$answeredEmails = [];
if (file_exists($answeredEmailsFile)) {
$answeredEmails = unserialize(file_get_contents($answeredEmailsFile));
}
if (!empty($mailsIds)) {
foreach ($mailsIds as $mailId) {
if (!is_array($answeredEmails) || !in_array($mailId, $answeredEmails)) {
$mail = $mailbox->getMail($mailId, false);
$from = $mail->fromAddress;
$isUnwantedEmail = false;
foreach ($unwantedEmails as $unwantedEmail) {
if (stripos($from, $unwantedEmail) !== false) {
$isUnwantedEmail = true;
break;
}
}
if (!$isUnwantedEmail) {
$subject = $mail->subject;
$date = $mail->date;
$email_body = $mail->textPlain;
echo "From: $from<br>";
echo "Subject: $subject<br>";
echo "Date: $date<br>";
echo "<br>";
sendAutoresponder($from, $subject, $email_body);
$answeredEmails[] = $mailId;
} else {
echo "No E-Mail sent to $from because rule do not reply to '$from'<br>";
}
}
}
$fileHandle = fopen($answeredEmailsFile, 'w');
if ($fileHandle) {
fwrite($fileHandle, serialize($answeredEmails));
fclose($fileHandle);
} else {
echo "Error: Failed to open the file for writing.";
}
} else {
echo "No new mails found.";
}
function sendAutoresponder($recipient, $subject, $body) {
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.mail.me.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'xxx';
$mail->setFrom('[email protected]', '[email protected]');
$mail->addReplyTo('[email protected]', '[email protected]');
$mail->addAddress($recipient);
$mail->Subject = '### Auto Message ###';
$mail->CharSet = 'UTF-8';
$mail->isHTML(true);
$mail->Body = '<html><body>Text for [email protected] here!</body></html>';
############ HOW TO SET A CUSTOM TEXT IF E-MAIL RECIEVED TO [email protected]?
$mail->addCustomHeader('Precedence', 'bulk');
$mail->addCustomHeader('Auto-Submitted', 'auto-generated');
if (!$mail->send()) {
echo 'E-Mail konnte nicht gesendet werden.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Autoresponded to ' . $recipient . ' sent.';
}
}
?>
I tried:
// Prüfen, ob die E-Mail an die no-reply Adresse gesendet wurde
if (stripos($mail->toAddress, '[email protected]') !== false) {
$email_body = "<html><body>E-Mail empfangen an [email protected]</body></html>";
} else {
$email_body = "<html><body>E-Mail empfangen an [email protected]</body></html>";
}
And:
// Prüfen, ob die E-Mail an die no-reply Adresse gesendet wurde
if (stripos($mail->recipient, '[email protected]') !== false) {
$email_body = "<html><body>E-Mail empfangen an [email protected]</body></html>";
} else {
$email_body = "<html><body>E-Mail empfangen an example.com</body></html>";
}
But both did not work!