使用 php 邮件程序功能向​​一组用户发送批量电子邮件

Sending bulk email to an array of users using php mailer function

我正在使用 PHP 邮件程序功能向​​网站上的所有用户(大约 100/200)发送电子邮件。出于某种原因,在我测试时,电子邮件是第一次发送给第一个用户。第二次,到第1+2次。等等。我的循环中的错误在哪里?请帮忙!

    $res    =   mysql_query('select name, email from users');
    while($val  =   mysql_fetch_array($res)){
$emailTo    =   $val['email'];
$name   =   $val['name'];


           $content =   '<table width="100%" cellspacing="0" cellpadding="0" border="0" style="width:100.0%">
                        <tbody>
                            <tr>
                                <td valign="top" style="padding:15.0pt 0in 15.0pt 0in">
                                <div align="center">
                                <table width="650" cellspacing="0" cellpadding="0" border="1" style="width:487.5pt;background:white;border:solid #e0e0e0 1.0pt">
                                <tbody>
                            <tr>
                            <td width="126" style="border: medium none; padding-top: 10px; padding-left: 10px;"><a href="http://test.com/" target="_blank"><img src="images/new_logo.png" alt="test"/></a></td>
                        </tr>
                        <tr>
                            <td valign="top" style="border: medium none; padding-left: 10px; padding-right: 10px;">
                            <h1 style="margin-right:0in;margin-bottom:8.25pt;margin-left:0in;line-height:16.5pt">
                            <h1 style="margin-right:0in;margin-bottom:8.25pt;margin-left:0in;line-height:16.5pt">
                            <span style="font-family: tahoma; color: rgb(87, 87, 87); font-size: 15px; font-weight: lighter;">

                            <u></u>
                            <u></u>
                            </span>
                            </h1>
                            <div style="border:solid #e0e0e0 1.0pt; background:#f9f9f9">
                            </div>
                            <h1 style="font-family: tahoma; font-size: 15px; font-weight: 300; color: black; padding-left: 10px;font-weight:bold;text-align:center;">Certificate Of Participation</h1>
                            <p style="margin-right: 0in; margin-bottom: 6pt; margin-left: 35px; line-height: 20pt;">
                            Presented to:<br/>
                            '.$name.'<br/> 
                            for participating in the 2015 Conference.<br/>
                            April 19th, 2015
                            </br>-Team GDFC


                            </p>
                        </tbody>
                        </table>';

            $mail->SetFrom($email, $subject);

            $mail->AddReplyTo($email);

            $address = $emailTo;

            $mail->AddAddress($address);


            $mail->Subject    = $subject;

            $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

            $mail->MsgHTML($content);

            $mail->Send();
  }
    header("location:adminPage.php");

这样试试。

不要在 php 变量中存储电子邮件和姓名,直接在正文中使用 $val['email'] 和 $val['name']。

这将解决问题。

尝试像这样在循环中使用 AddAddress,

 for ($i = 0; $i < sizeof($val); $i++)
     $mail->AddAddress($val[$i]);

我终于明白了! 问题是我正在使用 php 邮件程序功能代码。并且 AddAddresses() 不断添加电子邮件 ID。所以现在,我在发送电子邮件后不久调用的代码中发现了一个 ClearAddresses()! :)

感谢您的帮助! :)