yii2-spoolmailer

Implements email queue using SwiftMailer spool transport and yii2-swiftmailer extension

BSD-3-CLAUSE License

Downloads
3.8K
Stars
4
Committers
1

SwiftMailer Extension for Yii 2 with Spooling

Implements email queue using SwiftMailer spool transport and yii2-swiftmailer extension.

It supported queues based on built-in SwiftMailer spools or Yii2 Queue Extension.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist izumi-kun/yii2-spoolmailer

or add

"izumi-kun/yii2-spoolmailer": "~1.0.0"

to the require section of your composer.json.

Basic Usage

This way uses built-in SwiftMailer spools: FileSpool (default) or MemorySpool.

Add the following code in your application configuration (both web and console):

return [
    //....
    'components' => [
        //....
        'mailer' => [
            'class' => izumi\spoolmailer\Mailer::class,
        ],
    ],
];

Add the following code in your console configuration:

return [
    //....
    'controllerMap' => [
        'mail' => izumi\spoolmailer\MailController::class,
    ],
];

You can then add an email in queue as follows:

Yii::$app->mailer->compose('contact/html')
     ->setFrom('[email protected]')
     ->setTo($form->email)
     ->setSubject($form->subject)
     ->queue();

Process email queue by follow console command:

./yii mail/flush

CRON job:

* * * * * php /var/www/yii-app/yii mail/flush >/dev/null 2>&1

Advanced Usage

This way requires Yii2 Queue Extension in your application.

Add the following code in your application configuration (both web and console):

return [
    //....
    'components' => [
        //....
        'mailer' => [
            'class' => izumi\spoolmailer\Mailer::class,
            'spoolMailer' => [
                'class' => izumi\spoolmailer\spools\QueueSpool::class,
                'queue' => 'queue', // the application component ID of the queue object
            ],
        ],
    ],
];

For more details see the Yii2 Queue Guide.

License

BSD-3-Clause