You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
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.
Import Class
Import class as below in main.php
'import'=>array(
'application.models.*', 'application.components.*', 'ext.yii-mail.YiiMailMessage', ),
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
:/
@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!
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
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.
Swiftmailer update
Maybe an idea to update Swiftmailer? The version used in this extension is from 2010.
http://swiftmailer.org/download
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 ""
Solved: issue #15396
please try by adding
//'encryption'=>'ssl',
in main.php setings
2 minutes to send a mail
In my case it takes two minutes to process all shipping. At the end of the two minutes I get the mail but is time consuming.
What could it be?
Validate SMTP connection with credentials
I'm trying to validate the connection before sending the email. But, unable to find appropriate method to validate. Please help me in this.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.