dousha2020 2018-11-26 17:34
浏览 102
已采纳

如何使用PHP从数据库发送邮件到多个电子邮件列表

i am working on script to send a mail to a list of emails from my table using php

presently i run a query on the table and echo all the emails meeting such requirements from the sql query

sql="SELECT * FROM people WHERE status in('member','client')"; 
$result = mysql_query($sql)or die("Cannot query orders_products data" . mysql_error()); 

while($row=mysql_fetch_array($result)) { 
    $list = array();
$list[] = $row['Email']; 

echo implode(",",$list);

$email = 'xxxxxxxxxxx'; 
$emailto = implode( "," ,$list ); 

$subject = "xxxxxxxxxxxxxxxx"; 

$headers = "From: $email";

$body .= ++$i.") ".$row['title'] ."

"; 
} 
$body .= "Regards

"; 

$body .= "xxxxxxxxxxxxxxxxxx

";

$send = mail($emailto, $subject, $body, $headers); 

when i try passing these emails to a mail function snippet the mail sending fails.

Please what could i be doin wrong, i need help

Update :

$list[] = $row['Email']; 

    echo implode(",",$list);

all the emails are displayed

But without commas.

$emailto = implode( "," ,$list ); 

i use the same method of imploding the array of data gotten from

$list[] = $row['Email'];

By fail i just mean i was hoping since the emails got echoed using the implode it would all successfully pass the emails to $emailto and then send a mail to each of them.

  • 写回答

1条回答 默认 最新

  • dora0817 2018-11-26 19:06
    关注

    So, what I think you're trying to do is to get the email address from your table where the user is either a member or a client. Then, you have a from address, subject, and a body. In addition, it looks like maybe you're trying to add to the body some unique information. Finally, I think you want to send an individual email to each user, separately.

    You could modify your code to be like this:

    <?php
    $sql="SELECT * FROM people WHERE status in('member','client')";
    $result = mysql_query($sql) or die("Cannot query orders_products data" . mysql_error());
    
    $emailContent = 'xxxxxxxxxxx';
    $emailSubject = 'xxxxxxxxxxx';
    $body = 'xxxxxxxxxxxxxxx';
    $headers = "From: from@email.com";
    
    while ($row = mysql_fetch_array($result)) {
      // optionally modify body here...?
      $emailBody = $body .= $row['title']; // or whatever you're trying to do here...
    
      // send email to each individual person
      mail($row['Email'], $emailSubject, $emailBody, $headers);
    }
    

    I think this is how you'd modify your code to get it to do what you're asking.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化