Bypassing Mail Settings

Hi there. I have a situation where I need my application to use a different mail server for a specific function. I need to be able to bypass the mail settings I have in the main.php configs. I’ve tried several different ways to do this but I can’t seem to get anything to work.

Basically in my main.php I have the mail config for ext.yii-mail.YiiMail. For all of the functions of the website, I need it to use those settings.

But for one specific function, I need to specify a completely different SMTP server along with a username and password.

Has anyone else ever had to do this?

We use the params file with different addresses for incoming mail, Yii::app()->params[‘emailAdmin’], …[emailSupport’], etc. Instead of bypassing, you would have two set ups in params and call the appropriate one when needed. Make sense?

But my config settings are under components, like so:




	'components'=>array(

		'mail' => array(

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

 			'transportType' => 'php',

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

 			'logging' => true,

 			'dryRun' => false

 		),

	),



And to support an external mail server I have to change it to this:




	'components'=>array(

		'mail' => array(

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

                        'transportType' => 'smtp',

                        'transportOptions' => array(

                                                'host' => '<myhost>',

                                                'username' => '<myuser>',

                                                'password' => '<mypassword>',

                                                'port' => '25',

                                                ),

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

 			'logging' => true,

 			'dryRun' => false

 		),

	),



Configure two mail components and use the appropriate one in your controller/service.




'components'=>array(

	'mailDefault' => array(

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

 		'transportType' => 'php',

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

 		'logging' => true,

 		'dryRun' => false

 	),

	'mailForASpecificUsecase' => array(

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

    	'transportType' => 'smtp',

    	'transportOptions' => array(

        	'host' => '<myhost>',

        	'username' => '<myuser>',

        	'password' => '<mypassword>',

        	'port' => '25',

    	),

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

 		'logging' => true,

 		'dryRun' => false

    ),

),



Sorry for the delay in my reply, but since I’m a new user it only allows me 2 posts per day :(

If I set the config up this way, how would I invoke the mail object? Right now I do it by

$message = new YiiMailMessage;

Both of those component configs are using the same class, so I’m not sure how I would call it.

I found an alternative where I can use createComponent inside my function, but I’d much rather have it in the config and call it separately like you suggested.

You can have multiple instances of the same application component class, with different configurations and component ids.

Don’t change anything in your code except


Yii::app()->mail->send($message);

to


Yii::app()->mailDefault->send($message);

or


Yii::app()->mailForASpecificUsecase->send($message);

as required (feel free to use much better/more descriptive component ids in your configuration).

The Application Components guide is a bit terse but you will figure it out with some practice.