[symfony/mailer]一个优雅易用的发送邮件类库

2022-04-05 奥古斯宏

邮件似乎已经过时了,但在某些场景中,邮件通知仍然是最合适的方案。

相比微信提醒和钉钉提醒,邮件提醒不限平台,并且拥有更强的内容展示能力,并且各类操作系统都有强大的邮件客户端,如果我们自己搭建一个邮箱服务器,任何发送还都是免费的,我们还是会经常与邮件打交道。

symfony/mailer是新一代的邮件操作库,使用它发送邮件,比写数据库查询还要简单:

use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;

$transport = Transport::fromDsn('smtp://localhost');
$mailer = new Mailer($transport);

$email = (new Email())
    ->from('hello@example.com')
    ->to('you@example.com')
    //->cc('cc@example.com')
    //->bcc('bcc@example.com')
    //->replyTo('fabien@example.com')
    //->priority(Email::PRIORITY_HIGH)
    ->subject('Time for Symfony Mailer!')
    ->text('Sending emails is fun again!')
    ->html('<p>See Twig integration for better HTML integration!</p>');

$mailer->send($email);

他支持标准的邮箱协议:

smtp smtp://user:pass@smtp.example.com:25 SMTP 服务器
sendmail sendmail://default 使用本地邮箱客户端发送
native native://default 使用php配置的邮箱目录

还内置了流行的邮箱服务平台:

Amazon SES composer require symfony/amazon-mailer
Gmail composer require symfony/google-mailer
MailChimp composer require symfony/mailchimp-mailer
Mailgun composer require symfony/mailgun-mailer
Mailjet composer require symfony/mailjet-mailer
Postmark composer require symfony/postmark-mailer
SendGrid composer require symfony/sendgrid-mailer
Sendinblue composer require symfony/sendinblue-mailer
OhMySMTP composer require symfony/oh-my-smtp-mailer

它还内置了许多高级组件操作,比如html模板、将css编译为内联样式等。是我们发送邮件时的首选工具。


版权声明:本文由phpreturn.com(PHP武器库官网)原创和首发,所有权利归phpreturn(PHP武器库)所有,本站允许任何形式的转载/引用文章,但必须同时注明出处。

最近浏览
IP用户:140.205.*.*
12 小时前 Generic Bot
IP用户:34.64.*.*
1 天前 Googlebot
IP用户:34.64.*.*
1 天前 Googlebot
IP用户:74.125.*.*
1 天前 Googlebot
IP用户:34.64.*.*
2 天前 Googlebot
IP用户:52.167.*.*
3 天前 BingBot
IP用户:40.77.*.*
4 天前 BingBot
IP用户:54.36.*.*
4 天前 aHrefs Bot
IP用户:66.249.*.*
5 天前 Googlebot
IP用户:101.67.*.*
5 天前 Generic Bot
IP用户:101.91.*.*
7 天前 Chrome Mac 10.15
IP用户:72.14.*.*
8 天前 Googlebot
累计浏览次数:949
评论
点击登录
phpreturn,PHP武器库,专注PHP领域的项目和资讯,收录和介绍PHP相关项目。
最近浏览 点击登录
累计浏览次数:101905
一周浏览次数:2777
今日浏览次数:32

本站所有权利归 phpreturn.com 所有

举报/反馈/投稿邮箱:phpreturn@ulthon.com

鲁ICP备19027671号-2