byjg/mailwrapper

发送邮件的轻量级包装器。接口与发送者完全解耦。可用的发送者有:PHP Mailer、AWS SES Api、Mandril Api。

4.9.0 2023-05-21 22:09 UTC

README

Build Status Opensource ByJG GitHub source GitHub license GitHub release

发送邮件的轻量级包装器。接口与发送者完全解耦。目的是创建一个单一的发送邮件接口,无论发送者是谁。有三个选项可供选择

  • SMTP(带SSL/TLS)
  • AWS SES(直接使用API)
  • Mailgun(直接使用API)

如何使用

MailWrapper将你的类完全解耦为三个部分

  • 信封:邮件信封。定义了邮件发送者、收件人、正文、主题等;
  • 邮件发送者:负责处理发送信封的过程;
  • 注册表:将在系统中注册可用的邮件发送者。

信封类

MailWrapper提供了一个带有创建电子邮件所需所有基本属性的信封类。由于这个信封类与邮件发送引擎完全解耦,因此你也可以将其用作DTO。以下是一个示例:(别忘了 require "vendor/autoload.php"

<?php
// Create the email envelope
$envelope = new ByJG\Mail\Envelope();
$envelope->setFrom('johndoe@example.com', 'John Doe');
$envelope->addTo('jane@example.com');
$envelope->setSubject('Email Subject');
$envelope->setBody('html text body');

发送电子邮件

一旦你创建了信封,你就可以发送电子邮件。基本上,你需要在工厂中注册所有打算使用的邮件发送者,然后创建邮件发送者

<?php
// Register the available class
\ByJG\Mail\MailerFactory::registerMailer(\ByJG\Mail\Wrapper\PHPMailerWrapper::class);
\ByJG\Mail\MailerFactory::registerMailer(\ByJG\Mail\Wrapper\MailgunApiWrapper::class);

// Create the proper mailer based on the scheme
// In the example below will find the "mailgun" scheme
$mailer = \ByJG\Mail\MailerFactory::create('mailgun://api:YOUR_API_KEY@YOUR_DOMAIN');

// Send the email:
$mailer->send($envelope);

你可以直接创建邮件发送者,而不使用工厂

<?php
$mailer = new \ByJG\Mail\Wrapper\MailgunApiWrapper(
    new \ByJG\Util\Uri(
        'mailgun://your_api_key@YOUR_DOMAIN'
    )
);

// Send the email:
$mailer->send($envelope);

发送附件

<?php
$envelope = new \ByJG\Mail\Envelope('from@email.com', 'to@email.com', 'Subject', 'Body');
$envelope->addAttachment('name_of_attachement', '/path/to/file', 'mime/type');
$mailer->send($envelope);

将附件作为嵌入图像添加

将图像作为内联附件(或嵌入)添加,你的邮件阅读器不会将其显示为下载,但你可以在电子邮件中将其用作本地图像。

查看示例

<?php
$envelope = new \ByJG\Mail\Envelope('from@email.com', 'to@email.com', 'Subject');
$envelope->addEmbedImage('mycontentname', '/path/to/image', 'mime/type');
$envelope->setBody('<img src="cid:mycontentname" />');
$mailer->send($envelope);

连接URL

要创建一个新的发送者,你必须定义一个类似于下面的URL

scheme://username:password/smtpserver:port

选项包括

可用的协议包括

Gmail特定

从2014年12月开始,Google开始实施一种名为XOAUTH2的基于OAuth2的认证机制,用于访问其应用程序,包括Gmail。此更改可能会破坏SMTP和IMAP对gmail的访问,你可能会从许多电子邮件客户端收到认证失败(通常是“5.7.14 请通过您的网络浏览器登录”)的消息,包括PHPMailer、Apple Mail、Outlook、Thunderbird和其他电子邮件客户端。错误输出可能包含一个链接到https://support.google.com/mail/bin/answer.py?answer=78754,其中列出了可能的补救措施。

有两种主要解决方案

通过SMTP发送

你必须启用“允许不安全的应用程序”选项。这并不会真正使你的应用程序变得不安全。据报道,更改此设置可能需要一个小时或更长时间才能生效,因此不要期望立即修复。你可以从这里开始更改这里

通过GMAIL使用SMTP发送电子邮件的连接字符串

tls://YOUREMAIL@gmail.com:YOURPASSWORD@smtp.gmail.com:587

通过XOAuth2发送

此选项目前不受支持。

有关如何设置的更多信息和文档,请参阅此wiki页面。

Amazon SES API特定

AWS SES api的连接URL

ses://ACCESS_KEY_ID:SECRET_KEY@REGION

access_key_id和secret_key是在AWS控制面板中创建的。区域可以是us-east-1等。

Mailgun API特定

Mailgun api的连接URL

mailgun://YOUR_API_KEY@YOUR_DOMAIN

YOUR_API_KEY 和 YOUR_DOMAIN 在 Mailgun 控制面板中定义。

端点所在区域可以通过查询参数 "region" 进行配置(例如:mailgun://api-key@mg.domain.cz?region=eu)

有效值是:us 和 eu。

Sendmail 特殊说明

Sendmail 的连接 URL 为

sendmail://localhost

您需要在 php.ini 中设置邮件中继。

实现自己的包装器

要实现自己的包装器,您需要创建一个继承自 ByJG\Mail\Wrapper\BaseWrapper 的类,并在 public function send(Envelope $envelope); 方法中实现发送方式。

class MyWrapper extends \ByJG\Mail\Wrapper\BaseWrapper
{
    public static function schema()
    {
        return ['mywrapper'];
    }

    public function send(Envelope $envelope)
    {
        // Do how to send the email using your library
    }

    // You can create your own validation methods.
    public function validate(Envelope $envelope)
    {
        parent::validate($envelope);
    }
}

安装

composer require "byjg/mailwrapper"

运行测试

./vendor/bin/phpunit

依赖关系

开源 ByJG