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 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起