Pollen Solutions - 邮件组件 - 用于创建和发送电子邮件的工具。

v1.0.1 2021-08-20 00:00 UTC

This package is auto-updated.

Last update: 2024-09-07 05:49:40 UTC


README

Latest Stable Version MIT Licensed PHP Supported Versions

Pollen Solutions 邮件 组件提供创建和发送电子邮件的工具。

安装

composer require pollen-solutions/mail

基本用法

发送电子邮件

use Pollen\Mail\MailManager;

$mail = new MailManager();

$mail->send([
    'to'   => ['to@domain.ltd', 'Recipient Name'],
    'from' => ['from@domain.ltd', 'Sender Name'],
]);

调试电子邮件

use Pollen\Mail\MailManager;

$mail = new MailManager();

$mail->debug([
    'to'   => ['to@domain.ltd', 'Recipient Name'],
    'from' => ['from@domain.ltd', 'Sender Name']
]);

队列电子邮件(进行中)

即将推出 ...

邮件配置

/**
 * Name identifier in mail manager.
 * @var string|null
 */
'name'         => null,
/**
 * Recipients of the message.
 * @var string|array|null
 */
'to'           => null,
/**
 * Message sender.
 * @var string|array|null
 */
'from'         => null,
/**
 * Reply-to contact of the message.
 * @var string|array|null
 */
'reply-to'     => null,
/**
 * Blind carbon copy recipients of the message.
 * @var string|array|null
 */
'bcc'          => null,
/**
 * Carbon copy recipients of the message.
 * @var string|array|null
 */
'cc'           => null,
/**
 * Attachments of the message.
 * @var string|array|null
 */
'attachments'  => null,
/**
 * HTML content of the message.
 * {@internal false or empty to disable|true to use the html/message template|string of the HTML
 * content|array of message parts. The Array parts could have header|body|footer key and typed
 * as bool|string.}
 * @var bool|string|array|null
 */
'html'         => true,
/**
 * Plain text content of the message.
 * {@internal false or empty to disable|true to use the text/message template|string of text content.}
 * @var string|null
 */
'text'         => null,
/**
 * List of template datas.
 * @var array
 */
'datas'        => [],
/**
 * Subject of the message.
 * @var string
 */
'subject'      => 'Mail test',
/**
 * Locale of the message.
 * @var string
 */
'locale'       => 'en',
/**
 * Charset of the message.
 * @var string
 */
'charset'      => 'utf-8',
/**
 * Encoding of the message.
 * @var string|null 8bit|7bit|binary|base64|quoted-printable
 */
'encoding'     => null,
/**
 * Content type of the message.
 * @var string|null multipart/alternative|text/html|text/plain
 */
'content_type' => null,
/**
 * Css properties of the HTML message.
 * @var string
 */
'css'          => file_get_contents($this->mail()->resources('/assets/css/styles.css')),
/**
 * Inline CSS formatting flag.
 * @var bool
 */
'inline_css'   => true,
/**
 * List of parameters of the template view|View instance.
 * @var array|ViewInterface $view
 */
'view'         => [],

使用已注册的邮件

use Pollen\Mail\MailManager;

$mail = new MailManager();

$mail->setMailable([
    'name' => 'my-first-mail',
    'to'   => ['to@domain.ltd', 'Recipient Name'],
    'from' => ['from@domain.ltd', 'Sender Name'],
]);

if ($mailable = $mail->get('my-first-mail')) {
    $mailable->send();
}

使用 Mailable

使用基础 Mailable 发送电子邮件

use Pollen\Mail\MailManager;
use Pollen\Mail\Mailable;

$mail = new MailManager();
$mailable = new Mailable($mail);
$mailable
    ->setFrom(['from@domain.ltd', 'Sender Name'])
    ->setTo(['to@domain.ltd', 'Recipient Name']);

$mailable->send();

使用自定义 Mailable 发送电子邮件

use Pollen\Mail\MailManager;
use Pollen\Mail\Mailable;

$mail = new MailManager();
$mailable = new class($mail) extends Mailable {
    protected ?array $from = ['from@domain.ltd', 'Sender Name'];
    protected ?array $to = ['to@domain.ltd', 'Recipient Name'];
};

$mailable->send();

模板电子邮件

@see https://tedgoas.github.io/Cerberus/

提示

使用 MailHog 测试电子邮件

MailHog 必须已安装在您的服务器上并正在运行。

更多详情: https://github.com/mailhog/MailHog

  1. 使用默认配置启动 MailHog
~/go/bin/MailHog
  1. 为 MailHog 配置 Pollen 邮件组件
use Pollen\Mail\MailManager;
use Pollen\Mail\Drivers\PhpMailerDriver;

$mail = new MailManager();
$mail->setMailerConfigCallback(function (PhpMailerDriver $mailer) {
    $mailer->isSMTP();
    $mailer->Host = '0.0.0.0';
    $mailer->Username = 'mailhog.example';
    $mailer->Port = 1025;
});

$mail->send();

访问 MailHog Web UI: http://0.0.0.0:8025

在 WordPress 中配置 MailHog

对于 WordPress 环境,在当前主题的 functions.php 中添加此配置

# functions.php 
add_action('phpmailer_init', function (PHPMailer $mailer) {
    $mailer->isSMTP();
    $mailer->Host = '0.0.0.0';
    $mailer->Username = 'mailhog.example';
    $mailer->Port = 1025;
});