如何在电子邮件中检索多个数据?

时间:2013-09-26 07:39:11

标签: php email

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

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

<?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";
?>

1 个答案:

答案 0 :(得分: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";
 }