在MySQL datetime附近自动发送邮件 [英] Send mail automatically near MySQL datetime

查看:125
本文介绍了在MySQL datetime附近自动发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MySQL数据库中,我有一个包含datetime列的表。我想在当前时间在其中一个日期时间值之前的30分钟内发送电子邮件。
例如datetime值为20140623180000,服务器应在2014年6月23日17:30发送邮件。datetime列中的值不是常规的。任何人都知道如何以简单的方式做到这一点? (Linux在服务器上运行)

In my MySQL database I have a table containing a datetime column. I'd like to send emails whenever current time is 30 minutes before one of the datetime values. E.g. datetime value is '20140623180000' the server should send a mail at 17:30 on 23rd June 2014. The values in the datetime column aren't regular. Anyone knows how to do this in an easy way? (Linux running on server)

推荐答案

所以你需要一个cron运行每一分钟,寻找datetime<当前时间

So you will need a cron to run every minute that looks for records where datetime < currenttime

然后循环浏览结果并发送必要的电子邮件。

Then loop through the results and send the necessary email.

ie

#!/usr/bin/php
<?php
$db = new PDO($dsn);
$stmt = $db->query("SELECT * FROM table WHERE datetime > ?");
$stmt->execute(array(date("Y-m-d H:i:s", strtotime("-30 minutes")));
$r = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($r as $res){
  mail($to, $subject, $message, $headers);
}
?>

这篇关于在MySQL datetime附近自动发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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