目前,我开发了一个系统,当他们的产品保修即将到期时会通知用户。如果日期少于 45 天,则会自动向我发送一封电子邮件。现在,我使用下面的代码发送电子邮件。它可以毫无问题地运行。
$days = (strtotime("2013-9-23") - strtotime(date("Y-m-d"))) / (60 * 60 * 24);
if ($days<45)
include 'sendmail.php';
else {
echo "Problem!";
}
可能无法将日期逐一编码。所以,我认为系统可以读取表格本身中的每一行日期,但我不知道这样做。我该怎么办,这样上面的代码就可以读取数据而不需要具体的数据,比如“2013-9-23”等。
我的完整代码是:
// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "";
$db = "master_inventory";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = 'SELECT Lap_PC_Name, Lap_War_Expiry FROM laptop';
$date = 'SELECT Lap_War_Expiry FROM laptop';
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0)
$days = (strtotime($date) - strtotime(date("Y-m-d"))) / (60 * 60 * 24);
if ($days<45)
include 'sendmail.php';
else {
echo "Problem!";
}
while($row = mysql_fetch_array($result)) {
$Lap_PC_Name = $row['Lap_PC_Name'];
$Lap_War_Expiry = $row['Lap_War_Expiry'];
}
我的 sendmail.php 代码:
<?php
// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "";
$db = "master_inventory";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "SELECT * FROM laptop";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 10) {
for ($query=1; $query<=$result; $query++){
while($row = mysql_fetch_array($result)) {
$Lap_PC_Name = $row['Lap_PC_Name'];
$Lap_War_Expiry = $row['Lap_War_Expiry'];
}
}
$to = 'nazeni@domain.com';
$subject = 'Testing sendmail';
$message = 'The Following licensed will expired in less than one month. PC
Name:'.$Lap_PC_Name. '.The license will expire on '.$Lap_War_Expiry;
$headers = 'From: nazeni@domain.com';
if (mail($to, $subject, $message, $headers)) {
echo "Email sent.";
}
else {
echo "Email sending failed.";
}
}
?>