dongzhi5386 2012-03-21 10:55
浏览 32
已采纳

向多个收件人发送电子邮件

I'm trying to send an e-mail to multiple e-mail address in my database. Here is my current code. It is only working when I specify a single e-mail address, however, I need to have them query my database and send the e-mail to each e-mail address. Where am I going wrong here?

    $elist = $database->getRows("SELECT * FROM `emails`");

    if ($elist) {
        foreach ($elist as $elist_result) {
            $frm = 'rdsyh@gmail.com';
            $sub = 'Weekly Work Report';
            ob_start(); // start output buffering
            include_once('mail_content.php');
            $mail_body = ob_get_contents(); // get the contents from the buffer
            ob_end_clean();
            $to = $elist_result['email'];
            $mailstatus = l_mail('', '', $to, $elist_result['firstname'] . ' ' . $elist_result['lastname'], $frm, 'HR', $sub, $mail_body);

        }
    }


if ($mailstatus == 'ok') {
    echo '<center><font color=red style="font-size:14px">Message has been sent Succesfully.....!</font></center><br>';
} else {
    echo $mailstatus;
}
  • 写回答

2条回答 默认 最新

  • dqingn8836 2012-03-21 11:07
    关注

    Well, there's a lot of abstraction here that we know nothing about from your code. Things to check:

    • Are you certain that your database query is returning all of the results you're looking for (is $elist populated properly)?
    • Are you certain that the query is returning data in the format that you're trying to access it in (is $to populated properly)?
    • Are you certain your l_mail() function is behaving (is it possible it exit's or otherwise terminates script execution in the middle of the first pass)?

    Based on what I see here, if everything else was working properly, you should successfully be sending a bunch of emails, one to each email in your list.

    Now, if instead you're trying to send a single email that is sent to all of the addresses at once, then you need to group the email addresses in the for loop and then run your mail function afterwards:

    <?
    $tos = array();
    foreach ($elist as $elist_result) {
        $tos[] = $elist_result['email'];
    }
    $frm = 'rdsyh@gmail.com';
    $sub = 'Weekly Work Report';
    ob_start(); // start output buffering
    include_once('mail_content.php');
    $mail_body = ob_get_contents(); // get the contents from the buffer
    ob_end_clean();
    $to = implode(', ', $tos);
    $mailstatus = l_mail('', '', $to, $elist_result['firstname'] . ' ' . $elist_result['lastname'], $frm, 'HR', $sub, $mail_body);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 配置UGM库出现问题
  • ¥15 把Excel导入MATLAB显示错误怎么解决?
  • ¥15 Java中消息和缓存如何使用
  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?