jerowork/email-message

该包已被 废弃 并不再维护。未建议替代包。

PHP 7.2+ 的电子邮件消息值对象。

0.1.0 2019-03-13 17:48 UTC

This package is not auto-updated.

Last update: 2021-01-20 16:42:13 UTC


README

PHP 7.2+ 的电子邮件消息值对象。

特性

  • 不可变值对象
  • 可用参数:主题、HTML/文本正文、发件人、回复电子邮件、多个收件人/抄送/密送、附件(基于字符串)
  • 可序列化(例如:队列使用)(实现 JsonSerializable
  • 消息验证(最小所需有效消息)
  • 验证电子邮件地址

安装

通过 Composer 安装

$ composer require jerowork/email-message

用法

// Construct message
$message = new Message(
    Addressee::fromString('Jero Work <info@jero.work'),
    'Some subject',
    new Body('<p>Some html body</p>')
);

// Add to recipients
$message = $message
    ->withToRecipient(
        Addressee::fromString('info@jero.work'),
        Addressee::fromString('help@jero.work')
    );

// Add cc/bcc recipients
$message = $message
    ->withCcRecipient(Addressee::fromString('Somebody <info@example.com>'))
    ->withBccRecipient(new Addressee(new Email('info@foo.com'), 'Another body'));

// Add reply to email
$message = $message->withReplyToEmail(new Email('reply@jero.work'));

// Add attachments
$message = $message->withAttachment(
    '/path/to/file',
    '/path/to/file'
);

// Update body
$message = $message->withTextBody('Some text body');

// Update other parameters, e.g. sender
$message = $message->withSender(Addressee::fromString('no-reply@jero.work'));

// Verify if email is setup correctly
if ($message->isValid() === true) {
    // Do something
    // ...
    $subject      = $messsage->getSubject();
    $toRecipients = $message->getToRecipients();
}

// Serialize value object (for use in e.g. queues)
$array = $message->jsonSerialize();