【问题标题】:Yii: console cron application is not sending emailYii:控制台 cron 应用程序不发送电子邮件
【发布时间】:2015-01-13 13:52:41
【问题描述】:

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

受保护的/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();

【问题讨论】:

    标签: php yii


    【解决方案1】:

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

    【讨论】:

    • 就我而言,cron.php 是控制台应用程序的配置文件。请参阅我的问题。
    • 没有错误,只在 Yii::app()->mail->send($message) 线上执行冻结
    • 这一行的目的是什么:if (Yii::app()->mail->send($message) > 0. 我在打电话所以不清楚
    • 检查邮件发送成功与否,方法send返回一些数量
    • 好的。我在 Google 代码上看不到代码。但是在 if send 语句和 else 子句之后并尝试检索发送错误。我不使用那个邮件客户端。但这通常是生命 if($mail->send()) { echo 'sent';}else{ echo $mail->getErrors();}
    猜你喜欢
    • 2012-09-15
    • 2013-09-22
    • 1970-01-01
    • 2018-11-15
    • 2013-11-23
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    相关资源
    最近更新 更多