项目作者: Tigrov

项目描述 :
Yii2 mail queue component for yii2-swiftmailer.
高级语言: PHP
项目地址: git://github.com/Tigrov/yii2-mailqueue.git
创建时间: 2016-10-22T04:02:07Z
项目社区:https://github.com/Tigrov/yii2-mailqueue

开源协议:MIT License

下载


yii2-mailqueue

Yii2 mail queue component for yii2-symfonymailer.

Latest Stable Version

Limitation

Since 1.1.6 requires PHP >= 8.1

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

  1. "tigrov/yii2-mailqueue": "~1.1.6"

to the require section of your composer.json file.

Configuration

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

  1. return [
  2. // ...
  3. 'components' => [
  4. 'mailer' => [
  5. 'class' => 'tigrov\mailqueue\Mailer',
  6. 'table' => '{{%mail_queue}}',
  7. 'maxAttempts' => 5,
  8. 'attemptIntervals' => [0, 'PT10M', 'PT1H', 'PT6H'],
  9. 'removeFailed' => true,
  10. 'maxPerPeriod' => 10,
  11. 'periodSeconds' => 1,
  12. ],
  13. ],
  14. // ...
  15. ];

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:

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

  1. * * * * * 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:

  1. Yii::$app->mailer->compose('contact/html')
  2. ->setFrom('from@domain.com')
  3. ->setTo($form->email)
  4. ->setSubject($form->subject)
  5. ->setTextBody($form->body)
  6. ->delay('PT3M') // seconds or an interval specification to delay of sending the mail message, see http://php.net/manual/en/dateinterval.construct.php
  7. ->unique('unique key') // a unique key for the mail message, new message with the same key will replace the old one
  8. ->queue();

You can still send mails directly with yii2-swiftmailer:

  1. Yii::$app->mailer->compose('contact/html')
  2. ->setFrom('from@domain.com')
  3. ->setTo($form->email)
  4. ->setSubject($form->subject)
  5. ->setTextBody($form->body)
  6. ->send();

License

MIT