2012-09-15 44 views
0

我希望能够根据mysql日期自动发送电子邮件。 我知道我可以使用Crontabs来安排它们,但问题是Crontabs只能指定日期,月份,星期等等 - 它不够具体..根据MySQL日期发送自动电子邮件

MySQL表格填充了不同的日期。我希望它能够在特定记录的日期起3个月后自动发送电子邮件。

有谁知道我能做什么?

+0

您在cron中运行一个程序,它在SQL语句中执行这些检查。每天运行一次,检查3个月前的记录。 – andrewsi

+0

http://stackoverflow.com/questions/680999/how-can-i-send-an-auto-email-from-mysql-records –

回答

0

安排每天运行一次的cron,检查那天需要发送电子邮件的记录。

想象一下,你有一个像notify_at一列是一个日期 - 那么你每天的cron基本上没有

SELECT * FROM records WHERE notify_at = DATE(NOW())

你的cron可以存储在/etc/cron.d

/etc/cron.d/send_reminders

# run once a day at 1AM 
0 1 * * * someuser /path/to/your/script 

脚本的内容是你的逻辑,在psu edo-code:

results = fetch_by_sql("SELECT * FROM records WHERE notify_at = DATE(NOW()) AND last_notified_at IS NULL"); 
foreach(results as record) { 
    send_email(record.recipient, "Your subject", "Your body"); 
    /* mark the record as notified so we dont ever send multiple emails */ 
    update_sql("UPDATE records SET last_notified_at = NOW() WHERE id = " + record.id); 
} 
+0

你能解释一下怎么做吗?在步骤中,即是 – ThisBoyPerforms