当日期字段比到期日早一个月时 PHP 电子邮件提醒

发布于 2024-08-16 17:36:10 字数 2170 浏览 3 评论 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 <[email protected]>";
$to = "me <[email protected]>";
//$cc = "another person <[email protected]>";
$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 ;`

I'm trying to set up an e-mail notification as a reminder to let me know when a vehicle inspection is due, preferably a month in advance. When a date is one month prior to the date in the due_date inspection table, it should send the reminder. Your help would be greatly appreciated. Below is the php code and MySQL schema I have so far:

<?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 <[email protected]>";
$to = "me <[email protected]>";
//$cc = "another person <[email protected]>";
$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 schema:

`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 ;`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2024-08-23 17:36:10

你走在正确的道路上。剩下要做的唯一重要的事情是为 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
}

You're on the correct path. The only important thing left to do is to create cron job for your PHP script to run it once everyday.
Your script will have to check if the record is outdated and don't forget to modify your schema to contain the boolean field "NotificationSent" to avoid sending mail notification everyday.
More on cron jobs: http://www.developertutorials.com/blog/php/running-php-cron-jobs-regular-scheduled-tasks-in-php-172/
A script draft:

$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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文