bayfrontmedia/mail-manager

不依赖框架的库,用于使用统一的API从多个服务中排队和发送电子邮件。

v2.0.1 2024-09-10 01:21 UTC

This package is auto-updated.

Last update: 2024-09-10 01:22:12 UTC


README

不依赖框架的库,用于使用统一的API从多个服务中排队和发送电子邮件。

注意:目前正在开发中,以整合额外的邮件服务(适配器)到邮件管理器中,这些服务将随着开发进程逐步发布。

许可证

此项目为开源,可在MIT许可证下使用。

作者

Bayfront Media

要求

  • PHP ^8.0
  • PDO PHP 扩展

安装

composer require bayfrontmedia/mail-manager

用法

注意:Mail Manager 抛出的所有异常都扩展自 Bayfront\MailManager\Exceptions\MailException,因此您可以选择以尽可能狭窄或广泛的方式捕获异常。

适配器

必须将 Bayfront\MailManager\AdapterInterface 传递给 Bayfront\MailManager\MailBayfront\MailManager\MailQueue 构造函数。有各种适配器可供选择,每种适配器都有自己的必需配置。

此外,您还可以创建和使用自己的适配器,以与 Mail Manager 一起使用。

所有适配器都有一个 getInstance() 方法,可以用来获取适配器使用的底层实例。

PHPMailer

PHPMailer 适配器允许您使用 PHPMailer 发送消息。

use Bayfront\MailManager\Adapters\PHPMailer;

$config = [
    'smtp' => true,
    'smtp_auth' => true,
    'smtp_secure' => 'tls', // STARTTLS or SMTPS
    'host' => 'mail.example.com',
    'port' => 587,
    'username' => 'your_name@example.com',
    'password' => 'your_password',
    'debug' => false
];

try {

    $adapter = new PHPMailer($config);

} catch (AdapterException $e) {
    die($e->getMessage());
}

PHPMailer 适配器还有一个 testConnection() 方法,您可以使用它来测试与 SMTP 服务器的成功连接。

开始使用 Mail Manager

您可以选择以下类之一进行使用

Bayfront\MailManager\Mail 类允许创建并立即发送消息。不需要数据库。

Bayfront\MailManager\MailQueue 类与上述相同,但它需要一个 PDO 实例来处理排队消息。排队消息允许在稍后的日期以编程方式发送消息。

邮件默认配置

use Bayfront\MailManager\Mail;

$mail = new Mail($adapter);

邮件队列默认配置

use Bayfront\MailManager\MailQueue;
use Bayfront\MailManager\Exceptions\QueueException;

$pdo = new PDO('mysql:host=example.com;dbname=database_name', 'username', 'password');

$queue_config = [
    'table' => 'mail_queue', // Name of database table to use
    'max_attempts' => 3 // Maximum number of failed sending attempts before deleting message
];

try {

    $queue = new MailQueue($adapter, $pdo, $queue_config);

} catch (QueueException $e) {
    die($e->getMessage());
}

公共方法

仅限邮件队列

创建

描述

创建一个新的消息。

参数

  • $message (数组)

返回

  • (self)

抛出

  • Bayfront\MailManager\Exceptions\MessageException

示例

$message = [
    'to' => [ // Array of recipients
        [
            'address' => 'to@example.com'.
            'name' => 'Recipient name' // Optional
        ]
    ],
    'from' => [
        'address' => 'you@example.com'
        'name' => 'Your name' // Optional
    ],
    'subject' => 'Message subject',
    'body' => 'Message body'
];

try {

    $mail->create($message);

} catch (MessageException $e) {
    die($e->getMessage());
}

添加地址

描述

添加一个“收件人”。

参数

  • $address (字符串)
  • $name = NULL (字符串|null):如果为 NULL,则不会定义名称

返回

  • (self)

示例

$mail->addAddress('jane.doe@example.com', 'Jane Doe');

添加抄送

描述

添加一个“抄送”收件人。

参数

  • $address (字符串)
  • $name = NULL (字符串|null):如果为 NULL,则不会定义名称

返回

  • (self)

示例

$mail->addCC('jane.doe@example.com', 'Jane Doe');

添加密送

描述

添加一个“密送”收件人。

参数

  • $address (字符串)
  • $name = NULL (字符串|null):如果为 NULL,则不会定义名称

返回

  • (self)

示例

$mail->addBCC('jane.doe@example.com', 'Jane Doe');

添加附件

描述

添加一个附件。

参数

  • $file (字符串):文件的路径
  • $name = NULL (字符串|null):分配给文件的新的名称。如果为 NULL,则使用现有名称。

返回

  • (self)

示例

$mail->addAttachment('/path/to/existing/image.jpg', 'new-name.jpg');

丢弃

描述

丢弃消息。

参数

返回

  • (self)

示例

$mail->discard();

发送

描述

发送消息。

参数

返回

  • (void)

抛出

  • Bayfront\MailManager\MessageException

示例

$mail->send();

添加队列

注意:此方法仅适用于 MailQueue 类。

描述

排队消息。

参数

  • $date_due (\DateTimeInterface)
  • $priority = 5 (int):消息将按到期日期排序,优先级按降序排序发送

返回

  • (void)

抛出

  • Bayfront\MailManager\QueueException

示例

$message = [
    'to' => [ // Array of recipients
        [
            'address' => 'to@example.com'.
            'name' => 'Recipient name' // Optional
        ]
    ],
    'from' => [
        'address' => 'you@example.com'
        'name' => 'Your name' // Optional
    ],
    'subject' => 'Message subject',
    'body' => 'Message body'
];

try {

    $mail->create($message);

} catch (MessageException $e) {
    die($e->getMessage());
}

try {

    $mail->addQueue(new \Datetime('2020-10-01 10:35:00'));

} catch (QueueException $e) {
    die($e->getMessage());
}

移除队列

注意:此方法仅适用于 MailQueue 类。

描述

从队列中移除指定 ID。

参数

  • $id (整型): 数据库表中的唯一 id

返回

  • (布尔型)

抛出

  • Bayfront\MailManager\QueueException

示例

$mail->removeQueue(35);

获取队列

注意:此方法仅适用于 MailQueue 类。

描述

获取队列中所有到期消息,最多到给定限制。

参数

  • $limit = 0 (整型): 0 表示获取所有

返回

  • (数组)

抛出

  • Bayfront\MailManager\QueueException

示例

try {

    print_r($mail->getQueue());

} catch (QueueException $e) {
    die($e->getMessage());
}

发送队列

注意:此方法仅适用于 MailQueue 类。

描述

发送队列中所有到期消息,最多到给定限制。

参数

  • $limit = 0 (整型): 0 表示发送所有

返回

  • (数组)

抛出

  • Bayfront\MailManager\QueueException

示例

try {

    $results = $mail->sendQueue(50); // Send up to 50 queued messages

} catch (QueueException $e) {
    die($e->getMessage());
}

返回的数组包含以下结构的摘要结果

[
    'sent' => 0, // Number of messages sent
    'removed' => 0, // Number of messages removed (exceeded max attempts)
    'failed' => 0, // Number of messages failed
    'failed_ids' => [] // Unique ID's of failed messages
];