简体   繁体   English

php 脚本在一天、一周、2 周后通过 cron 作业向注册用户发送电子邮件

[英]php script to send emails by cron job after a day , week ,2 weeks to the registred users

<?php

global $wpdb;
$sql = $wpdb->get_results("SELECT * FROM wp_users");
$date = date_create("2013-03-15");
$reminderSent = false; // Get this value from the db (true or false) $expiryActioned = false; // Get this value from the db (true or false) //if( is_array( $sql ) && count( $sql ) > 0 ) {
if (!$sql) {
    throw new My_Db_Exception('Database error: ' . mysql_error());
}
foreach ($sql as $sq) {
    $regusers = $sq->user_registered;
    // $ID=$sq->ID; date_add($date,date_interval_create_from_date_string("1 days"));
    //echo date_format($date,"Y-m-d"); echo $re=date($regusers);
    if ($regusers == date('Y-m-d', strtotime("+1 day")) || $regusers == date('Y-m-d', strtotime("+2 day")) || $regusers == date('Y-m-d', strtotime("+1 month"))) {
        echo "Hello";
        $expiringDate = date('Y-m-d', strtotime($sq['user_registred']));
    }
    $todayDate = time();
    $reminderDate = strtotime($expiringDate);
    if ($todayDate == $reminderDate && $reminderSent == false && $expiryActioned == false) {
        echo "Sending"; // Send mail     $reminderSent = "true";
        $expiryactioned = "true";
        $update = mysql_query("UPDATE `wp_users` SET reminder_sent='$reminderSent', expiry_actioned='$expiryactioned' ");
    } elseif ($todayDate != $reminderDate && $reminderSent == false && $expiryActioned == true) { //    echo "Email is sent already ";
    }
}
//echo date('Y-m-d',strtotime("+1 day" , $todaysdate));
//}

I want to run the cron job in server and emails should be sent to the users after the day of registration , a week , 2 weeks.我想在服务器中运行 cron 作业,并且应在注册日、一周、两周后向用户发送电子邮件。 How can I write the script to call cron job.如何编写脚本来调用 cron 作业。

Firstly create a php script that checks how long has each user been registered.首先创建一个 php 脚本来检查每个用户注册了多长时间。 If it's first day, a week or second week of a user then send mail using mail() function or use any php mail library like swiftmailer.如果是用户的第一天、一周或第二周,则使用 mail() 函数或使用任何 php 邮件库(如 swiftmailer)发送邮件。

Then run that php script everyday by assigning to crontab.然后通过分配给 crontab 每天运行该 php 脚本。 Here's crontab config to run script "At 00:00 every day."这是用于运行脚本“每天 00:00”的 crontab 配置。 http://crontab.guru/#0_0_ _ _* http://crontab.guru/#0_0_ _ _*

{   

    $url = "set email function url";
    $cSession = curl_init(); 
    curl_setopt($cSession,CURLOPT_URL,$url);
    curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($cSession,CURLOPT_HEADER, false); 
    $result=curl_exec($cSession);
    curl_close($cSession);
    echo $result;

}

after that save this file and set that file path on hosting server cron job section i hope this code will you help thanks之后保存此文件并在托管服务器 cron 作业部分设置该文件路径我希望此代码对您有所帮助,谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM