systream/mail-service

此包已被弃用,不再维护。未建议替代包。

0.9.1 2017-05-09 19:25 UTC

This package is not auto-updated.

Last update: 2023-01-07 19:01:55 UTC


README

邮件模板和队列服务。

安装

您可以通过 packagist.org 使用 composer 安装此包。

composer require systream/mail-service

composer.json

"require": {
    "systream/mail-service": "*"
}

此库需要 php 5.6 或更高版本。

使用示例

MailQueue

使用工厂创建一个简单的 mailQueue 项目。

$item = Mail\MailQueueItem\MailQueueItemFactory::make('subject', 'Message for {$name}', 'recipient@email.hu', 'Recipent Name', array('name' => 'John'));

格式化器

您可以向 mailqueue 项目添加消息格式化器。例如,一个标记格式化器,可以在消息和主题中替换指定的标记。

您可以添加多个消息格式化器。

$tokenFormatter = new TokenFormatter($tokens);
// ...
$mailQueueItem = new MailQueueItem($message);
$mailQueueItem->addMessageFormatter($tokenFormatter);

邮件模板

邮件模板给出消息的基本内容。此库提供了一个简单的 StringMailTemplate 对象,但您可以轻松添加自定义的。您需要做的就是实现 MailTemplateInterface

$mailTemplate = new StringMailTemplate($body, $subject);

收件人

可以给消息添加一个或多个收件人。

$message = new Message($mailTemplate);
$message->addRecipient(new Recipient($recipientEmail, $recipientName));

自定义 mailqueue 项目

$tokenFormatter = new TokenFormatter($tokens);
$mailTemplate = new StringMailTemplate($body, $subject);
$message = new Message($mailTemplate);
$message->addRecipient(new Recipient($recipientEmail, $recipientName));
$mailQueueItem = new MailQueueItem($message);
$mailQueueItem->addMessageFormatter($tokenFormatter);

发送消息

您需要一个邮件发送者和队列处理器。

邮件发送者

此存储库为 MailSenderInterface 提供了一个 phpmailler 适配器。

$PHPMailer = new \PHPMailer(true);
$PHPMailer->isSMTP();
$PHPMailer->Host = "127.0.0.1";
$PHPMailer->Port = 1025;
$PHPMailer->SetFrom('test@unit.test');

$mailer = new Mail\MailSender\PHPMailerAdapter($PHPMailer);

队列处理器

SQLite 队列处理器
$mail = new Mail($mailer, new Mail\QueueHandler\SqliteQueHandlerAdapter());

立即发送

$mail->send($item);

将项目添加到队列

$mail->queue($item);

处理队列

$mail->consume();

设置记录器

您可以使用任何 PSR-3 兼容的记录器,例如 monolog。

$mail->setLogger($logger);

测试

Build Status