如何在电子邮件中检索多个数据? [英] How to retrieve more than one data in email?

查看:141
本文介绍了如何在电子邮件中检索多个数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为'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没有工作
$ mail-> Port = 465;
$ mail-> SMTPSecure ='ssl';


// ========获取数据库数据==================
$ link =的mysql_connect( 本地主机, 根, );
$ 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是所需用户的
名称的id。
$ 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->用户名=email@gmail.com;
$ mail-> Password =somepassword;

$ mail-> IsHTML(true); //如果要发送HTML格式的电子邮件
$ mail-> SingleTo = true;

$ mail-> From =email@gmail.com;
$ mail-> FromName =AMS;
$ mail-> addAddress(emailaddress,AMS);
$ mail-> Subject =保修期满通知;
$ mail-> Body =亲爱的女士,< br />< br />以下电脑的许可证将在不到一个月内到期
< br /> ;< br /> PC名称:$ Lap_PC_Name。 < br />过期日期:
。$ Lap_War_Expiry;

if(!$ mail-> Send())
echo未发送消息< br /> PHPMailer错误:。 $ MAIL-> ERRORINFO;
else
echo已发送消息;
?>


解决方案

您需要从循环到它。



编辑
更改

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

to

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

$ mail->用户名=email@gmail.com;
$ mail-> Password =somepassword;

$ mail-> IsHTML(true); //如果要发送HTML格式的电子邮件
$ mail-> SingleTo = true;

$ mail-> From =email@gmail.com;
$ mail-> FromName =AMS;
$ mail-> addAddress(emailaddress,AMS);
$ mail-> Subject =保修期满通知;
$ mail-> Body =亲爱的女士,< br />< br />以下电脑的许可证将在不到一个月内到期
< br /> ;< br /> PC名称:$ Lap_PC_Name。 < br />过期日期:
。$ Lap_War_Expiry;

if(!$ mail-> Send())
echo未发送消息< br /> PHPMailer错误:。 $ MAIL-> ERRORINFO;
else
echo已发送消息;
}


I have a table named 'laptop' and a column inside the table named 'Lap_War_Expiry'. I need to send an email to user when the warranty of the laptop is going to expire soon. For your information, there is more than one warranty that will expired in a day. But the code below is only send the same data from table 'laptop'. Why is that happened and what I have to add in the code so that the email send will retrieve two or more data from database ?

This is my coding for sending the email :

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

解决方案

You need to move everything from below your while loop into it.

EDIT Change

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

to

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

这篇关于如何在电子邮件中检索多个数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆