Search code examples
phpyiipear

How to include PEAR's mail and SMTP functions with YII?


I have set up pear, and previously got stuck at:

require_once('Mail.php');

I managed to resolve this by fixing the paths in PHP.ini, but now YII is complaining:

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

I don't know which libary to install to get PEAR to pick up LOGIN.php, if that's the problem. It could also be that YII is not allowing the import of LOGIN.PHP because it might have its own? I'm grabbing at straws though. Any ideas?


Solution

  • YiiMail is an availble extension to send mails with or without smtp, This is an emailing extension that wraps SwiftMailer. This extension also allows you to create emails from view files. download this from here

    In your config file, include below code in component section

    'mail' => array(
                    'class' => 'application.extensions.yii-mail.YiiMail',
                    'transportType'=>'smtp',
                    'transportOptions'=>array(
                        'host'=>'smtp.googlemail.com',
                        'username'=>'[email protected]',//
                        'password'=>'passwd',
                        'port'=>'465',
                        'encryption'=>'ssl',
                    ),
                    'viewPath' => 'application.views.mail',
                    'logging' => true,
                    'dryRun' => false
            ),
    

    And in controller action section use something like below

    $message = new YiiMailMessage;
    $message->view = 'registrationFollowup';
    
    //userModel is passed to the view
    $message->setBody(array('userModel'=>$userModel), 'text/html');
    
    
    $message->addTo($userModel->email);
    $message->addBcc('[email protected]');
    $message->from = Yii::app()->params['adminEmail'];
    Yii::app()->mail->send($message);
    

    the view registrationFollowup resides in mail folder inside views folder, the view path is understood from the config file ('viewPath' => 'application.views.mail')