PHPMailer sending email twice

35 Views Asked by At

I've setup a very basic example using phpmailer, and it's working, except it sends the email twice. The script is only being executed once, but I'm always getting 2 separate emails in the destinatary email (i know because the subject changes the rand() value each time). Is this a bug, or am I doing something wrong?

This is my current code:

<?php
# phpmailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

# vendor
require 'vendor/autoload.php';

# create mail object
$mail = new PHPMailer(true);

try {
    # smtp configuration
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->isSMTP();
    $mail->Host = 'smtp.example.com';
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = '*************';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // for 587
    $mail->Port = 587;

    # from
    $mail->setFrom('[email protected]', 'My name');

    # to
    $mail->addAddress('[email protected]', 'My friend');

    # content
    $mail->isHTML(true);
    $mail->Subject = 'This is a test: '.rand();
    $mail->Body = '<strong>Hello!</strong> This is a test';
    $mail->AltBody = 'Hello! This is a test';

    # send email
    $mail->send();
    echo 'Successfully sent!';
}
catch(phpmailerException $e) {
    echo $e->errorMessage();
} 
catch(exception $e) {
    echo $mail->ErrorInfo;
}
die();
0

There are 0 best solutions below