0

我正在尝试设置电子邮件通知作为提醒,让我知道车辆检查的到期时间,最好提前一个月。当日期比due_date检查表中的日期早一个月时,它应该发送提醒。您的帮助将不胜感激。以下是我到目前为止的 php 代码和 MySQL 架构:

<?php
//calling PEAR Mailer
require_once "Mail.php";
?>
<?php function connect()
{
  require('includes/config.php');
  return $conn;
}
?>
<?php
// Make a MySQL query
$query = "SELECT * FROM inspection";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$from = "Server Database <admin@server.com>";
$to = "me <me@server.com>";
//$cc = "another person <another@server.com>";
$subject = "Vehicle Inspection Reminder";
$body = "echo "The following vehicle is due for inspection:;
echo $row['vehicle'];
if (!$conn)
  {
  die('Could not connect: ' . mysql_error());
  }

?>";

$host = "mail.server.com";
$username = "username";
$password = "password";

$headers = array ('From' => $from,
  'To' => $to,
  'CC' => $cc,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>

MySQL 架构:

`CREATE TABLE IF NOT EXISTS `inspection` (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `vehicle` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
  `last_date` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
  `due_date` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `vehicle` (`vehicle`,`last_date`,`due_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;`
4

1 回答 1

0

你在正确的道路上。剩下要做的唯一重要的事情是为您的 PHP 脚本创建 cron 作业,使其每天运行一次。您的脚本将必须检查记录是否已过时,并且不要忘记修改您的架构以包含布尔字段“NotificationSent”以避免每天发送邮件通知。有关 cron 作业的更多信息:http: //www.developertutorials.com/blog/php/running-php-cron-jobs-regular-scheduled-tasks-in-php-172/ 脚本草稿:

$request = "SELECT due_date, NotificationSent FROM inspection WHERE due_date>$expected_date AND (NOT NotificationSent)";
$res = mysql_query($request);
while ($somerow = mysql_fetch_assoc($request))
{
    // ... here you call your mail notification script and set NotificationSent to true for the row
}
于 2010-01-05T17:35:43.727 回答