Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Yii Mailer
PHP
branch: master
Failed to load latest commit information.
src/MailerYii Reorganize files
.gitignore Add composer.json
README.md Update README.md
composer.json Update composer.json

README.md

Mail

Integration of the DDD Mail Component for Yii 1.1

Installation

Using Composer, add repository and require the package:

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/slavcodev/yii-mail.git"
        },
        {
            "type": "package",
            "package": {
                "name": "ddd-php/ddd-components",
                "version": "0.1",
                "source": {
                    "type": "git",
                    "url": "https://github.com/slavcodev-social/ddd-components.git",
                    "reference": "master"
                },
                "autoload": {
                    "psr-0": {
                        "Ddd": "src/"
                    }
                }
            }
        }
    ],
    "require": {
        "swiftmailer/swiftmailer": "*",
        "ddd-php/ddd-components": "*",
        "slavcodev/yii-mail": "dev-master"
    }
}

Use GIT submodules, add both repository to vendor (extensions) path.

git submodule add git@github.com:slavcodev-social/ddd-components.git protected/extensions/dddcomponents
git submodule add git@github.com:slavcodev/yii-mail.git protected/extensions/yii-mail

Config application main.php

return array(
    // Add alias to namespace loader
    'aliases' => array(
        'Ddd' => 'ext.dddcomponents.src.Ddd',
        'MailerYii' => 'ext.yii-mail.src. MailerYii',
    ),

    // Add mail component
    'components' => array(
        'mail' => array(
            'class' => '\MailerYii\SwiftMailer',
            'transport' => array(
                // Use any SWIFT transport
                // e.g. Swift_NullTransport, Swift_MailTransport or
                'class' => 'Swift_SmtpTransport',
                'host' => 'mail.host.com',
                'username' => 'username',
                'password' => '******',
            ),
        ),
    ),
);

Usage

Method A: Use model

use Ddd\Mail\Model\HtmlMail;
use Ddd\Mail\Model\Contact;

$mail = new HtmlMail(new Contact('support@localhost.com', 'Sender Name'));
$mail
    ->compose('Mail Subject', 'Mail body')
    ->addRecipient(new Contact('recipient@mail.com'))
    ->addRecipient(new Contact('recipient@mail.eu', 'Recipient Name'));

$mail->send(Yii::app()->mail);

Method B: Use mail builder

use Ddd\Mail\MailBuilder;

$mail = MailBuilder::create('support@localhost.com', 'Sender Name')
    ->compose('Mail Subject', 'Mail body')
    ->addRecipients(array(
        'recipient@mail.com',
        'recipient@mail.eu' => 'Recipient Name'
    ))
    ->getHtmlMail();

$mail->send(Yii::app()->mail);

Hints: Some useful snippets

// Get mailer
$mailer = Yii::app()->mail;

// Create SWIFT plugin
$logger = new Swift_Plugins_Loggers_ArrayLogger();

// Use mailer events to register and dump transport log
$mailer->onBeforeSend = function($event) use ($logger) {
    $mailer = $event->sender;
    $mailer->getTransport()->registerPlugin(
        new Swift_Plugins_LoggerPlugin($logger)
    );
};
$mailer->onAfterSend = function($event) use ($logger) {
    $logger->dump();
};

// Render views to mail body
$mail = $mailer->createBuilder('support@localhost.com', 'Sender Name')
    ->compose(
        'Mail Subject',
        Yii::app()->controller->renderPartial('view-name', array(), true)
    )
    ->addRecipients(array('recipient@mail.eu' => 'Recipient Name'))
    ->getHtmlMail();

$mail->send($mailer);
Something went wrong with that request. Please try again.