【发布时间】:2021-10-15 00:39:39
【问题描述】:
正如标题所暗示的那样 我试图发送一封电子邮件(一封电子邮件),但我面临的问题很奇怪。 每当打开调试时,电子邮件只会发送一次。 但是关闭调试后,一次会发送大约 3 到 4 封电子邮件。
注意:我使用的是 localhost,而不是实际的服务器。
为了诊断问题,我执行了以下操作: 1- 使用 md5 在“主题”中生成随机字符串,以检查发送的电子邮件是否具有相同的内容或不同的内容。结果完全不同。意思是,电子邮件不是重复的,而是实际上被发送了几次。 2- 在没有扩展的浏览器中打开项目,以确保问题不是在多次加载我的项目页面的扩展中。并且结果也与数字1相似,不同的“主题”也。
所以,长篇大论。我不知道是什么导致了这个问题的发生。以及为什么它仅在打开调试时才停止发生。
注意:这不是我第一次使用 PHP mailer,但我第一次遇到这个问题。我也在使用最新版本的 PHP mailer (6.5.1)
这是我的整个代码: 在 PHP 邮件文件中:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require ABSPATH.'inc/phpmailer/script/src/PHPMailer.php';
require ABSPATH.'inc/phpmailer/script/src/Exception.php';
require ABSPATH.'inc/phpmailer/script/src/SMTP.php';
function SendEmail(){
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->isSMTP();
$mail->Host = '*****';
$mail->SMTPAuth = true;
$mail->Username = '*****';
$mail->Password = '*****';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->SMTPAutoTLS = false;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->ClearAllRecipients();
$mail->clearAttachments();
//Recipients
$mail->setFrom('*****', '******');
$mail->addAddress('*******', '*****');
$mail->addReplyTo('******', '******');
//Attachments
$mail->addAttachment(ABSPATH.'upload/dog.jpg', 'new.jpg');
//Content
$mail->isHTML(true);
$mail->Subject = md5(rand()); //This is how I'm checking whether it sends same email with same subject header or different ones. and it does send different ones
$mail->Body = 'This is the HTML message body <b>in bold!</b>'. md5(rand());
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->ContentType = 'text/html; charset=utf-8\r\n';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
这是我用来调用函数的文件
<?PHP
// NO LOOP here
require_once(ABSPATH.'inc/phpmailer/phpmailer.php');
SendEmail();
【问题讨论】:
-
“debug”是指 PHPMailer、WordPress 还是 PHP?