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

To add remainder email in yii using crontab

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

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" );

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

但我不想刷新网站..我需要在后台将其设为 运行..请帮助我。

windows相当于cron job是定时任务。

可以使用带有 schtasks 的命令行创建计划任务

一个例子:

schtasks /create /tn calculate /tr calc /sc weekly /d MON /st 06:05 /ru "System"

您可以使用 yii console applications 来完成您的任务。

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

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

在 class MessengerCommand you have several options to create the command action 中。在此示例中,我们将覆盖 run 方法:

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。该文件将成为命令执行者:

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

要测试它,在 Linux/Unix、console/terminal 中的 运行:

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

要在 Windows 上进行测试,您需要参考您的 php.exe 位置路径或在您的系统环境变量和 use yiic equivalence for Windows

上设置 php.exe

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

在console/terminal中:

crontab -e

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

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

保存文件并退出。

在 Windows、refer to their docs / help on Internet 上安排自动任务。

如有错误,Yii Console applications use their own config file (protected/config/console.php)。常见错误是 protected/config/console.php.

中的数据库连接、组件、模块错误