phpemailphpmailer

PHPMailer Sending Duplicate Emails


I'm having a small issue with getting my php mailer to send a single email, it keeps sending a duplicate with it. I've check whether the script is being ran twice by putting a random number on the end of the subject but surprisingly, both emails have the same subject which rules that one out. So I thought the best thing would be to get another set of eyes to have a look and see if they can work out where I might be going wrong here. Just as a side note the SMTPDebug isn't outputting either which doesn't help resolve this issue. Of course I've changed the password and the hosts to examples but I can assure you that the emails are being sent successfully but for some odd reason it's sending two copies.

<?php    

    require("php/PHPMailer.php");
    require("php/SMTP.php");
    use PHPMailer\PHPMailer\PHPMailer;  

    $mail = new PHPMailer(true); 

    $mail->SMTPDebug = 2;
    $mail->isSMTP();
    $mail->Host = 'mail.example.com;';
    $mail->SMTPAuth = true;
    $mail->Username = 'info@example.com';
    $mail->Password = 'emailClientPass';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    $mail->setFrom('info@example.com', 'Info');
    $mail->addAddress('emailto@gmail.com', 'Email To Name');
    $mail->addReplyTo('info@example.com', 'Info');

    $mail->isHTML(true);
    $mail->Subject = 'Email Subject'.rand();;
    $mail->Body    = 'Email Body';
    $mail->AltBody = 'Email Body 2';

    $mail->send();

    if(!$mail->Send()) {
        echo 'Email Failed To Send.'; 
    } 
    else {
        echo 'Email Was Successfully Sent.'; 
    }

?>

Solution

  • Here is mistake

    //$mail->send(); //remove this one its work
    
    if(!$mail->Send()) {
        echo 'Email Failed To Send.'; 
    } 
    else {
        echo 'Email Was Successfully Sent.'; 
    }