2013-09-26 92 views
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 protected]"; 
$mail->Password = "somepassword"; 

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

$mail->From = "[email protected]"; 
$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"; 
?> 

回答

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 protected]"; 
$mail->Password = "somepassword"; 

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

$mail->From = "[email protected]"; 
$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"; 
} 
+0

能否请你解释一下吗? – user2810332

+0

看我的编辑。执行'$ mail-> send'的代码只运行一次,对于从数据库中提取的第一行。 – Iain

+0

它现在工作!感谢您的快速真棒答案。从来没有想过这是解决方案。再次感谢Lain! – user2810332