PHPMailer发送重复的电子邮件

PHPMailer

<?php
    $mail = new PHPMailer;
    $mail->IsSMTP();
    $mail->IsHTML(true);
    $mail->SMTPSecure = "tls";
    $mail->Mailer = "smtp";
    $mail->Host = "smtp.office365.com";
    $mail->Port = 587;
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->Username = "xxx";
    $mail->Password = "xxx";
    $mail->setFrom('xxx', 'Website');

    //Send to Admin
    $AdminEmail = 'admin.example@gmail.com';
    $mail->AddAddress($AdminEmail, $AdminName);
    $mail->Subject  = "This is an email";
    $mail2 = clone $mail;
    $body = 'Hi Admin. This is an email';
    $mail->Body = $body;

    if(!$mail->Send()) {
    echo 'Message was not sent.';
    echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
    echo 'Message has been sent.';
    }

    //Send to User
    $UserEmail = 'user.example@gmail.com';
    $mail2->AddAddress($UserEmail, $UserName);
    $body2 = 'Hi User. This is an email';
    $mail2->Body = $body2;

    if(!$mail2->Send()) {
    echo 'Message was not sent.';
    echo 'Mailer error: ' . $mail2->ErrorInfo;
    } else {
    echo 'Message has been sent.';
    }
?>

我有一个问题,当我发送电子邮件时,当他们应该只收到$mail时,管理员将收到$mail和$mail2.虽然User没问题.通过接收$mail2可以正常工作.我试图把$mail-> ClearAddresses();但还是一样.碰巧是什么问题?

解决方法:

在添加管理员的地址和内容之前,请先克隆email变量. (如评论中所建议)

上一篇:3.2存款利息的计算


下一篇:qt 使用 QDir 读取文件地址 会出现 \u202A