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条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记