Kunal Khan
Kunal Khan

Reputation: 31

PHPMailer - Receive 2 mails each time I run send()

I am using PHPMailer 5.2.

Each time I tried to send mail, it goes twice. So receiver gets a single mail twice. I didn't change anything on the main class. Please help me.

class Mail{

    static function send($data){
        $mail = new PHPMailer;

        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = MAIL_HOST;  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = MAIL_USERNAME;                 // SMTP username
        $mail->Password = MAIL_PASSWORD;                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = MAIL_PORT;                                    // TCP port to connect to
        $mail->SingleTo = true;
        $mail->setFrom(MAIL_FROM_ID, MAIL_FROM_NAME);
        $mail->addAddress($data['email'], $data['name']);     // Add a recipient
        $mail->isHTML(true);                                  // Set email format to HTML

        $mail->Subject = $data['subject'];
        $mail->Body    = $data['body'];

        if(!$mail->send()) {
           return false;
        } else {
            return true;
        }
    }
} 

I'm using this code to send mail =>

class Index extends Controller{

    public function index(){

        $data['email'] = '[email protected]';
        $data['name'] = 'Kunal Khan';
        $data['subject'] = 'Hello Mr. Kunal Khan';
        $data['body'] = '<h1>Welcome in frsh Bazar</h1>';
        Mail::send($data);
    }
}

Upvotes: 2

Views: 547

Answers (1)

Maxim Masiutin
Maxim Masiutin

Reputation: 4782

You may receive duplicates because SingleTo is incompatible with the SMTP transport. The SingleTo only works with "sendmail" or "mail" transports, not with SMTP. If you use SingleTo with SMTP, this parameter is just ignored without any error or warning, and you may get duplicates. But this is highly unlikelly, since you only have one recipient address in the $data['email'] parameter. Just try to comment the SingleTo line and see whether it will make any difference.

The SMTP protocol is designed in a way that you cannot send one message to several different recipients, each having only its own address in the TO: field. To have each recipient have only its name in the TO:, the whole message have to be transmitted again. This explains why SingleTo is incompatible with SMTP.

According to the authors of the PHPMailer library, SingleTo is planned to be deprecated in the release of PHPMailer 6.0, and removed in 7.0. The authors have explained that it's better to control sending to multiple recipients at a higher level, since PHPMailer isn't a mailing list sender. They tell that the use of the PHP mail() function needs to be discouraged because it's extremely difficult to use safely; SMTP is faster, safer, and gives more control and feedback. And since SMTP is incompatible with SingleTo, the authors of PHPMailer will remove SingleTo, not SMTP.

Another, and the most probable reason may be that you code is just called twice, while the PHPMailer does everything correctly. To check that, add simple logging to your function, so a log string will be written each time you function will be called. So you will be able to count how many times was your function called: twice or once.

If you have access to the php error log on your server, just add the error_log() to the beginning of your send() function, e.g.

error_log($data['email']);

So you will see from the log how many times the function have been actually called. Just don't forget to remove the error_log line from your code. If you don't have access to the error log file, use file_put_contents:

file_put_contents($filepath, date("r", time())." ". $data['email'].PHP_EOL, FILE_APPEND);

Before calling it, set $filepath to the path name of the file to which you have access.

Upvotes: 1

Related Questions