php cronjob 自动提醒邮件
php cronjob auto reminder email
我构建了一个网络应用程序,用户支付 1/3/6 个月的费用并加入该网站,我需要在用户帐户到期 15 天之前向用户发送提醒邮件,我该如何实现?我不理解正确的逻辑...我正在将注册日期、到期日期存储在数据库中,下面的逻辑可以正常工作吗?
<?php
$expiringDate = "2015-07-21";
$todaydate = date("Y-m-d");
$date = date("Y-m-d",strtotime("-15 days", strtotime($expiringDate)));
if($todaydate == $date){
//send mail
}else{
//quit
}?>
如果今天是过期日,我还想更改数据库中的值...是在其他 cronjob 中更改更好,还是我可以像这样在上面的代码中更改...
<?php
$expiringDate = "2015-07-21";
$todaydate = date("Y-m-d");
$date = date("Y-m-d",strtotime("-15 days", strtotime($expiringDate)));
if($todaydate == $date){
//send mail
}else{
//check and change the value if today is the expiring
}?>
我走的路是否正确,是否安全,或者是否有其他更好的方法来完成这项工作
我建议 运行每天执行 cron 作业。
然后您的 PHP 脚本应该检查所有在 15 天后过期的人。
然而,正如其他人指出的那样,如果有一天您的 cron 作业失败,您可能会错过一批人。
因此,我会检查剩余 15 天或 更少 的人,他们没有在您的数据库中设置针对他们的提醒标志。这意味着如果 cron 作业对剩余 15 天的人失败,那么在 14 日/13 日/12 日等剩余的一天,您的脚本将看到未针对他们设置提醒标志,并且仍会发送提醒。
<?php
$reminderSent = false; // Get this value from the db (true or false)
$expiryActioned = false; // Get this value from the db (true or false)
$expiringDate = strtotime('2015-07-21'); // Get this date from the db
$todayDate = time();
$reminderDate = strtotime("-15 days", $expiringDate);
if ($todayDate >= $reminderDate && $reminderSent == false) {
// Send mail
// Set flag $reminderSent in database to indicate reminder has been sent
} elseif ($todayDate >= $expiringDate && $expiryActioned == false) {
// Do something
// Set $expiryActioned in database to indicate the user has expired and something has been done about it
}
?>
但是,我不会将上述逻辑构建到您的 SQL 查询中以提高效率,而不是 select 每个人并 运行 全部使用上述逻辑。
快速示例:
// Select all users that expire in 15 days or less
SELECT `userid`, `name`, `email` FROM `user` WHERE NOW() >= DATE_SUB(`expiry`, INTERVAL 15 DAY) AND reminder_sent = 0
// Now loop through each user, send them an email and then:
UPDATE `user` SET reminder_sent = 1 WHERE `userid` = X
和
// Select all users that have expired
SELECT `userid`, `name`, `email` FROM `user` WHERE NOW() >= `expiry` AND `expiry_actioned` = 0
// Now loop through each user, do whatever you need to and then:
UPDATE `user` SET expiry_actioned = 1 WHERE `userid` = X
我构建了一个网络应用程序,用户支付 1/3/6 个月的费用并加入该网站,我需要在用户帐户到期 15 天之前向用户发送提醒邮件,我该如何实现?我不理解正确的逻辑...我正在将注册日期、到期日期存储在数据库中,下面的逻辑可以正常工作吗?
<?php
$expiringDate = "2015-07-21";
$todaydate = date("Y-m-d");
$date = date("Y-m-d",strtotime("-15 days", strtotime($expiringDate)));
if($todaydate == $date){
//send mail
}else{
//quit
}?>
如果今天是过期日,我还想更改数据库中的值...是在其他 cronjob 中更改更好,还是我可以像这样在上面的代码中更改...
<?php
$expiringDate = "2015-07-21";
$todaydate = date("Y-m-d");
$date = date("Y-m-d",strtotime("-15 days", strtotime($expiringDate)));
if($todaydate == $date){
//send mail
}else{
//check and change the value if today is the expiring
}?>
我走的路是否正确,是否安全,或者是否有其他更好的方法来完成这项工作
我建议 运行每天执行 cron 作业。
然后您的 PHP 脚本应该检查所有在 15 天后过期的人。
然而,正如其他人指出的那样,如果有一天您的 cron 作业失败,您可能会错过一批人。
因此,我会检查剩余 15 天或 更少 的人,他们没有在您的数据库中设置针对他们的提醒标志。这意味着如果 cron 作业对剩余 15 天的人失败,那么在 14 日/13 日/12 日等剩余的一天,您的脚本将看到未针对他们设置提醒标志,并且仍会发送提醒。
<?php
$reminderSent = false; // Get this value from the db (true or false)
$expiryActioned = false; // Get this value from the db (true or false)
$expiringDate = strtotime('2015-07-21'); // Get this date from the db
$todayDate = time();
$reminderDate = strtotime("-15 days", $expiringDate);
if ($todayDate >= $reminderDate && $reminderSent == false) {
// Send mail
// Set flag $reminderSent in database to indicate reminder has been sent
} elseif ($todayDate >= $expiringDate && $expiryActioned == false) {
// Do something
// Set $expiryActioned in database to indicate the user has expired and something has been done about it
}
?>
但是,我不会将上述逻辑构建到您的 SQL 查询中以提高效率,而不是 select 每个人并 运行 全部使用上述逻辑。
快速示例:
// Select all users that expire in 15 days or less
SELECT `userid`, `name`, `email` FROM `user` WHERE NOW() >= DATE_SUB(`expiry`, INTERVAL 15 DAY) AND reminder_sent = 0
// Now loop through each user, send them an email and then:
UPDATE `user` SET reminder_sent = 1 WHERE `userid` = X
和
// Select all users that have expired
SELECT `userid`, `name`, `email` FROM `user` WHERE NOW() >= `expiry` AND `expiry_actioned` = 0
// Now loop through each user, do whatever you need to and then:
UPDATE `user` SET expiry_actioned = 1 WHERE `userid` = X