Basic Yii Framework extension that extends the SwiftMailer PHP email library (GitHub) with some useful Yii functions, and makes it easy to configure and access as a Component via Yii's app configuration system.
It's true, there are three other SwiftMailer extensions in the Yii Extensions repository right now. I had been using the classic mail one myself, for quite a while. Here is why I made another one, and what it does differently:
- Provides an easy way render emails using Views, no matter where they are (call view with full path alias for extensions, modules, etc)
- Turn off email sending config setting (useful for testing environments)
- Override all email sending to a specific address config setting (useful for testing environments, so you don't SPAM your users)
- Message debug logging config setting (full text of emails including headers is logged to the
/runtime/email
directory, good for testing and debugging)
Drop it in to your /extensions
directory.
(Make sure the /runtime/email
directory is writable for logging to work.)
<?php // components config array for your application
'components'=>array(
'mail'=>array(
'class' => 'ext.yii-swiftmailer2.SMail', // path to the extension
//'log' => false, // log emails to /runtime/email - - defaults to false
//'logDirectory' => 'email', // name of directory in the /runtime folder - defaults to 'email'
//'disableSend'=>false, // turn off email sending - defaults to false
//'overrideToAddress'=>'[email protected]', // override all email sent from system to this address, for testings/debugging
//'defaultTransport' => 'smtp', // defaults to 'mail'
//'transportOptions' => array(
//'host'=>'HOSTNAME',
//'username'=>'USERNAME',
//'password'=>'PASSWORD',
//'port'=>'465',
//'encryption'=>'ssl',
//),
),
),
?>
<?php
$message = Yii::app()->mail->newMessage(); // create a new email object
$message->addTo('[email protected]'); // to email address ( can be called multiple times )
$message->setFrom('[email protected]', 'Name of Address Owner'); // from email address
$message->setBody('Email body content.'); //
$message->setSubject('Email subject line!'); // set your subject line
if (!Yii::app()->mail->send($message)) { // send the message using the global transport config settings
Yii::log("Email send failed.", 'error', 'emailError');
}
?>
<?php
$message = Yii::app()->mail->newMessage(); // create a new email object
$message->addTo('[email protected]'); // to email address ( can be called multiple times )
$message->setFrom('[email protected]', 'Name of Address Owner'); // from email address
$message->setViewBody(
'application.views.mail.base', // declare the view to be called by renderPartial()
array(
'message'=>'Message text', // pass in your variables to the view like normal
'object'=>$object
)
);
$message->setSubject('Email subject line!'); // set your subject line
if (!Yii::app()->mail->send($message)) { // send the message using the global transport config settings
Yii::log("Email send failed.", 'error', 'emailError');
}
?>
Version: pre-beta :)
Author: Evan Johnson
http://splashlabsocial.com
Last updated: 1.20.2012