Yii 1.1: Send mail using YiiMail extension

16 followers

Introduction ΒΆ

Let me show a simpe example to send mail using YiiMail extension in 5 steps. It also includes any template view if required along with the content.

Step1: Download the extension from path 'http://code.google.com/p/yii-mail/downloads/list' .

Step 2: Place the extracted folder inside the extensions folder.

Step 3: Modify the config file main.php like below,

'mail' => array(
                'class' => 'ext.yii-mail.YiiMail',
                'transportType'=>'smtp',
                'transportOptions'=>array(
                        'host'=>'<hostanme>',
                        'username'=>'<username>',
                        'password'=>'<password>',
                        'port'=>'25',                       
                ),
                'viewPath' => 'application.views.mail',             
        ),

Hint: The view path points to '/protected/views/mail'.

Step 4: Import the extension in the main.php file .

'ext.yii-mail.YiiMailMessage',

Step 5: Controller function to send the mail.

public function SendMail()
    {   
        $message            = new YiiMailMessage;
           //this points to the file test.php inside the view path
        $message->view = "test";
        $sid                 = 1;
        $criteria            = new CDbCriteria();
        $criteria->condition = "studentID=".$sid."";            
        $studModel1          = Student::model()->findByPk($sid);        
        $params              = array('myMail'=>$studModel1);
        $message->subject    = 'My TestSubject';
        $message->setBody($params, 'text/html');                
        $message->addTo('yourmail@domain.com');
        $message->from = 'admin@domain .com';   
        Yii::app()->mail->send($message);       
    }
 test.php file
<html>
        <head>
        </head>
        <body>
            Dear <?php 
             echo $myMail->studentName;
             ?>
                <br>This is a test mail.
        </body>
       </html>

That's it. Run the code and mail is sent.

Total 8 comments

#18127 report it
KoushikSantra at 2014/09/13 09:46am
Solved: issue #15396

please try by adding

//'encryption'=>'ssl',

in main.php setings

#15396 report it
sirin k at 2013/11/05 10:30pm
im getting an error

im getting an error when tried to send a mail through my gmail SMTP account.

Expected response code 220 but got code "", with message ""
#15392 report it
Davey at 2013/11/05 05:45am
Swiftmailer update

Maybe an idea to update Swiftmailer? The version used in this extension is from 2010.

http://swiftmailer.org/download

#13914 report it
Trejder at 2013/07/05 09:20am
Re: full stack trace

From a very quick look at your code, I can clearly see, that your application "dies" (timeout) on fsockopen() function. This is PHP (not Yii, not YiiMail) function, a low-level system function, which is responsible for opening a socket - i.e. initiating Internet connection. Read more here.

If your application ends here, then there is 99,99% that your problem lies outside your code (outside even PHP) and it is most likely caused by a wrong configuration of your computer (server).

Either search the Internet on how to properly configure your computer / development server to send e-mails directly from it. Or upload your file to some production server, where e-mail client and functions are correctly configured and e-mail sending works. And test e-mail related functions only there.

For example, I'm using XAMPP as my development server, to run PHP and my Yii-apps. It has fake e-mail program configured, so I can test sending e-mails directly from my windows machine / development server. But they did this fake e-mail configuration on one of last versions of XAMPP. When I was using older one, where e-mail client on Windows was not working, I also wasn't able to send e-mails and I had to test these functions on production server only.

Anyway -- there isn't much we can do, as in my opinion source of your problem is not in YiiMail extension, Yii framework or even PHP language, but in your local web server configuration. That's all.

#13913 report it
onchomngebul at 2013/07/05 09:09am
full stack trace

its full stack trace of my error, sorry if it is too long..

Time Memory Function Location

1 0.0011 145024 {main}( ) ..\index.php:0 2 0.0239 1103560 CApplication->run( ) ..\index.php:13 3 0.0239 1104168 CWebApplication->processRequest( ) ..\CApplication.php:169 4 0.0263 1223808 CWebApplication->runController( ) ..\CWebApplication.php:141 5 0.0340 1556064 CController->run( ) ..\CWebApplication.php:282 6 0.0349 1584384 CController->runActionWithFilters( ) ..\CController.php:265 7 0.0366 1635720 CFilterChain->run( ) ..\CController.php:291 8 0.0368 1636872 CInlineFilter->filter( ) ..\CFilterChain.php:130 9 0.0368 1636960 CController->filterAccessControl( ) ..\CInlineFilter.php:58 10 0.0378 1691256 CFilter->filter( ) ..\CController.php:1145 11 3.2664 1886784 CFilterChain->run( ) ..\CFilter.php:40 12 3.2664 1886784 CController->runAction( ) ..\CFilterChain.php:133 13 3.2664 1886856 CInlineAction->runWithParams( ) ..\CController.php:308 14 3.2664 1887200 SiswaController->actionKirimemail( ) ..\CInlineAction.php:49 15 4.4043 4371112 YiiMail->send( ) ..\SiswaController.php:207 16 4.4272 4664568 Swift_Mailer->send( ) ..\YiiMail.php:133 17 4.4272 4664688 Swift_Transport_AbstractSmtpTransport->start( ) ..\Mailer.php:74 18 4.4295 4675432 Swift_Transport_StreamBuffer->initialize( ) ..\AbstractSmtpTransport.php:101 19 4.4295 4675400 Swift_Transport_StreamBuffer->_establishSocketConnection( ) ..\StreamBuffer.php:70 20 4.4295 4675640 fsockopen ( ) ..\StreamBuffer.php:233 21 34.4298 4677552 CApplication->handleError( ) ..\StreamBuffer.php:233

#13906 report it
Trejder at 2013/07/05 03:26am
@onchomngebul

Presented file and line number are not from extension, only from core framework and is responsible for displaying error message itself. It tells us nothing. Show us full stack trace, so we can try to help you!

#13901 report it
onchomngebul at 2013/07/04 11:49pm
need help

i got this error..

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Users\OnChomNgeBul\Dropbox\Final Round Bucho\code\yii\framework\base\CApplication.php on line 765

:/

#13573 report it
buminda at 2013/06/07 12:47am
Import Class

Import class as below in main.php

'import'=>array( 'application.models.', 'application.components.', 'ext.yii-mail.YiiMailMessage', ),

Leave a comment

Please to leave your comment.

Write new article
  • Written by: RKA
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +8
  • Viewed: 45,551 times
  • Created on: Mar 19, 2013
  • Last updated: Mar 19, 2013
  • Tags: YiiMail