7

I am trying to use a custom class I have created to send out mail so I can keep the controller files thin. I created my custom class and put it in the components folder. I then added:

'sendMail' => array(
    'class'=>'application.components.SendMail.',
), 

underneath main components in my main config file.

This should allow me to access the class directly correct? I try using:

Yii::app()->SendMail->MailerConfirmation();

and:

Yii:app()->MailerConfirmation();

and all I end up with is errors.

Can anyone tell me how I include a custom component? Maybe I am doing this all wrong?

trejder
  • 17,148
  • 27
  • 124
  • 216
KyleVan
  • 501
  • 4
  • 9
  • 18

2 Answers2

12

First it should be:

'sendMail' => array(
    'class'=>'application.components.SendMail',
), 

Notice the absence of dot in the end "SendMail" instead of "SendMail.". Also, this configuration expects that you have php file SendMail.php, in protected/components directory that is a class with name "SendMail" and that this component extends CApplicationComponent. The component id will be with lower first letter, eg Yii::app()->sendMail and this will return instance of "SendMail" class. I do not know what MailerConfirmation is, but if this is a method of SendMail object, then you should access it like Yii::app()->sendMail->MailerConfirmation()

If this doesn't help, then please post some code and post the errors you are getting.

ddinchev
  • 33,683
  • 28
  • 88
  • 133
2

Note that if you are not going to set any component properties in the config file or use other CApplicationComponent features and your config file includes the default:

'import'=>array(
    'application.models.*',
    'application.components.*',
),

You can put your SendMail.php class in the components directory and it will autoload by calling it via:

$mailer = new SendMail();

then call your methods via:

$mailer->MailerConfirmation();

if you do want to use CApplicationComponent, you may want to look here or here for a couple examples, as well as the Yii tutorials.

ldg
  • 9,112
  • 2
  • 29
  • 44