adt/mail-queue

终极Nette邮件解决方案。

v1.8 2024-06-05 02:23 UTC

README

允许在应用程序内进行邮件队列和延迟发送。

1.1 安装

composer

composer require adt/mail-queue

config.neon

extensions:
	adtMailQueue: ADT\MailQueue\DI\MailQueueExtension

1.1.1 使用默认队列实体

让Doctrine知道我们的实体

doctrine:
	metadata:
		ADT\MailQueue\Entity: %vendorDir%/adt/mail-queue/src/Entity

1.1.2 使用自定义队列实体

创建一个扩展我们抽象实体的自定义实体

namespace App\Model\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class QueueEntity extends \ADT\MailQueue\Entity\AbstractMailQueueEntry {

	/**
	 * @ORM\Column(type="text")
	 */
	protected $customProperty;

}

让我们知道您的自定义实体

adtMailQueue:
	queueEntityClass: App\Model\Entity\QueueEntity

当入队邮件时填充您实体的自定义属性,您可以使用enqueue的第二个参数

$this->mailQueueService->enqueue($mail, [
	'customProperty' => 'customValue'
]);

或者

$this->mailQueueService->enqueue($mail, function (QueueEntity $e) {
	$e->customProperty = 'customValue';
});

1.2.1 使用唯一的一个IMailer

adtMailQueue:
	mailer: @sparkPostApiMailerService

1.2.2 自定义邮件发送器

如果您需要根据您自定义队列实体中的信息来决定使用哪个邮件发送器,您可以实现ADT\MailQueue\Service\IMessenger接口。此接口有一个send($entity)方法,其中$entity是您的自定义实体。

adtMailQueue:
	messenger: @queueMailerMessenger

1.3 迁移

清空您的temp/cache目录。

生成迁移并迁移

php www/index.php migrations:diff
php www/index.php migrations:migrate

1.4 处理入队消息

使用预定义的命令行工具

php www/index.php mail-queue:process

或者从DI容器获取ADT\MailQueue\Services\QueueService并调用

$queueService->process()

1.5 发送错误处理器

如果您需要处理发送错误,您可以设置

adtMailQueue:
	sendErrorHandler: @ErrorHandlerClass::handlerMethod

处理器方法接收队列条目实体和发送时生成的异常。

1.6 队列清空事件

如果您需要在队列清空时收到通知,您可以设置

adtMailQueue:
	onQueueDrained: @EventHandlerClass::handlerMethod

如果可用,事件处理器接收OutputInterface实例,否则接收NULL

2.1 配置

adtMailQueue:
    messenger: #or mailer
    queueEntityClass: #default Entity\MailQueueEntry::class,
    autowireMailer: false
    sendErrorHandler: null
    onQueueDrained: null
    lockTimeout: 600
    limit: 1000 #how many emails send
    tempDir: %tempDir%
    backgroundQueueService: @ADT\BackgroundQueue\Service
    backgroundQueueCallbackName: mailSending