Implementing a Daily Birthday SMS Notification Command in ThinkPHP
This guide walks through creating a ThinkPHP command that sends birthday SMS messages each day, detailing folder setup, PHP code, command registration, and crontab scheduling with testing instructions.
The author explains how to implement a daily birthday SMS notification feature that runs via a scheduled script using ThinkPHP.
Step 1: Create a command folder under app/admin (or the appropriate module) and add a SendMessage.php file containing the following code:
<?php
namespace app\admin\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;
class SendMessage extends Command
{
protected function configure(){
$this->setName('SendMessage')->setDescription("计划任务 SendMessage");
}
// When this class is called, the execute method runs automatically
protected function execute(Input $input, Output $output){
$output->writeln('Date Crontab job start...');
/*** 这里写计划任务列表集 START ***/
$this->birthday(); // send SMS
/*** 这里写计划任务列表集 END ***/
$output->writeln('Date Crontab job end...');
}
// Get employees whose birthday is today and send SMS
public function birthday(){
echo '这里写你要实现的逻辑代码';
}
}Step 2: Register the command by adding it to app/command.php: return ['app\admin\command\SendMessage']; Step 3: Set up a crontab job. Example commands:
crontab -l // list current jobs
crontab -e // edit to add a new job
crontab -r // remove all jobsFor testing, you can schedule the command to run every minute and log its output:
*/1 * * * * php /www/wwwroot/tool/think SendMessage >> /www/wwwroot/tool/runtime/message/2019.log 2>&1
# Monitor the log
tail -f /www/wwwroot/tool/runtime/message/2019.logFor the full article and additional screenshots, visit the original link provided at the end of the page.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
