dejvidecz/yii2-mailqueue

修改过的用于自身系统目的的Yii2邮件队列组件

安装: 375

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 4

类型:yii2-extension

v1.0.0 2017-11-24 15:09 UTC

This package is auto-updated.

Last update: 2024-09-17 19:54:35 UTC


README

yii2-swiftmailer提供的Yii2邮件队列组件。

安装

安装此扩展的首选方式是通过 composer

运行以下命令

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

或者在您的 composer.json 文件的 require 部分添加以下内容

"tigrov/yii2-mailqueue": "*"

配置

安装扩展后,将以下代码添加到您的应用程序配置中

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

以下属性可用于自定义邮件队列的行为。

  • table 存储添加到队列中的电子邮件的数据库表名;
  • maxAttempts 每封电子邮件发送尝试的最大次数;
  • attemptIntervals 两次发送尝试之间的延迟秒数或间隔规范,请参阅 https://php.ac.cn/manual/en/dateinterval.construct.php
  • removeFailed 指示是否删除在 maxAttempts 内未发送的邮件消息;
  • maxPerPeriodperiodSeconds 内可以发送的邮件消息数量;
  • periodSeconds 表示 maxPerPeriod 选项时间间隔的秒数。

更新数据库架构

在命令行中运行 yii migrate 命令

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

发送邮件队列

要从队列中发送邮件,请调用 Yii::$app->mailer->sending() 或运行控制台命令 yii mailqueue,该命令可以通过 CRON 作业触发

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

邮件消息成功发送后,它将从队列中删除。

用法

您可以通过以下方式将邮件发送到队列

Yii::$app->mailer->compose('contact/html')
     ->setFrom('from@domain.com')
     ->setTo($form->email)
     ->setSubject($form->subject)
     ->setTextBody($form->body)
     ->delay('PT3M') // seconds or an interval specification to delay of sending the mail message, see https://php.ac.cn/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();

您仍然可以直接使用 yii2-swiftmailer 发送邮件

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

许可

MIT