0

我有一个名为“laptop”的表和一个名为“Lap_War_Expiry”的表内的列。当笔记本电脑的保修期即将到期时,我需要向用户发送电子邮件。供您参考,有不止一项保修将在一天内到期。但下面的代码只是从表“笔记本电脑”发送相同的数据。为什么会发生这种情况,我必须在代码中添加什么,以便电子邮件发送将从数据库中检索两个或更多数据?

这是我发送电子邮件的编码:

<?php
require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com'; // "ssl://smtp.gmail.com" didn't worked
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';


// ======== get database data ==================
$link = mysql_connect("localhost","root","");
$database="master_inventory";
mysql_select_db ($database,$link) OR die ("Could not open $database" );
$query = 'SELECT Lap_PC_Name, Lap_War_Expiry FROM laptop'; //xyz is id of desired user
name.
$result1 = mysql_query($query);

while($row = mysql_fetch_array($result1)) {
  $Lap_PC_Name = $row['Lap_PC_Name'];
  $Lap_War_Expiry = $row['Lap_War_Expiry'];
 }

$mail->Username = "email@gmail.com";
$mail->Password = "somepassword";

$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true; 

$mail->From = "email@gmail.com";
$mail->FromName = "AMS";
$mail->addAddress("emailaddress","AMS");
$mail->Subject = "Notification on warranty expiry";
$mail->Body = "Dear Madam,<br/><br />The licensed for the following PC will expired   
in less than one month.<br /><br /> PC Name : ".$Lap_PC_Name. "<br />Date of expired :"   
.$Lap_War_Expiry;

 if(!$mail->Send())
    echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
 else
    echo "Message has been sent";
?>
4

1 回答 1

0

您需要将所有内容从while循环下方移入其中。

编辑 更改

while($row = mysql_fetch_array($result1)) {
  $Lap_PC_Name = $row['Lap_PC_Name'];
  $Lap_War_Expiry = $row['Lap_War_Expiry'];
 }

while($row = mysql_fetch_array($result1)) {
  $Lap_PC_Name = $row['Lap_PC_Name'];
  $Lap_War_Expiry = $row['Lap_War_Expiry'];

$mail->Username = "email@gmail.com";
$mail->Password = "somepassword";

$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true; 

$mail->From = "email@gmail.com";
$mail->FromName = "AMS";
$mail->addAddress("emailaddress","AMS");
$mail->Subject = "Notification on warranty expiry";
$mail->Body = "Dear Madam,<br/><br />The licensed for the following PC will expired   
in less than one month.<br /><br /> PC Name : ".$Lap_PC_Name. "<br />Date of expired :"   
.$Lap_War_Expiry;

 if(!$mail->Send())
    echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
 else
    echo "Message has been sent";
 }
于 2013-09-26T07:43:45.507 回答