使用php mysql向多个收件人发送唯一的电子邮件内容

时间:2015-09-04 07:33:18

标签: php mysql email while-loop

我想向多个用户发送唯一的确认链接,该用户列表从数据库mysql获取。我尝试使用以下代码,但它只发送第一个用户而不是全部。

<?php

  $get_ref_q = mysql_query("select * from $referal_details where ser_fk='$last_insert_id'");
  $get_last_ref_value = mysql_num_rows($get_ref_q);

  $z=1;
  if($get_last_ref_value > 0)
  {
    //$user_email = array();
    while($sel_per_fet = mysql_fetch_assoc($get_ref_q))
    {

      $rf_name = $sel_per_fet['rf_name'];
      $dy_link = $sel_per_fet['dy_link'];

      $user_email = $sel_per_fet['rf_email'];

      $userhtml = '<html>
        <body style="width:100%; font-family:Arial; font-size:13px; line-height:22px; background:#fff;  position:relative;color:#555555; margin:0px; padding:0px;">

          <div style="margin:0 auto; width:600px;">
            <div style="background:#fff; width:594px; float:left;border:#2fb25d 3px solid;">
              <div style="padding:27px; width:540px; float:left;text-align:center; border-bottom:#2fb25d 3px solid;">

              </div>
              <div style="background:#fff; padding:44px 34px; width:526px; float:left;">
                <h1 style="color:#000; font-size:20px; font-family:Arial; font-weight:normal; margin-bottom:20px;">
Dear '.$rf_name.' <br />
                </h1>

                <p>Vetri has refered you for this servey</p>
                <p>please find your servey link</p>
                <p><a href="'.$uri.'/index.php?token='.$dy_link.'">'.$uri.'/index.php?token='.$dy_link.'</a></p>

              </div>
            </div>
          </div>

        </body>
      </html>';

      $user_subject = 'Vetri has refered you for this servey';
      $user_message = $userhtml;
      $user_headers  .= 'MIME-Version: 1.0' . "\r\n";
      $user_headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      $user_headers .= "From: Servey <$comp_email> \r\n";
      mail($user_email,$user_subject,$user_message,$user_headers,"-f$comp_email");
      $z++;
    }
  }
?>

注意:我已经有$last_insert_id值。我想向每个用户发送唯一的电子邮件内容,而不是常见的竞争对手。提前谢谢。

1 个答案:

答案 0 :(得分:0)

你的代码看起来不错 - 除了一个小问题:

$user_headers  .= 'MIME-Version: 1.0' . "\r\n";
---------------^

此行不需要.,后续电子邮件可能会向$user_headers添加越来越多相同的标题,导致后续消息失败。