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

imd*_*t39 0 php foreach phpmailer

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

PHP邮件程序

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();
}
Run Code Online (Sandbox Code Playgroud)

Rig*_*lly 5

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

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();
}
Run Code Online (Sandbox Code Playgroud)

请注意,只会清除to地址,如果您还使用抄送和密件抄送,您可能需要这样做

    //Clear all recipients (to/cc/bcc) for the next iteration
    $mail->clearAllRecipients();
Run Code Online (Sandbox Code Playgroud)