dpsmailer

Yii extension for sending emails
7 followers

dpsMailer

Yii extension for sending emails

Features

  • comfortable interface for testing logics of emails sending
  • email templates just like page templates can have layouts
  • possibility to send emails via Swift Mailer [ http://swiftmailer.org/ ] and PhpMailer [ https://github.com/Synchro/PHPMailer ]

Installation

  1. Download archive.
  2. Extract files in the directory protected/extensions/dpsmailer of the Yii project.

Config

Put the below code in the configuration file in the 'components' section:

'dpsMailer' => array(
    'class' => 'ext.dpsmailer.components.dpsMailer',
    'sViewPath' => '[/path/to/mail/views]',
    'aFrom' => array( '[from@example.com]' => '[from]' ),
    'aBehaviors' => array(
        'swift' => array(
            'class' => 'ext.dpsmailer.behaviors.dpsSwiftMailerBehavior',
            'sLibPath'=> '[/path/to/Swift/Swift-5.0.0/lib]',
            'sTransport' => 'Swift_SmtpTransport',
            'aOptions' => array(
                'Host'          => 'smtp.gmail.com',
                'Port'          => 465,
                'Encryption'    => 'ssl',
                'Username'      => '[username]',
                'Password'      => '[password]',
            ),
        ),
    ),
)

Usage

Sending email:

Yii::app()->dpsMailer->sendByView(
    array( 'to@example.com' => 'Username' ),
    'emailTpl', // template email view 
    array(
        'sUsername' => 'Username',
        'sLogoPicPath' => '/path/to/logo.gif',
        'sFilePath' => '/path/to/attachment.txt',
    )
);

Email view:

<?
// /path/to/mail/views/emailTpl.php
 
/** @var dpsEmailController $this */
 
$this->setSubject( 'For ' . $sUsername );
$this->setLayout( 'emailLayoutTpl' );
$this->attach( $sFilePath ); ?>
 
 
Hello <?= $sUsername ?>!

Email layout:

<?
// /path/to/mail/views/emailLayoutTpl.php
 
/** @var dpsEmailController $this */ ?>
 
 
<img src="<?= $this->embed( $sLogoPicPath ) ?>"/>
 
<?= $content ?>

Extended variants of the usage:

Saving emails as files

// in config
 
'aBehaviors' => array(
    'file' => array(
        'class' => 'ext.dpsmailer.behaviors.dpsFileMailerBehavior',
        'sDir'  => '[/path/for/save/eml]'
    ),
),

Saving emails in the memory

// in config
 
'aBehaviors' => array(
    'memory' => array(
        'class' => 'ext.dpsmailer.behaviors.dpsMemoryMailerBehavior',
    ),
),

Getting return email objects from tests:

$oEmailEvent = Yii::app()->dpsMailer->getBehavior( 'memory' )->getLastEmail( 'to@example.com' );
$oEmailEvent = Yii::app()->dpsMailer->getBehavior( 'memory' )->getLastEmails();

Saving emails in cache

It allows addressing to the sent emails from another php process, for example from functional tests.

// in config
 
'aBehaviors' => array(
    'cache' => array(
        'class' => 'ext.dpsmailer.behaviors.dpsCacheMailerBehavior',
    ),
),

Dynamic definition of the behavior

Yii::app()->dpsMailer->setSendersEnable( array() );
 
// dynamically we add behavior of emails saving in the memory
Yii::app()->dpsMailer->attachBehavior( array(
    'preview' => array(
        'class' => 'ext.dpsmailer.behaviors.dpsMemoryMailerBehavior'
    ),
) );
 
Yii::app()->dpsMailer->sendByView( ... );
 
// Getting the object with email data 
$oEmailEvent = Yii::app()->dpsMailer->getBehavior( 'preview' )->getLastEmail();

Balancing between behaviors

// in config
 
'dpsMailer' => array(
        …,
        'aBehaviors' => array(
            'mailer1' => array( ... ),
            'mailer2' => array( ... ),
            'mailer3' => array( ... ),
        ),
        'cBalance' =>function( $aSenders, dpsEmailEvent $oEmailEvent ) {
            // distributing load (having information about 'senders' load, we can make full balancer )
            $iNum = mt_rand( 1, 3 );
            $aSenders[ 'mailer1' ] = $iNum == 1;
            $aSenders[ 'mailer2' ] = $iNum == 2;
            $aSenders[ 'mailer3' ] = $iNum == 3;
            return $aSenders;
        },
),

Sending emails from CConsoleApplication:

// in console config
return array(
    ...,
    'behaviors' => array ( 'ext.dpsmailer.components.dpsRenderConsoleBehavior' ),
    'components' => array( ... ),
    ...
);

Sending an email with extra parameters:

// special object for extra parameters passing 
$oForceEmailEvent = new dpsEmailEvent();
$oForceEmailEvent->aCc = array( 'cc@example.com' => '' );
 
Yii::app()->dpsMailer->sendByView(
    array( 'to@example.com' => 'to' ),
    'emailTpl',
    array( 'sUsername' => 'username' ), 
    $oForceEmailEvent // object with extra parameters
);

Total 1 comment

#14991 report it
Gero at 2013/09/26 05:10pm
Send mail from console

Great extension!

I had to tweak console config a bit to send mails from console...

When trying to send mail from console:

Fatal Error: Call to a member function render() on a non-object in /Yii/1.1.13/web/CController.php on line 351 The line 351 of CController attempts to call Yii::app()->getClientScript()->render($output) on the clientScript component returned by dpsRenderConsoleBehavior:22

To allow CConsoleApplication to create a CClientScript, its required to add clientScript component in console config:

// in console config
return array(
    ...,
    'behaviors' => array ( 'ext.dpsmailer.components.dpsRenderConsoleBehavior' ),
    'components' => array( 
        'clientScript'=>array('class'=>'system.web.CClientScript'),
        //... ),
    ...
);

Leave a comment

Please to leave your comment.

Create extension
Downloads