使用 PHPMailer 发送电子邮件时将所有电子邮件发送给同一个人的问题

Problem with sending all emails to the same person when sending email with PHPMailer

我正在尝试使用 PHPMailer 将邮件发送到多个电子邮件。首先,我列出了 table 中的人名。然后我使用循环向那些人发送电子邮件。但问题是每个人的信息都发到所有邮箱了。

PHPMailer

foreach ($id as $mailId) {
    $connect->connect('account where id=:id', array('id' => $mailId), '', 0);
    $users = $connect->connect->fetch(PDO::FETCH_ASSOC);

    $name = $users['name'];
    $mailAdres = $users['mail'];


    // ob_start();
    // include("PHPMailer/template.php");
    // $mail_template = ob_get_clean();

    $mail_template = $name;
    $mail->addAddress($mailAdres, $name);

    // $mail->addBCC($mailAdres, $name);

    //Content
    $mail->isHTML(true);
    $mail->CharSet = 'UTF-8';

    $mail->Subject = $odemeType;
    $mail->Body    = $mail_template;
    $mail->AltBody = '';
        $mail->send();
}

您需要在邮件发送后清除当前地址,否则每次循环都会向现有发送列表添加一封新电子邮件。

foreach ($id as $mailId) {
    $connect->connect('account where id=:id', array('id' => $mailId), '', 0);
    $users = $connect->connect->fetch(PDO::FETCH_ASSOC);

    $name = $users['name'];
    $mailAdres = $users['mail'];

    $mail_template = $name;
    $mail->addAddress($mailAdres, $name);

    // $mail->addBCC($mailAdres, $name);

    //Content
    $mail->isHTML(true);
    $mail->CharSet = 'UTF-8';

    $mail->Subject = $odemeType;
    $mail->Body    = $mail_template;
    $mail->AltBody = '';
    $mail->send();

    // clear this email address before continuing the loop
    $mail->clearAddresses();
}

Note that will clear only the to address, if you also use cc and bcc you may need to do

    //Clear all recipients (to/cc/bcc) for the next iteration
    $mail->clearAllRecipients();