webiik / mail
Mail 提供了一个通用的发送电子邮件的接口,无论您想使用哪个邮件库。它默认支持 PHPMailer 和 SwiftMailer。
1.0
2019-02-28 21:18 UTC
Requires
- php: >=7.2
Suggests
This package is auto-updated.
Last update: 2024-09-29 05:34:40 UTC
README
Mail 提供了一个通用的发送电子邮件的接口,无论您想使用哪个邮件库。它默认支持 PHPMailer 和 SwiftMailer。
安装
composer require webiik/mail
示例
$mail = new \Webiik\Mail\Mail(); // Add PHPMailer $mail->setMailer(function () { return new \Webiik\Mail\Mailer\PHPMailer(new \PHPMailer\PHPMailer\PHPMailer()); }); // Create Message $message = $mail->createMessage(); // Set charset and priority of Message $message->setCharset('utf-8'); $message->setPriority(3); // Set Message sender, bounce address and recipients $message->setFrom('your@email.tld', 'Firstname Lastname'); $message->setBounceAddress('your@email.tld'); $message->addTo('your@email.tld', 'Firstname Lastname'); $message->addReplyTo('your@email.tld'); $message->addCc('your@email.tld'); $message->addBcc('your@email.tld'); // Set Message subject and body $message->setSubject('Test subject'); $message->setBody('Hello world, this is body of test message.'); $message->setAlternativeBody('Hellow world, this is alternative body of test message.'); // Set Message attachment $message->addFileAttachment(__DIR__ . '/nice_picture.jpg'); // Send Message $unsent = $mail->send([$message]); // Get unsent email addresses foreach ($unsent as $email) { // Do something with undelivered email }
setMailer
setMailer(callable $factory):void
setMailer() 设置邮件发送器。默认情况下,您可以选择 PHPMailer 或 SwiftMailer。
$mail->setMailer(function () { return new \Webiik\Mail\Mailer\PHPMailer(new \PHPMailer\PHPMailer\PHPMailer()); });
不要忘记安装邮件发送器所依赖的库,例如
composer require phpmailer/phpmailer
编写自定义邮件发送器
您可以编写自己的自定义邮件发送器。要编写自定义邮件发送器,您必须实现接口 Webiik\Mail\Mailer\MailerInterface
。
// CustomMailer.php declare(strict_types=1); namespace Webiik\Mail\Mailer; use Webiik\Mail\Message; class CustomMailer implements MailerInterface { // Your implementation...
getMailerCore
getMailerCore()
getMailerCore() 返回邮件发送器的核心库,例如 \PHPMailer\PHPMailer\PHPMailer()
$phpMailer = $mail->getMailerCore();
send
send(array $messages): array
send() 发送一个 Message 对象数组。返回未发送的电子邮件地址数组。要发送消息,您必须将 set mailer 设置为 Mail 类。
$mail->send($messages);
Message
createMessage
createMessage(): Message
createMessage() 返回一个新的 Message。
$message = $mail->createMessage();
可用消息方法的概述
杂项
setCharset(string $charset): void
getCharset(): string
setPriority(int $int): void
getPriority(): int
优先级 1-5(最高-最低),3 = 正常
发送者
setFrom(string $email, string $name = ''): void
getFrom(): array
setBounceAddress(string $email): void
getBounceAddress(): string
收件人
addTo(string $email, string $name = ''): void
getTo(): array
addReplyTo(string $email, string $name = ''): void
getReplyTo(): array
addCc(string $email, string $name = ''): void
getCc(): array
addBcc(string $email, string $name = ''): void
getBcc(): array
主题
setSubject(string $subject): void
getSubject(): string
正文
setBody(string $string, $mime = 'text/html'): void
getBody(): array
setAlternativeBody(string $string): void
getAlternativeBody(): string
为不支持 text/html 的客户端提供的替代正文。
附件
要附加现有文件,请使用
addFileAttachment(string $path, string $filename = '', string $mime = ''): void
getFileAttachments(): array
要附加动态生成的内容,请使用
addDynamicAttachment(string $string, string $filename, string $mime = ''): void
getDynamicAttachments(): array
嵌入
要嵌入现有图像,请使用
addFileEmbed(string $path, string $cid, string $filename = '', string $mime = ''): void
getFileEmbeds(): array
要嵌入动态生成的图像,请使用
addDynamicEmbed(string $string, string $cid, string $filename = '', string $mime = ''): void
getDynamicEmbeds(): array