邮件似乎已经过时了,但在某些场景中,邮件通知仍然是最合适的方案。
相比微信提醒和钉钉提醒,邮件提醒不限平台,并且拥有更强的内容展示能力,并且各类操作系统都有强大的邮件客户端,如果我们自己搭建一个邮箱服务器,任何发送还都是免费的,我们还是会经常与邮件打交道。
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编译为内联样式等。是我们发送邮件时的首选工具。