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


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

我使用PHP mailer函数向网站上的所有用户发送电子邮件(可能是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");

尝试在循环中使用AddAddress,如下所示,

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

这样试试。

不要将电子邮件和名称存储在php变量中,只需直接使用正文中的$val['email']和$val[`name']即可。

这将解决问题。

我终于拿到了!问题是我使用的是php mailer函数代码。AddAddresses()不断添加电子邮件id。所以现在,我在代码中发现了一个ClearAddresss(),我在发送电子邮件后不久就调用了它!:)

感谢您的帮助!:)