承接 atsyscorp/yii2-mailqueue 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

atsyscorp/yii2-mailqueue

最新稳定版本:1.1.0

Composer 安装命令:

composer require atsyscorp/yii2-mailqueue

包简介

Email queue component for yii2. Created by Saranga Abeykoon, updated by ATSYS

README 文档

README

Email queue component for yii2 that works with yii2-symfonymailer

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

"atsyscorp/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' => 'atsyscorp\mailqueue\MailQueue',
			'mailsPerRound' => 10,
			'maxAttempts' => 3,
        ],
    ],
];

Following properties are available for customizing the mail queue behavior.

  • 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' => 'atsyscorp\mailqueue\MailQueue',
        ],
    ],
];

then run yii migrate command in command line:

php yii migrate/up --migrationPath=@vendor/atsyscorp/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('from@domain.com')
     ->setTo($form->email)
     ->setSubject($form->subject)
     ->setTextBody($form->body)
     ->queue();

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

return [
    //....
    'components' => [
        'mailqueue' => [
            'class' => 'atsyscorp\mailqueue\MailQueue',
			'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-symfonymailer:

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

License

MIT

统计信息

  • 总下载量: 33
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 50
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-27