[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用户:49.233.*.*
13 小时前 aiohttp
IP用户:153.3.*.*
19 小时前 Chrome GNU/Linux
IP用户:39.173.*.*
1 天前 Generic Bot
IP用户:158.220.*.*
1 天前 MJ12 Bot
IP用户:175.167.*.*
3 天前 Internet Explorer Windows 7
IP用户:112.13.*.*
3 天前 Generic Bot
IP用户:14.153.*.*
4 天前 Chrome Webview Android 7.1
IP用户:66.249.*.*
5 天前 Googlebot
IP用户:54.36.*.*
5 天前 aHrefs Bot
IP用户:221.194.*.*
6 天前 Microsoft Edge Windows 10
IP用户:114.119.*.*
8 天前 Petal Bot
IP用户:81.70.*.*
9 天前 aiohttp
累计浏览次数:1587
评论
点击登录
phpreturn,PHP武器库,专注PHP领域的项目和资讯,收录和介绍PHP相关项目。
最近浏览 点击登录
累计浏览次数:186539
一周浏览次数:1414
今日浏览次数:38

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

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

鲁ICP备19027671号-2