Calling a function in a different Controller

Hey guys, sorry if this has already been answered but I cannot seem to find it. I have a project going here and I am reusing the email call quite alot so I decided to try put it in the following function which is located in the

TestController class


 public function email($message,$sendTo,$subject){

                    require Yii::getPathOfAlias('application.extensions') . '/PHPMailer_v5.1/class.phpmailer.php';

                    $mail = new PHPMailer(true);

                    $mail->IsSMTP();                           // tell the class to use SMTP

                    $mail->SMTPAuth = true;                  // enable SMTP authentication

                    $mail->Port = 25;                    // set the SMTP server port

                    $mail->Host = "test.net"; // SMTP server

                    $mail->Username = "test.com";      // SMTP server username

                    $mail->Password = "test";            // SMTP server password

                    $mail->Mailer = "smtp";

                    $mail->From = 'paul@test.com';

                    $mail->FromName = 'Paul';

                    $mail->AddAddress($sendTo);

                    $mail->Subject = $subject;

                    $body = $message;

                    $mail->MsgHTML($body);

                    $mail->IsHTML(true); // send as HTML

                    $mail->Send();

    }

Its called succellfully from a function in that controller like so




    public function actionTest() {

                    $message = 'Test data';

                    $sendTo = 'whoever@hotmail.co.uk';

                    $subject = 'testing123';


                    $this->email($message, $sendTo, $subject);

}



However what im trying to do is call it from a different controller entirely, for example

TestController2 class


 $message = 'More test dara';

       $subject='Added As Participant in a contract';

       $sendTo = $model->Email;


                            

       $email = new TestController();

       $email->email($message, $sendTo, $subject);


       //Ive also tried

       //TestController::email($message, $sendTo, $subject);



However neither works, the erro i get is as follows

include(TestController.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory

and stack trace directs me to this part of the code

$email = new TestController();

Any help with this problem would be much appreciated, Thank you

For share code among controllers, create a widget.

Create a file in compoments named emailer:


class emailer extends CWidget

{

  public $message;

  publi $sendTo;

  public $subject;

   public function run()

   {

       require Yii::getPathOfAlias('application.extensions') . '/PHPMailer_v5.1/class.phpmailer.php';

                    $mail = new PHPMailer(true);

                    $mail->IsSMTP();                           // tell the class to use SMTP

                    $mail->SMTPAuth = true;                  // enable SMTP authentication

                    $mail->Port = 25;                    // set the SMTP server port

                    $mail->Host = "smtp.reg365.net"; // SMTP server

                    $mail->Username = "aontu.com";      // SMTP server username

                    $mail->Password = "aontu";            // SMTP server password

                    $mail->Mailer = "smtp";

                    $mail->From = 'paul@aontu.com';

                    $mail->FromName = 'Paul';

                    $mail->AddAddress($this->sendTo);

                    $mail->Subject = $this->subject;

                    $body = $this->message;

                    $mail->MsgHTML($body);

                    $mail->IsHTML(true); // send as HTML

                    $mail->Send();

   }

}

Now you can call this widget as: $this->widget(‘emailer’, array(…))

If a widget is too much in your opinion, simply create a class in component Helper, and create a static method email here.

You can access this method with Helper::email();

Excellent thanks very much Zaccharia

… or you can also create a "component" (inherit from CApplicationComponent) for instance. Advantage is that you can intialize it from the Yii config file (like any other component).

… or you may also have a look at this extension …

8)

You can also create a behavior which is attached to whichever class you desire, making that function available.

Or have a central Controller (extends CController) file that holds the email function, and have your actual controllers extend from Controller.