Undefined property message thrown while trying to send mail within console app

Hi.

I’ve configured the Yii console app for sending periodic emails. Here is my console.php config file.




<?php


// This is the configuration for yiic console application.

// Any writable CConsoleApplication properties can be configured here.

return array(

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'My Console Application',

	'preload'=>array('log'),	


	'import'=>array(

		'application.models.*',

		'application.components.*',

		'ext.yii-mail.YiiMailMessage',

	),


	// application components

	'components'=>array(

		'db'=>array(

			'connectionString' => 'mysql:host=localhost;dbname=***********',

			'emulatePrepare' => true,

			'username' => '*****',

			'password' => '*****',

			'charset' => 'utf8',

		),

		'mail' => array(

		    'class' => 'ext.yii-mail.YiiMailMessage',

		    'transportType'=>'smtp',

		    'transportOptions'=>array(

		       'host'=>'smtpout.secureserver.net',

		       'username'=>'*******',

		       'password'=>'*******',

		      // 'port'=>'465',

		      // 'encryption'=>'ssl',

		     ),

		    'viewPath' => 'application.views.mail',

		    'logging' => true,

		    'dryRun' => false,

		),

		'log'=>array(

			'class'=>'CLogRouter',

			'routes'=>array(

				array(

					'class'=>'CFileLogRoute',

					'levels'=>'error, warning, info',

				),

				// uncomment the following to show log messages on web pages

				/*

				array(

					'class'=>'CWebLogRoute',

				),

				*/

			),

		),

	),

);

As you can see, I’m using yii-mail extension. The problem is that whenever I try to send the email, it keeps showing the following message:


PHP Error[8]: Undefined property: CConsoleApplication::$mail

    in file /opt/lampp/htdocs/sgiMarea/protected/extensions/yii-mail/YiiMailMessage.php at line 112

#0 /opt/lampp/htdocs/yii/framework/YiiBase.php(219): YiiMailMessage->__construct()

#1 /opt/lampp/htdocs/yii/framework/base/CModule.php(387): createComponent()

#2 /opt/lampp/htdocs/yii/framework/base/CModule.php(104): CConsoleApplication->getComponent()

#3 /opt/lampp/htdocs/sgiMarea/protected/extensions/yii-mail/YiiMailMessage.php(112): CConsoleApplication->__get()

#4 /opt/lampp/htdocs/yii/framework/YiiBase.php(219): YiiMailMessage->__construct()

#5 /opt/lampp/htdocs/yii/framework/base/CModule.php(387): createComponent()

#6 /opt/lampp/htdocs/yii/framework/base/CModule.php(493): CConsoleApplication->getComponent()

#7 /opt/lampp/htdocs/yii/framework/base/CApplication.php(146): CConsoleApplication->preloadComponents()

#8 /opt/lampp/htdocs/yii/framework/YiiBase.php(127): CConsoleApplication->__construct()

#9 /opt/lampp/htdocs/yii/framework/YiiBase.php(115): createApplication()

#10 /opt/lampp/htdocs/sgiMarea/console.php(13): createConsoleApplication()

Here’s the method snippet where fails:


/**

	* You may optionally set some message info using the paramaters of this 

	* constructor.

	* Use {@link view} and {@link setBody()} for more control.

	* 

	* @param string $subject

	* @param string $body

	* @param string $contentType

	* @param string $charset

	* @return Swift_Mime_Message

	*/

	public function __construct($subject = null, $body = null, $contentType = null, $charset = null) {

		Yii::app()->mail->registerScripts(); //Line 112

		$this->message = Swift_Message::newInstance($subject, $body, $contentType, $charset);

	}

For some reason, it cannot to access


Yii::app()->mail

,like the property is missing or something like that.

I’ve reviewed the file so many times, trying to figure out if I’ve missed something, but can’t make it work. I even read this post: http://www.yiiframework.com/forum/index.php/topic/22359-how-to-access-module-model-from-within-the-console-app/ since it’s very similar.

I would appreciate if you help me solving this error. Thanks in advance.

at the moment you access the app()->mail component in the constructor of the file, it doesn’t exists, because you try to reference it itself (makes sense ?)

So instead of:




public function __construct($subject = null, $body = null, $contentType = null, $charset = null) {

                Yii::app()->mail->registerScripts(); //Line 112

                $this->message = Swift_Message::newInstance($subject, $body, $contentType, $charset);

        }



You should have




public function __construct($subject = null, $body = null, $contentType = null, $charset = null) {

                $this->registerScripts(); //Line 112

                $this->message = Swift_Message::newInstance($subject, $body, $contentType, $charset);

        }



Having in mind that your class does have a registerScripts method.

Thanks for your quick response twisted1919. I was defining the component class in a wrong way.

I had the following code in my components array:




               'mail' => array(

                    'class' => 'ext.yii-mail.YiiMailMessage',

                    //ommited for simplicity

                ),



When the right thing is (according to documentation):




             'mail' => array(

		    'class' => 'ext.yii-mail.YiiMail',

	            //ommited for simplicity

		),



Geez, your answer gave me the insight I needed. Thanks a lot!