0

データベースにクエリを実行し、結果を電子メールとして出力する次のスクリプト呼び出しがあります。ただし、すべての結果を含む 1 つのメールではなく、結果を個別に送信したいと考えています (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 ループ内にメール関数を追加します。

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 に答える