2013-09-26 93 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