【发布时间】: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 <test@gmail.com>Reply-To: test11 <test@gmail.com>Message-ID: <sK7qvbbNwmP2IikzBwV7c2mD3TqDUGwEOLeW2HZng@www.reboost.co.il>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->send()的返回值。如果发送邮件失败,则返回false。 -
What can cause PHP Mailer to return success message...你不知道它是否返回成功消息,因为你没有检查。无论如何,您总是会呼应“成功”。检查 send() 函数的返回值。它可以在没有 PHP 异常的情况下失败。 -
the try and catch is checking if $mail->send() return true or false...不,它没有。 Try/catch 检查是否发生异常。这与函数的返回值无关。 -
你需要告诉phpMailer使用smtp
$mail->isSMTP(); -
有时“发件人”需要与您的登录用户名地址相同,以防垃圾邮件