【发布时间】:2011-07-28 18:17:37
【问题描述】:
我遇到了一个奇怪的 PHPmailer(5.1 版)问题,我正在尝试解决这个问题。我在这里看到了很多很好的反馈,所以我想我会尝试一下。我发现当我尝试使用基于$mail->send() 的条件语句创建自定义确认消息时,我会收到重复的电子邮件。我可以使用 PHPMailer 下载附带的通用 testemail.php 脚本来复制它。代码如下:
require '../class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$mail->SMTPDebug = 1;
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "mail.domain.com"; // SMTP server
$mail->Username = "username"; // SMTP server username
$mail->Password = "password"; // SMTP server password
$mail->IsSendmail();
$mail->From = "example_from@domain.com";
$mail->FromName = "First Last";
$to = "example@domain.com";
$mail->AddAddress($to);
$mail->Subject = "PHP Mailer test";
$message = "This is a test. \n";
$mail->Body = $message;
$mail->Send();
if ($mail->Send()) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
上面的代码回显了“消息已发送”确认,但随后发送了两封电子邮件。如果我注释掉$mail->send() 行,我仍然会收到“消息已发送”确认,并且只收到一条消息。如果我删除条件语句并将 $mail->send() 行注释掉,则不会发送任何电子邮件。
为什么添加条件语句会导致发送电子邮件而不调用$mail->send() 方法?
添加自定义确认消息的正确方法是什么?
【问题讨论】:
标签: php email phpmailer conditional-statements