adt/single-recipient-mailer

组件修改了 `Nette\Mail\Message`,以便将所有邮件发送到单个地址。适用于非生产环境。

v5.1 2023-05-28 05:55 UTC

This package is auto-updated.

Last update: 2024-08-28 08:36:57 UTC


README

组件修改 Nette\Mail\Message,以便将所有邮件发送到单个地址。适用于非生产环境。

原始的 收件人抄送密送 电子邮件地址存储在电子邮件的主题中(例如:收件人: origTo@example.com; origTo2@example.com; 抄送: origCc@example.com | 我 的 电子邮件主题)。

安装

$ composer require adt/single-recipient-mailer

使用

通过特性

在您的邮件发送器中使用 ADT\SingleRecipient\SingleRecipient 特性,然后使用 applySingleRecipient 方法将单个收件人逻辑应用到原始邮件上。

namespace App\Model;

use ADT\SingleRecipient\SingleRecipient;
use \Nette\Mail\SendmailMailer;

class Mailer extends SendmailMailer
{
	use SingleRecipient;

	public function send(Message $mail): void
	{
		if ($this->singleRecipient) {
			$this->applySingleRecipient($mail, 'developers@myproject.com');
		}

		$this->send($mail);
	}
}

通过配置

在您的 config.neon 中注册 ADT\SingleRecipient\SingleRecipientMailer 以使用 @sendmailMailer 并将所有邮件重定向到 developers@myProject.com

services:
	sendmailMailer:
		class: Nette\Mail\SendmailMailer
		autowired: no # this is important

	mail.mailer: \ADT\SingleRecipient\SingleRecipientMailer(@sendmailMailer, 'developers@myproject.com')

重要:autowired: no 选项非常重要,因为 Nette DI 容器不知道应该将哪个 \Nette\Mail\IMailer 注入到您的应用程序中。

通过继承

如果您愿意,也可以扩展该类。

namespace App\Model;

use Nette\Mail\SendmailMailer;

class Mailer extends \ADT\SingleRecipient\SingleRecipientMailer 
{
	public function __construct() 
	{
		parent::__construct(new SendMailMailer, 'developers@myproject.com');
	}

	public function send(\Nette\Mail\Message $mail) 
	{
		parent::send($mail); # do not forget to call this
	}
}

通过传递空值(例如 NULL 或空字符串)可以禁用重定向到单个收件人。