[英]how to send mail on specified date and time using mysql and php
我所擁有的是
必填
我正在使用本地 apache 服務器
示例:A 想在 5 天內向 B 發送郵件。 A 在數據庫中輸入日期和時間,5 天后它將通過運行 php 郵件程序腳本自動發送。
有沒有辦法安排這個?
編寫一個函數從數據庫中獲取發送者和接收者列表以執行日期。
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
您可以使用腳本來檢查差異日期,現在按數據庫中的日期間隔 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`
通過在示例中運行每次檢查和發送郵件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.