-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Version 1.1 - Added support for yii console applications - Added new examples - Updated README
- Loading branch information
Showing
6 changed files
with
149 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,32 +6,39 @@ Yii extension for sending emails with layouts using [PHPMailer](http://code.goog | |
|
||
* Based on latest PHPMailer (version 5.2.2 bundled) | ||
* Supports Yii layouts and translation | ||
* Supports web and console applications | ||
* Send full HTML emails with embeded images | ||
* Work with views like you usually do in Yii | ||
|
||
|
||
## Installation | ||
1. Copy YiiMailer folder to protected/extensions | ||
2. Add 'ext.YiiMailer.YiiMailer' line to your imports in main yii config | ||
2. Add 'ext.YiiMailer.YiiMailer' line to your imports in main and/or console yii config | ||
3. Copy mail.php config file to protected/config | ||
4. Create email layout file mail.php in protected/views/layouts/ (default path, can be changed in config) | ||
5. Create all the views you want to use in protected/views/mail/ (default path, can be changed in config) | ||
6. Put all images you want to embed in emails in images/mail/ (default path, can be changed in config) | ||
|
||
## Usage | ||
|
||
Instantiate YiiMailer in your controller controller and pass view and data array: | ||
Instantiate YiiMailer in your controller or console command and pass view and data array: | ||
<pre> | ||
$mail = new YiiMailer('contact', array('message' => $model->body, 'name' => $model->name, 'description' => 'Contact form')); | ||
$mail = new YiiMailer('contact', array('message' => 'Message to send', 'name' => 'John Doe', 'description' => 'Contact form')); | ||
</pre> | ||
or | ||
<pre> | ||
$mail = new YiiMailer(); | ||
$mail -> setView('contact'); | ||
$mail -> setData(array('message' => 'Message to send', 'name' => 'John Doe', 'description' => 'Contact form')); | ||
</pre> | ||
Layout is automatically set from config but you may override it with $mail->setLayout('layoutName') | ||
|
||
Render HTML mail and set properties: | ||
<pre> | ||
$mail->render(); | ||
$mail->From = $model->email; | ||
$mail->FromName = $model->name; | ||
$mail->Subject = $model->subject; | ||
$mail->From = '[email protected]'; | ||
$mail->FromName = 'John Doe'; | ||
$mail->Subject = 'Mail subject'; | ||
$mail->AddAddress(Yii::app()->params['adminEmail']); | ||
</pre> | ||
You may use all PHPMailer properties you would usually use. | ||
|
@@ -46,6 +53,6 @@ if ($mail->Send()) { | |
} | ||
</pre> | ||
|
||
## Example | ||
## Examples | ||
|
||
View included example with standard yii contact form. | ||
Two examples included: one for standard contact form in yii webb app and the other one for yii console app. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
class CronCommand extends CConsoleCommand | ||
{ | ||
public function getHelp() | ||
{ | ||
echo "Some cron job"; | ||
} | ||
|
||
public function run($args) | ||
{ | ||
//Do some cron processing... | ||
$cronResult="Cron job finished successfuly"; | ||
|
||
$mail = new YiiMailer(); | ||
//use "cron" view from views/mail | ||
$mail->setView('cron'); | ||
$mail->setData(array('message' => $cronResult, 'name' => get_class($this), 'description' => 'Cron job', 'mailer' => $mail)); | ||
//render HTML mail, layout is set from config file or with $mail->setLayout('layoutName') | ||
$mail->render(); | ||
//set properties as usually with PHPMailer | ||
$mail->From = '[email protected]'; | ||
$mail->FromName = 'Console application'; | ||
$mail->Subject = $cronResult; | ||
$mail->AddAddress('[email protected]'); | ||
//send | ||
if ($mail->Send()) { | ||
$mail->ClearAddresses(); | ||
echo 'Mail sent successfuly'; | ||
} else { | ||
echo 'Error while sending email: '.$mail->ErrorInfo; | ||
} | ||
echo PHP_EOL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
// This is the configuration for yiic console application. | ||
// Any writable CConsoleApplication properties can be configured here. | ||
return array( | ||
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', | ||
'name'=>'My Console Application', | ||
'import'=>array( | ||
'ext.YiiMailer.YiiMailer', | ||
), | ||
// application components | ||
'components'=>array( | ||
'db'=>array( | ||
'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db', | ||
), | ||
// uncomment the following to use a MySQL database | ||
/* | ||
'db'=>array( | ||
'connectionString' => 'mysql:host=localhost;dbname=testdrive', | ||
'emulatePrepare' => true, | ||
'username' => 'root', | ||
'password' => '', | ||
'charset' => 'utf8', | ||
), | ||
*/ | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<p>Message from <?php echo $name . ': ' . $message ?></p> | ||
|
||
<p>You may change the content of this page by modifying the following two files:</p> | ||
<ul> | ||
<li>View file: <tt><?php echo __FILE__; ?></tt></li> | ||
<li>Layout file: <tt><?php echo $mailer->getViewFile($mailer->getLayoutPath() . '.' . $mailer->getLayout()); ?></tt></li> | ||
</ul> |