use*_*104 3 php email phpmailer conditional-statements
我遇到了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;
}
Run Code Online (Sandbox Code Playgroud)
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
$mail->send()$mail->send()
上面的代码回应了"已发送消息"确认,但随后发送了两封电子邮件.如果我注释掉该$mail->send()行,我仍然收到"已发送消息"确认并且只收到一条消息.如果我删除条件语句并将该$mail->send()行注释掉,则不会发送任何电子邮件.
为什么添加条件语句会导致在不调用
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;
}
Run Code Online (Sandbox Code Playgroud)
} catch (phpmailerException $e) {
echo $e->errorMessage();
}方法的情况下发送电子邮件?添加自定义确认消息的正确方法是什么?
Joh*_*ess 12
当你$mail->Send()输入条件时,你实际上是再次调用它,发送另一条消息,并检查是否发送了第二条消息.
如果你保持
if ($mail->Send()) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
Run Code Online (Sandbox Code Playgroud)
并摆脱原来的,无条件的发送电话,你应该没事.
或者,如果它对您来说更清楚,或者您需要在其他地方进行某些处理,这取决于消息是否已成功发送,您可以执行基本相同的操作:
$status = $mail->Send();
if ($status) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5185 次 |
| 最近记录: |