I receive two messages using PHPMailer

╄→尐↘猪︶ㄣ 提交于 2019-12-26 03:54:09

问题


I do not understand why two messages come to my mail. The send function is launched once and the message about the successful sending displays once.

<?php

require('class.phpmailer.php');

    $email = new PHPMailer();
    $email->CharSet = 'UTF-8';
    $email->From      = $_POST['mailmy'];
    $email->FromName  = '«Тэкс»';
    $email->Subject   = 'Ваша новая кухня почти готова.';
    $email->Body      = $_POST['mailText'];
    $email->AddAddress( $_POST['mailMeil']);

    $email->Send(); 
    echo 'Message has been sent';
    if (!$email->send()) {
    echo "Mailer Error: " . $email->ErrorInfo;
} else {
    echo "Message sent!";
}

?>

回答1:


You call the send() method twice:

$email->Send(); // first time
echo 'Message has been sent';
if (!$email->send()) { // second time

The code is doing exactly what you told it to do: send twice.

What you should do is store the result the first time and test that:

$sent = $email->Send();
echo 'Message has been sent';
if (!$sent) {

As an aside: your echo statement there doesn’t make sense. You shouldn’t tell the user the message has been sent if you don’t know that yet.



来源:https://stackoverflow.com/questions/51474832/i-receive-two-messages-using-phpmailer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!