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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵