douiwn6941 2015-11-20 11:41
浏览 7

循环数据库记录集创建的电子邮件

I wish to send an email to one address and cc to other addresses drawn from a table.

// get all cc email id's for this users site
$results3h = mysql_query("SELECT user_id FROM company_ccemails WHERE site_id = '$site_id' ");
echo mysql_error();                 
while($row = mysql_fetch_array($results3h))
    { 
        $cc_id = $row['user_id'] ;

        //get email addresses for each id

       $results3i = mysql_query("SELECT username FROM user WHERE id = '$cc_id' ");
       echo mysql_error();      

       while($row = mysql_fetch_array($results3i))
           { 
              $ccemails = $row['username'] . "," ;
           }
           mysql_free_result($results3i); 
}   
mysql_free_result($results3h); 
//send emails to 

    $to = "support@mydomain.com ; ";
$subject = "$email_subject";
$message = "$email_message";                                        
$headers = "CC: " .$ccemails. "
";
$headers .= "From: " . strip_tags($myusername) . "
";
$headers .= "Reply-To: ". strip_tags($myusername) . "
";
$headers .= "BCC: " .$myusername. "
";
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1
";                                  
mail($to, $subject, $message, $headers); 

when sending this email only the 'to' and 1 'cc' is sent although there are 3 'cc' email address. if I move the send code under $ccemails = $row... then an email is sent to each 'cc' separately along with support@mydomain.com. this results in support@ getting lots of emails.

how do I change the code to get the cc emails in one string and send as one email so support only receives one copy?

I'm relatively new to PHP coding (usually ASP) and am sure this is straight forward but is confusing me at the moment

thanks for any help

  • 写回答

1条回答 默认 最新

  • duanchu0031 2015-11-20 11:48
    关注

    With $ccemails = $row['username'] . "," ; you are assigning the value of $row['username'] to your $ccemails variable. This means you override it everytime in the loop.

    You have to concat the string with .=

    Try this below:

    $ccemails = ""; // define this here, because otherwise you will get a notice
    while ( $row = mysql_fetch_array( $results3h ) ) {
        /**
         * Your code
         */
        while ( $row = mysql_fetch_array( $results3i ) ) {
            $ccemails .= $row['username'] . ",";
        }
        /**
         * Your code
         */
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c