gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:03:16 25 4
gpt4 key购买 nike

我想为我的网络应用程序创建一个功能,一旦用户进入我的数据库,每 4 周就会向他们发送一封电子邮件,提醒他们,例如,提供一些反馈。我听说 cron job 是我正在寻找的东西,但我很好奇那里还有什么,是否可能存在一个 php 脚本或一个简单的方法来做到这一点?

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

最佳答案

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

定时任务

登录到您服务器上的 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);
}
}
?>

关于php - 为基于 Web 的应用程序实现自动提醒电子邮件功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17634089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com