如何设置phpmailer自动回复?

问题描述 投票:0回答:1

请指导我如何根据我的代码设置自动回复电子邮件: 以下代码运行良好,所有电子邮件都将毫无问题地进入收件箱文件夹。

    <?php
sleep(3);
$recipients = 'recipients-email';
try {
require './phpmailer/PHPMailerAutoload.php';
preg_match_all("/([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)/", $recipients, $addresses, PREG_OFFSET_CAPTURE);
if (!count($addresses[0])) {
    die('MF001');
}
if (preg_match('/^(127\.|192\.168\.)/', $_SERVER['REMOTE_ADDR'])) {
    die('MF002');
}
$template = file_get_contents('rd-mailform.tpl');
if (isset($_POST['form-type'])) {
    switch ($_POST['form-type']){
        case 'contact':
            $subject = 'A message from your site visitor';
            break;
        case 'subscribe':
            $subject = 'Subscribe request';
            break;
        case 'order':
            $subject = 'Order request';
            break;
        default:
            $subject = 'A message from your site visitor';
            break;
    }
}else{
    die('MF004');
}
if (isset($_POST['email'])) {
    $template = str_replace(
        array("<!-- #{FromState} -->", "<!-- #{FromEmail} -->"),
        array("Email:", $_POST['email']),
        $template);
}else{
    die('MF003');
}
if (isset($_POST['message'])) {
    $template = str_replace(
        array("<!-- #{MessageState} -->", "<!-- #{MessageDescription} -->"),
        array("Message:", $_POST['message']),
        $template);
}
preg_match("/(<!-- #{BeginInfo} -->)(.|\n)+(<!-- #{EndInfo} -->)/", $template, $tmp, PREG_OFFSET_CAPTURE);
foreach ($_POST as $key => $value) {
    if ($key != "email" && $key != "message" && $key != "form-type" && !empty($value)){
        $info = str_replace(
            array("<!-- #{BeginInfo} -->", "<!-- #{InfoState} -->", "<!-- #{InfoDescription} -->"),
            array("", ucfirst($key) . ':', $value),
            $tmp[0][0]);
        $template = str_replace("<!-- #{EndInfo} -->", $info, $template);
    }
}
$template = str_replace(
    array("<!-- #{Subject} -->", "<!-- #{SiteName} -->"),
    array($subject, $_SERVER['SERVER_NAME']),
    $template);
$mail = new PHPMailer();
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = '';       // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '';                 // SMTP username
$mail->Password = '';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; 
$mail->setFrom('', '');
$mail->addAddress('', 'Information Center');     // Add a recipient
$mail->addReplyTo('', 'Information');
foreach ($addresses[0] as $key => $value) {
    $mail->addAddress($value[0]);
}
$mail->CharSet = 'utf-8';
$mail->Subject = $subject;
$mail->MsgHTML($template);
if (isset($_FILES['attachment'])) {
    foreach ($_FILES['attachment']['error'] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $mail->AddAttachment($_FILES['attachment']['tmp_name'][$key], $_FILES['Attachment']['name'][$key]);
        }
    }
}
   $mail->send();
die('MF000');
} catch (phpmailerException $e) {
die('MF254');
} catch (Exception $e) {
die('MF255');
}
?>

我尝试添加以下代码,但所有电子邮件都会进入垃圾邮件文件夹。如何解决问题?

    if ($mail->send()){ 
require_once('./phpmailer/class.phpmailer.php');
$mail             = new PHPMailer(); 
$mail->Host = '';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '';                 // SMTP username
$mail->Password = '';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; 
$mail->setFrom('[email protected]');
$to = $_POST['email'];
$subject = 'Thanks for contacting us';
$msg = 'Hello, thanks for contacting us';
mail($to, $subject, $msg);
} 

这是我的问题:

1)我上面的代码可以工作,但是邮件将进入垃圾邮件文件夹(就在自动回复部分)。案件如何破案?

问候

php phpmailer
1个回答
0
投票
$mail->setFrom('[email protected]');
$to = $_POST['userEmail'];
$subject = 'Thanks for Contacting Us!';
$msg = 'Hi!  Thanks for contacting us!';
mail($to, $subject, $msg);

上面是我如何编辑启动此线程的代码的自动回复部分。自动回复测试成功!但是,[电子邮件受保护] 不会显示在“发件人”区域中。仅显示服务器电子邮件地址。有什么建议么?谢谢! :--)

© www.soinside.com 2019 - 2024. All rights reserved.