GithubHelp home page GithubHelp logo

shijiaoliang / yii2-mailqueue Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nterms/yii2-mailqueue

1.0 2.0 0.0 38 KB

Email queue component for yii2 that works with yii2-swiftmailer - https://github.com/yiisoft/yii2-swiftmailer

License: MIT License

PHP 100.00%

yii2-mailqueue's Introduction

yii2-mailqueue

Email queue component for yii2 that works with yii2-swiftmailer

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist nterms/yii2-mailqueue "*"

or add

"nterms/yii2-mailqueue": "*"

to the require section of your composer.json file.

Configuration

Once the extension is installed, add following code to your application configuration :

return [
    //....
    'components' => [
        'mailqueue' => [
            'class' => 'nterms\mailqueue\MailQueue',
			'table' => '{{%mail_queue}}',
			'mailsPerRound' => 10,
			'maxAttempts' => 3,
        ],
    ],
];

Following properties are available for customizing the mail queue behavior.

  • table: Name of the database table to store emails added to the queue.
  • mailsPerRound: Number of emails to send at a time.
  • maxAttempts: Maximum number of sending attempts per email.

Updating database schema

Apply the database migration to create the table required to store the mail queue messages. To do this, add following code to /config/console.php:

return [
    //....
    'components' => [
        'mailqueue' => [
            'class' => 'nterms\mailqueue\MailQueue',
			'table' => '{{%mail_queue}}',
        ],
    ],
];

then run yii migrate command in command line:

php yii migrate/up --migrationPath=@vendor/nterms/yii2-mailqueue/migrations/

Processing the mail queue

Now calling process() on Yii::$app->mailqueue will process the message queue and send out the emails. In one of your controller actions:

public function actionSend()
{
	Yii::$app->mailqueue->process();
}

Most preferably this could be a console command (eg: mail/send) which can be triggered by a CRON job.

Setting the CRON job

Set a CRON job to run console command:


*/10 * * * * php /var/www/html/myapp/yii mailqueue/process

Usage

You can then send an email to the queue as follows:

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

While nterms\mailqueue\MailQueue extends from yii\swiftmailer\Mailer, you can replace it with this extension by adding yii2-swiftmailer configuations directly to mailqueue configurations as follows:

return [
    //....
    'components' => [
        'mailqueue' => [
            'class' => 'nterms\mailqueue\MailQueue',
			'table' => '{{%mail_queue}}',
			'mailsPerRound' => 10,
			'maxAttempts' => 3,
			'transport' => [
				'class' => 'Swift_SmtpTransport',
				'host' => 'localhost',
				'username' => 'username',
				'password' => 'password',
				'port' => '587',
				'encryption' => 'tls',
			],
        ],
    ],
];

And use following code for directly sending emails as you ususally do with yii2-swiftmailer:

Yii::$app->mailqueue->compose('contact/html')
     ->setFrom('[email protected]')
     ->setTo($form->email)
     ->setSubject($form->subject)
     ->setTextBody($form->body)
     ->send();

License

MIT

yii2-mailqueue's People

Contributors

nterms avatar bbrunekreeft avatar saranga-kraken avatar 3064709 avatar peterngockhoa avatar apolyntsev avatar beroso avatar diego-betto avatar macklus avatar lukesky1 avatar

Stargazers

vabc avatar

Watchers

James Cloos avatar vabc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.