Swift Mailer Component

[size="7"]swiftmailer-component[/size]

Component for Yii Framework based application which provides simple configuration interface for Swift Mailer library.

[size="6"]Installation[/size]

The easiest way to install the package is to use Composer, so all the dependent packages will be installed along with this one.

Add dependency to your composer.json file:




{

    "require": {

        "sobit/swiftmailer-component": "dev-master"

    }

}



Update your protected/config/main.php file:




<?php


Yii::setPathOfAlias('vendor', dirname(__FILE__) . '/../../vendor');


return array(

    'components' => array(

        'mailer' => array(

            'class' => 'vendor.sobit.swiftmailer-component.SwiftMailerComponent',


            'swiftBasePath' => dirname(__FILE__) . '/../../vendor/swiftmailer/swiftmailer',


            'host'     => 'localhost', // smtp host

            'port'     => 25,          // smtp port

            'username' => null,        // username

            'password' => null,        // password

            'security' => null,        // security, e.g. "ssl"

        ),

    ),

);



[size="6"]Usage[/size]

Most simple usage example:




$message = Yii::app()->mailer

    ->createMessage('Test subject', 'Test body content')

    ->setFrom(array('john@doe.com' => 'John Doe'))

    ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))

;

Yii::app()->mailer->send($message);



[size="6"]Links[/size]

Homepage: https://github.com/sobit/swiftmailer-component

Composer: http://getcomposer.org

Hello, I have noticed following issues:

1. composer.lock

you should remove it from gitignore

2. you are using @dev version of SwiftMailer

Do you really require this? if no - you should use requirements like this:


swiftmailer/swiftmailer": ">=4.2.0,<5.1-dev

(it is from symfony/swiftmailer-bundle)

Because, if you want to build something stable - you need at least use stable components…

3.You are not using autoloader from composer… why?

It is simple, you need just to include it at your application entry point (index.php or yiic.php for console scripts):




$yii = dirname(__FILE__) . '/vendor/yiisoft/yii/framework/yii.php';

$composerAutoloader = dirname(__FILE__) . '/vendor/autoload.php';


require_once($composerAutoloader);

require_once($yii);



4. You are creating mailer object in init() method.

It is bad idea - because, you do not need it in every request: you need it only when you are sending email, and it is not often.

So you need to create it lazy, only when it is actually required, like this:




    public function getMailer()

    {

        if ($this->mailer === null)

            $this->mailer = Swift_Mailer::newInstance($this->getTransport());


        return $this->mailer;

    }



The same thing about transport object for mailer.

Also, I have created similar component for Yii some time ago :)

If you are interested here it is.

It’s true for the Composer based application, not for the package.

That’s right. I will update this one.

Unfortunately, composer autoload is not working well with Swift Mailer classes when Yii autoloader is registered. At the moment it’s the best solution I’ve found.

I agree about the mailer object. But not about the transport. If it will be created on request, there’s a possibility that config values will be overwritten.

Thanks for your feedback.

Really? I what way? - I am always using it, and still does not noticed any problem with it.

I think it also should be created on first request (because, no need to create it for every request - it is just waste of memory and time).

About configuration values: component is designed to be configured in application configuration, so if someone is trying to change them in application he is doing something wrong(and I think strange behavior for such things is normal)…

If you want notify user about that, you can do this in setters for configuration values:




public function setHost($host){

   if($this->getIsInitialized()) throw new Exception(...);

   $this->host = $host;

}



But, really - I do not think that, it is required…