Backend Development 3 min read

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.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Implementing a Daily Birthday SMS Notification Command in ThinkPHP

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:

<code>&lt;?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 '这里写你要实现的逻辑代码';
    }
}
</code>

Step 2: Register the command by adding it to app/command.php :

<code>return ['app\admin\command\SendMessage'];</code>

Step 3: Set up a crontab job. Example commands:

<code>crontab -l   // list current jobs
crontab -e   // edit to add a new job
crontab -r   // remove all jobs</code>

For testing, you can schedule the command to run every minute and log its output:

<code>*/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.log</code>

For the full article and additional screenshots, visit the original link provided at the end of the page.

cronSMSThinkPHPCommand
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.