为基于web的应用程序实现自动提醒电子邮件功能


Implement an automatic reminder email feature for web based application

我想为我的web应用程序创建一个功能,一旦用户输入我的数据库,每4周就会向他们发送一封电子邮件,提醒他们,例如,提供一些反馈。我听说cron工作是我想要的,但我很好奇还有什么,可能有php脚本存在吗?或者有一种简单的方法吗?

我想要一个类似倒计时的东西,从他们进入数据库开始倒计时,直到4周过去,然后调用一个php文件或向他们发送我选择的电子邮件的东西。如果可能的话,请告诉我!感谢

我想说的是使用cron作业(它可以每天在某个时间运行,这对发送电子邮件很好),cron作业可以调用一个php脚本,该脚本会查看所有用户并检查他们何时注册,看看是否有人在4周前注册(或是4周前的倍数)。对于任何满足此条件的人,您可以通过一个循环,并使用mail()函数向他们发送电子邮件。

Cron作业

登录到服务器上的shell,键入"sudo crontab-e",然后键入以下内容:

30 14 * * * php path/to/some/phpscript.php

在本例中,phpscript.php将在每天14:30(下午2:30)运行。但这并不意味着它会每天给所有用户发电子邮件!请参阅下面的脚本。

PHP脚本

<?php
# get all users (or your query could choose only users who signed up (4 weeks)*n ago)
$result = mysql_query('SELECT * FROM user');
$users = array();
while($row = mysql_fetch_assoc($result)) $users[] = $row;
# loop through users and (if you didn't already check) see which ones have signed up (4 weeks)*n ago
foreach ($users as $user) {
    # take the number of seconds and convert it to number of days
    $today = (int)(strtotime(date('c')) / 60 / 60 / 24);
    $signup_day = (int)(strtotime($user['signup_date']) / 60 / 60 / 24);
    # check if the amount of days since signup is a multiple of 28 (4*7)
    if (($today - $signup_day) && ($today - $signup_day) % 28 == 0) {
        send_mail_to($user);
    }
}
?>