drj14664 2015-11-20 11:47
浏览 35
已采纳

使用php邮件向多封电子邮件发送消息

I have a table below in MYSQL

Mmeber_Email---------------Member_Name------Data---
mike01@yahoo.com  --------  Mike ---------- 100 ------
jacknick@gmail.com  --------Jack -----------50 ----
jillwag@hotmail.com  -------Jill ---------- 75 ------
jnash@gmail.com  --------   John ---------- 10 ------

Now, I have managed to extract the data from the database using PHP. but i don't know how to email the data of each member on their respective email using PHP Mail function. How should i tackle the problem of sending emails to multiple recipients using the PHP mail function below i.e Mike Should get the email containing the message 100, Jack 5 and so on..

<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = //data extracted from database using a common query comes here
$headers = "From: webmaster@example.com";

mail($to,$subject,$txt,$headers);
?>
  • 写回答

2条回答 默认 最新

  • dougu2006 2015-11-20 13:10
    关注

    You would need to loop through the results returned from MySQL. Here is an example:

    while($row = mysqli_fetch_array($results)) { // start loop
    
        $to       = $row['member_email']; 
        $subject  = 'the subject'; 
        $message  = "Dear ". $row['member_name'] . "
    ";
        $message .= "Here is your data: " . $row['member_data'] . "
    ";
        $headers = 'From: webmaster@example.com' . "
    " .
           'Reply-To: webmaster@example.com' . "
    " .
           'X-Mailer: PHP/' . phpversion();
    
        mail($to, $subject, $message, $headers);
    }
    

    The data from your database is contained in the $row array setup by the while{} loop.

    Make sure to pay attention to the details for mail in the docs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?