0

我有以下脚本调用查询数据库并将结果作为电子邮件输出。但是,我想单独发送结果(10 个结果 = 10 封电子邮件),而不是在一封包含所有结果的电子邮件中。

任何善良的灵魂可以指出我正确的方向吗?这就是我现在所拥有的。我应该 foreach 结果吗?

    <?php
//authentication for database
$hostname = "xxxx";
$username = "xxxx";
$password = "xxxx";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("test",$dbhandle) 
or die("Could not select examples");

//execute the SQL query and return records
$result = mysql_query("SELECT * FROM tbl_assignees WHERE work_pass_expiry = DATE(NOW())");

if (!$result) {
//handle your error
die('The query failed.');
}

//variable for email message
$to      = 'some email address';
$emailBody = "";

//fetch tha data from the database 
while ($row = mysql_fetch_array($result))
{
$subject = $row['name']."'s"." work pass is expiry soon";
$emailBody .="Creator: ".$row['rlog_create_user_name']." \n";
$emailBody .="Company: ".$row['company']."\n";
$emailBody .="Assignee's Name: ".$row['name']." \n";
$emailBody .="Fin No: ".$row['fin_no_']." \n";
$emailBody .="Relationship: ".$row['relationship']." \n";
$emailBody .="Main Holder's Fin :".$row['main_holder_fin']." \n";
$emailBody .="Work Pass Type :".$row['work_pass_type']." \n";
$emailBody .="Work Pass Expiry :".$row['work_pass_expiry']." \n";
$emailBody .="Passport No. :".$row['passport_no_']." \n";
$emailBody .="Passport Expiry :".$row['passport_expiry']." \n";
$emailBody .="D.O.B :".$row['d_o_b']." \n";
$emailBody .="Contact :".$row['contact']." \n";
$emailBody .="Email :".$row['email']." \n";
$emailBody .="Notes :".$row['note'];

}

$headers = 'From: Work Pass Notification System <no-reply@someemailaddress>' . "\r\n" .
    'Reply-To: someemailaddress' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


if(mail($to, $subject, $emailBody, $headers)) {
echo $emailBody;
echo 'Email sent successfully!';
} else {
echo $emailBody;
die('Failure: Email was not sent!');

}   
//close the connection
mysql_close($dbhandle);
?>
4

1 回答 1

0

在 while 循环中添加 mail 函数,如下所示:

while ($row = mysql_fetch_array($result))
{    $to      = 'some email address'; // it's good if you get email ids from database also
     $emailBody=""; // Clear body for each email.

     /** Your Email Subject and body Here **/

     $headers = 'From: Work Pass Notification System <no-reply@someemailaddress>' . "\r\n" .
    'Reply-To: someemailaddress' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

      if(mail($to, $subject, $emailBody, $headers)) {
          echo $emailBody;
          echo 'Email sent successfully!';
      } else {
          echo $emailBody;
          die('Failure: Email was not sent!');
      }
}

这将发送等于从数据库中检索到的记录数量的电子邮件。

于 2013-03-27T05:26:23.950 回答