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.

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能