【问题标题】:how to send mail on specified date and time using mysql and php如何使用mysql和php在指定的日期和时间发送邮件
【发布时间】:2018-04-27 00:04:00
【问题描述】:

我所拥有的是,

  1. 用户在数据库中指定的日期和时间。
  2. 一个用于发送邮件的 php 脚本。

必填

  1. 当指定的日期和时间到达时触发php脚本。

我正在使用本地 apache 服务器

示例:A 想在 5 天后向 B 发送邮件。 A 在数据库中输入日期和时间,5 天后将通过运行 php mailer 脚本自动发送。

有没有办法安排这个?

【问题讨论】:

  • 制作 1 个 cron 作业并安排它。在 db 中输入日期和时间时,还要添加发送日期和时间,以便在 cron 中指定在发送日期和时间运行 cron 并发送电子邮件的条件。

标签: php mysql email automation


【解决方案1】:

编写一个函数以从数据库中获取发送者和接收者列表以用于执行日期。

ex:  check php date is equal to DB date to sent the email.
Add the php script to a cron job

查看示例代码

$queryToGetuserListToSendEmail = "select * from userlist where date_toSend_email = ''". date("Y-m-d H:i:s")."'";
$userListToSendEmail = array();//senders relievers and other details taken from DB to send emails

foreach ($userListToSendEmail as $usersToSendEmail){
//sendEmail
}

每天凌晨 2 点 30 分运行作业的 Cron 作业(在您的 cron 选项卡中添加)

30 2 * * * php /pathtoyourfile

【讨论】:

  • 您能否提供一个示例代码,通过从数据库中提取日期来将 php 脚本添加到 cron 作业中
【解决方案2】:

您现在可以使用脚本在数据库中按日期检查差异日期,间隔 1 分钟或 1 秒或曾经运行脚本检查。例如通过 php:

public static function diff_time($date, $timezone = null,$now=null)
    {
        if($now == null)
        {
            $now =  date_create(date('Y-m-d H:i:s'));
            $now = new \DateTime(date('Y-m-d H:i:s'));
            $time_now = time();
        }
        if(is_int($date)){
            $time = $date;
            $date = date('Y-m-d H:i:s',$date);
        }
        else{
            $time = strtotime($date);
        }
        $date=new \DateTime($date);
        $diff=$date->diff($now);
        return $diff`;}

如果接近或零时间发送邮件,您可以使用检查差异时间。 现在你可以添加

crontab -e 

添加

` * * * * * php ~/script.php`

在示例中运行每次检查并发送邮件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 2014-04-12
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多