Search code examples
phpphpmailer

I receive two messages using PHPMailer


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!";
}

?>

Solution

  • 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.