berlioz / mailer
Berlioz Mailer 是一个用于发送邮件的 PHP 库,支持或不支持本地服务器。
v1.3.2
2021-07-09 14:04 UTC
Requires
- php: ^7.1 || ^8.0
- ext-fileinfo: *
- ext-mbstring: *
- psr/log: ^1.0
Requires (Dev)
- phpunit/phpunit: ^7.4 || ^8.0 || ^9.0
README
Berlioz Mailer 是一个用于发送邮件的 PHP 库,支持或不支持本地服务器。
安装
Composer
您可以使用 Composer 安装 Berlioz Mailer,这是推荐的安装方式。
$ composer require berlioz/mailer
依赖关系
- PHP ^7.1 || ^8.0
- PHP 扩展
- 文件信息
- 包
- psr/log
使用方法
示例
您可以像这样简单发送邮件
use Berlioz\Mailer\Address; use Berlioz\Mailer\Mail; use Berlioz\Mailer\Mailer; $mail = (new Mail()) ->setSubject('Test of Berlioz/Mailer') ->setText('Text plain of my mail') ->setHtml('<p>Html text of my mail</p>') ->setFrom(new Address('sender@test.com', 'Me the sender')) ->setTo([new Address('recipient@test.com', 'The recipient')]); $mailer = new Mailer(); $mailer->send($mail);
邮件
\Berlioz\Mailer\Mail 是邮件的对象表示。
基本
use Berlioz\Mailer\Address; use Berlioz\Mailer\Mail; $mail = new Mail(); $mail->setSubject('Subject of my mail') ->setText('Text plain of my mail') ->setHtml('<p>Html text of my mail</p>') ->setFrom(new Address('sender@test.com', 'Me the sender')) ->setTo([new Address('recipient@test.com', 'The recipient')]);
附件
添加可下载的附件
use Berlioz\Mailer\Attachment;use Berlioz\Mailer\Mail;$attachment = new Attachment('/path/of/my/file.pdf'); $mail = new Mail(); $mail->addAttachment($attachment);
将附件附加到 HTML 内容
use Berlioz\Mailer\Attachment;use Berlioz\Mailer\Mail;$attachment = new Attachment('/path/of/my/img.jpg'); $mail = new Mail(); $mail->addAttachment($attachment); $html = '<p>Html content 1</p>'; $html .= '<img src="cid:' . $attachment->getId() . '">'; $html .= '<p>Html content 2</p>'; $mail->setHtml($html);
警告:调用 $attachment->getId()
方法,这意味着附件将处于内联位置。仅使用此方法进行内联附件。
传输
默认传输
默认传输是 \Berlioz\Mailer\Transport\PhpMail,它使用 PHP 内置的 mail() 函数。
您可以使用其他可用的传输来直接与 SMTP 服务器通信: \Berlioz\Mailer\Transport\Smtp。
use Berlioz\Mailer\Mailer; use Berlioz\Mailer\Transport\Smtp; $smtp = new Smtp( 'smpt.test.com', 'user@test.com', 'password', 25, ['timeout' => 5] ); $mailer = new Mailer(); $mailer->setTransport($smtp);
use Berlioz\Mailer\Mailer; $mailer = new Mailer([ 'transport' => [ 'name' => 'smtp', 'arguments' => [ 'host' => 'smpt.test.com', 'username' => 'user@test.com', 'password' => 'password', 'port' => 25, 'options' => ['timeout' => 5] ] ] ]);
创建新的传输
出于各种原因,您可以创建新的传输。为此,您需要创建一个实现 \Berlioz\Mailer\Transport\TransportInterface 接口的新类。