Yii Framework Forum: Swift Mailer Component - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Swift Mailer Component Component for Yii based applications to provide Swift Mailer Rate Topic: ***** 1 Votes

#1 User is offline   sobit 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 02-December 10

Posted 06 November 2013 - 01:23 PM

swiftmailer-component

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

Installation

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"
        ),
    ),
);


Usage

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);


Links

Homepage: https://github.com/s...ailer-component
Composer: http://getcomposer.org
0

#2 User is offline   Bogdan Savluk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 20-April 10
  • Location:Kiev

Posted 06 November 2013 - 09:59 PM

View Postsobit, on 06 November 2013 - 01:23 PM, said:

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


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.
0

#3 User is offline   sobit 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 02-December 10

Posted 07 November 2013 - 03:24 AM

View PostBogdan Savluk, on 06 November 2013 - 09:59 PM, said:


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

View PostBogdan Savluk, on 06 November 2013 - 09:59 PM, said:

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...

That's right. I will update this one.

View PostBogdan Savluk, on 06 November 2013 - 09:59 PM, said:

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);


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.

View PostBogdan Savluk, on 06 November 2013 - 09:59 PM, said:

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.

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.
0

#4 User is offline   Bogdan Savluk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 20-April 10
  • Location:Kiev

Posted 07 November 2013 - 08:01 AM

View Postsobit, on 07 November 2013 - 03:24 AM, said:

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.

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

View Postsobit, on 07 November 2013 - 03:24 AM, said:

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.

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...
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users