使用 crontab 在 yii 中添加剩余电子邮件

2022-01-04 00:00:00 email crontab php yii

按照建议,我在 protected/commands 下创建了一个 File MessengerCommand.php 作为

as suggested I created a File MessengerCommand.php under protected/commands as

class MessengerCommand extends CConsoleCommand
{
    public function run($args)
    {
        /* if(ERunActions::runBackground())
        { */

       $mail=Yii::app()->Smtpmail;
        $mail->SetFrom("tsadmin@softthink.com", 'From NAme');
        $mail->Subject    ="hello";
        $mail->MsgHTML("haiii workd");
        $mail->AddAddress("rajesh.udutha@itaugments.com", "");
        if(!$mail->Send()) {
            echo "Mailer Error: " . $mail->ErrorInfo;
        }else {
            echo "Message sent!";
        }
}
}

并添加 yiic 命令为

$path = dirname(__FILE__);
//echo $path;
shell_exec( $path . "/protected/yiic messenger" );

它会在我加载网站时触发电子邮件....

and it will trigger email when I load the site ....

但我不想刷新网站..我需要让它在后台运行..请帮助我.

but I dont wanna refresh the site ..I need to make this to run in background ..Please help me.

推荐答案

您可以使用 yii 控制台应用程序 来完成你的任务.

You can use yii console applications to accomplish your task.

protected/commands中创建一个带有Command后缀的新文件,例如:MessengerCommand.php:

In protected/commands create a new file with Command sufix, for example: MessengerCommand.php:

<?php
class MessengerCommand extends CConsoleCommand
{
.......

在班级 MessengerCommand 你有几个创建选项命令动作.在此示例中,我们将覆盖 run 方法:

In class MessengerCommand you have several options to create the command action. In this sample we will override run method:

public function run($args)
{
        $birth_month = date("m");
        $birth_day = date("d");
        $criteria = new CDbCriteria;
        $criteria->condition = "birth_month = $birth_month and birth_day = $birth_day";
        $listScheduledRecords = Table::model()->findAll($criteria);
        foreach($listScheduledRecords as $scheduled_record):
            $this->send($scheduled_record);
        endforeach;
}

public function send($scheduled_record)
{
    ....
    your logic to send your email
    ....
}

protected 目录下,创建一个文件:messenger.php.该文件将作为命令执行器:

In protected dir, create a file: messenger.php. This file will be the command executer:

<?php
$path = dirname(__FILE__);
$output = shell_exec( $path . "/./yiic messenger" );
echo $output;

要在 Linux/Unix 上进行测试,请在控制台/终端中运行:

To test it, on Linux/Unix, run in console/terminal:

cd /.../.../...your_protected_path 
php messenger.php

要在 Windows 上测试,您需要参考您的 php.exe 位置路径或在您的系统环境变量中设置 php.exe 和 在 Windows 上使用 yiic 等价

To test on Windows, you need to refer to your php.exe location path or set php.exe on your system environment variables and use yiic equivalence for Windows

要安排自动任务,在此示例中,每日执行,在 Linux/Unix 上,您可以使用 cron 作业:

To schedule automatic task, in this sample, daily execution, on Linux/Unix, you can use cron jobs:

在控制台/终端:

crontab -e

在 cron 文件中,添加计划任务,每天,在 9h00.记住 cron 语法:#minute hour day of month month day of week 命令

In cron file, add the scheduled task, daily, at 9h00. Remember cron sintax: # minute hour day of month month day of week command

0   9  *  *  * php /full_path/protected/messenger.php

保存文件并退出.

要在 Windows 上安排自动任务,参考到他们在 Internet 上的文档/帮助.

To schedule automatic task on Windows, refer to their docs / help on Internet.

如果有错误,Yii Console应用程序使用自己的配置文件(protected/config/console.php).常见的错误是protected/config/console.php中的db连接、组件、模块错误.

If you have errors, Yii Console applications use their own config file (protected/config/console.php). Common mistakes are wrong db connection, components, modules in protected/config/console.php.

相关文章