Yii:console cron应用程序没有发送电子邮件

时间:2015-01-13 13:52:42

标签: php yii

我的控制台cron应用程序未发送电子邮件。邮件发送与web-app完美配合。我应该配置一些选项还是你给我正确的方向?

保护/ cron.php:

<?php
 require_once(dirname(__FILE__) . '/../components/helpers.php');
 return array(
'basePath'   => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
'name'       => 'Cron',
'preload'    => array('log'),
'import'     => array(
    'application.models.*',
    'application.components.*',
),
'components' => array(
    'cache'        => array(
        'class' => 'system.caching.CFileCache'
    ),

    'mail'         => array(
        'class'         => 'ext.yii-mail.YiiMail',
        'transportType' => 'php',
        'viewPath'      => 'application.views.mail',
        'logging'       => false,
        'dryRun'        => false,
    ),        
),
'params'     => array(
    'adminEmail' => 'some_email@he.re',
),

);

我的模型上的电子邮件发送方法:

public function sendEmail()
    {
        if ($this->mail) {
            if ($this->user->email) {
                try {
                    $notification = $this->notification;
                    Yii::import('ext.yii-mail.YiiMailMessage');
                    $params           = array('notification' => $notification);
                    $message          = new YiiMailMessage;
                    $message->view    = "notification";
                    $message->subject = __('MyApp: title', array(':title' => $notification->title));
                    $message->from    = Yii::app()->params['adminEmail'];

                    $message->setBody("dasdasd");
                    $message->setTo($this->user->email);

                    if (Yii::app()->mail->send($message) > 0) {
                        $cmd = Yii::app()->db->createCommand();
                        $cmd->update($this->tableName(), array('mail' => 1), "id={$this->id}");
                        print_r($this->getAttributes());
                    }
                } catch (Exception $e) {
                    Yii::log($e->getMessage(), CLogger::LEVEL_ERROR);
                }
            }
        }
    }

web-root上的cron.php:

<?php
defined('YII_DEBUG') or define('YII_DEBUG',true);
require_once('../yii/framework/yii.php');

$configFile='protected/config/cron.php';
Yii::createConsoleApplication($configFile)->run();

1 个答案:

答案 0 :(得分:1)

您还必须在控制台配置文件中配置邮件组件。控制台和Web视图具有自己的配置文件。在Yii版本1.1.xx中,这是main.php。它控制Web应用程序设置。控制台还有一个配置文件 - console.php。