GithubHelp home page GithubHelp logo

yii2-mailqueue's Introduction

yii2-mailqueue

Yii2 mail queue component for yii2-swiftmailer.

Latest Stable Version

Limitation

Since 1.1.1 requires PHP >= 7.0

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist tigrov/yii2-mailqueue "~1.1.1"

or add

"tigrov/yii2-mailqueue": "~1.1.1"

to the require section of your composer.json file.

Configuration

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

return [
    // ...
    'components' => [
        'mailer' => [
            'class' => 'tigrov\mailqueue\Mailer',
            'table' => '{{%mail_queue}}',
            'maxAttempts' => 5,
            'attemptIntervals' => [0, 'PT10M', 'PT1H', 'PT6H'],
            'removeFailed' => true,
            'maxPerPeriod' => 10,
            'periodSeconds' => 1,
        ],
    ],
    // ...
];

Following properties are available for customizing the mail queue behavior.

  • table name of the database table to store emails added to the queue;
  • maxAttempts maximum number of sending attempts per email;
  • attemptIntervals seconds or interval specifications to delay between attempts to send a mail message, see http://php.net/manual/en/dateinterval.construct.php;
  • removeFailed indicator to remove mail messages which were not sent in maxAttempts;
  • maxPerPeriod number of mail messages which could be sent per periodSeconds;
  • periodSeconds period in seconds which indicate the time interval for maxPerPeriod option.

Updating database schema

Run yii migrate command in command line:

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

Sending the mail queue

To sending mails from the queue call Yii::$app->mailer->sending() or run the console command yii mailqueue which can be triggered by a CRON job:

* * * * * php /var/www/vhosts/domain.com/yii mailqueue/sending

After the mail message successfully sent it will be deleted from the queue.

Usage

You can then send a mail to the queue as follows:

Yii::$app->mailer->compose('contact/html')
     ->setFrom('[email protected]')
     ->setTo($form->email)
     ->setSubject($form->subject)
     ->setTextBody($form->body)
     ->delay('PT3M') // seconds or an interval specification to delay of sending the mail message, see http://php.net/manual/en/dateinterval.construct.php
     ->unique('unique key') // a unique key for the mail message, new message with the same key will replace the old one
     ->queue();

You can still send mails directly with yii2-swiftmailer:

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

License

MIT

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.