【问题标题】:PHP Mailer returns true but email is not receivedPHP Mailer 返回 true 但未收到电子邮件
【发布时间】:2021-09-19 09:47:50
【问题描述】:

我在我的网站中使用 PHP Mailer 作为我的联系表格。

<?php 
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;

    require 'phpmailer/PHPMailer/src/Exception.php'; 
    require 'phpmailer/PHPMailer/src/PHPMailer.php';
    require 'phpmailer/PHPMailer/src/SMTP.php';
    

    $name = $_POST['name'];
    $lastname = $_POST['lastname'];
    $phone = $_POST['phone'];
    $message = $_POST['textarea'];
    $from = $_POST['email'];
    $to = "support@domain.com"; 
    $subject = 'email from website';



    $mail = new PHPMailer();
    $mail->Host   = "smtp.hostinger.com";
    $mail->SMTPDebug = 0;
    $mail->CharSet = 'UTF-8';
    $mail->SMTPAuth   = true;
    $mail->Port       = 465;
    $mail->Username   = $to;
    $mail->Password   = "mypassword";
    $mail->SMTPSecure = "ssl";
    $mail->From = $from;
    $mail->FromName = $name;
    $mail->addReplyTo($from, $name);
    $mail->Subject  = $subject;
    $mail->isHTML(true);
    $mail->Body = "Sender": " . $name . " " . $lastname . " \n <br/> Phone: ". $phone ." \r\n <br/> message: " . $message";;
    $mail->addAddress($to);


    
if(!$mail->Send())
{
   echo "Error sending: " . $mail->ErrorInfo;
}
else
{
   echo 'success';
}

?>

一旦我在我的网站上提交了联系表格,上面的代码就会返回 success 消息。

实际上没有收到任何电子邮件。我已经仔细检查了所有$_POST,值是正确的。还仔细检查了我的用户名和密码(我在问题中更改了它们)。

什么会导致 PHP Mailer 返回成功消息而不发送电子邮件?

PHP Mailer 调试

Sending with mail()<br>
Sendmail path: /usr/sbin/sendmail -t -i<br>
Envelope sender: <br>
To: support@domain.com<br>
Subject: =?UTF-8?B?15nXpteZ16jXqiDXp9ep16gg157XlNeQ16rXqA==?=<br>
Headers: Date: Sun, 19 Sep 2021 09:37:31 +0000From: test11 &lt;test@gmail.com&gt;Reply-To: test11 &lt;test@gmail.com&gt;Message-ID: &lt;sK7qvbbNwmP2IikzBwV7c2mD3TqDUGwEOLeW2HZng@www.reboost.co.il&gt;X-Mailer: PHPMailer 6.5.1 (https://github.com/PHPMailer/PHPMailer)MIME-Version: 1.0Content-Type: text/html; charset=UTF-8<br>
Result: true<br>

【问题讨论】:

  • 您没有检查$mail-&gt;send() 的返回值。如果发送邮件失败,则返回false
  • What can cause PHP Mailer to return success message...你不知道它是否返回成功消息,因为你没有检查。无论如何,您总是会呼应“成功”。检查 send() 函数的返回值。它可以在没有 PHP 异常的情况下失败。
  • the try and catch is checking if $mail-&gt;send() return true or false...不,它没有。 Try/catch 检查是否发生异常。这与函数的返回值无关。
  • 你需要告诉phpMailer使用smtp $mail-&gt;isSMTP();
  • 有时“发件人”需要与您的登录用户名地址相同,以防垃圾邮件

标签: php phpmailer


【解决方案1】:

你需要告诉 phpMailer 使用 SMTP

$mail-&gt;isSMTP();

OP 回复说以下错误:

Error sending: SMTP Error: The following recipients failed:  support@domain.com: <test@gmail.com>: Sender address rejected: not owned by user support@domain.com

这可能是由于“发件人”字段与 smtp 登录名不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 2012-10-27
    • 2015-05-05
    • 2022-11-03
    • 2018-12-19
    • 1970-01-01
    • 2020-12-23
    相关资源
    最近更新 更多