hautzi / system-mail-bundle
此包提供了一个发送系统邮件的优雅抽象
0.9.0
2020-03-19 09:47 UTC
Requires
- php: >=7.0
- symfony/http-kernel: ~4.0|~5.0
- symfony/swiftmailer-bundle: ~3.0
- twig/twig: ~2.0
Requires (Dev)
- phpunit/phpunit: ~4.6
README
此包将发送系统消息的过程抽象化。如果您在控制器中实例化 Swift_Message
实例感到不自然,这可能正是您需要的。
此包的想法是为要发送的每封系统邮件创建某种类型的 XML 配置(例如“您已成功注册,请激活您的账户”或“嘿,老兄,您在我们的平台上已经浪费了10天的钱,请回来”),利用 twig 的强大功能。
嘿,为什么不去看看代码呢?它有点自解释
// AppBundle/Resources/emails/send-info.xml.twig <?xml version="1.0"?> <email xmlns="http://christoph-hautzinger.de/schema/system-mail-bundle" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://christoph-hautzinger.de/schema/system-mail-bundle http://christoph-hautzinger.de/schema/system-mail-bundle/email-1.0.xsd"> <to name="{{ user.name }}">{{ user.email }}</to> <subject locale="de">Hallo {{ user.name }}</subject> <subject locale="en">Hello {{ user.name }}</subject> <messageHtml locale="en"><![CDATA[ Hallo {{ user.name }}, <a href="{{ url('') }}">please click this link</a>. ]]></messageHtml> <messageText locale="en" removeIndent="true"> Hello {{ user.name }}, please click this link: {{ url('') }} </messageText> </email>
然后通过简单地调用以下方式显式发送此邮件
// from your code simply call $container->get('system_mailer')->send('emails/send-info.xml.twig', [ 'user' => $user, ]);
安装
步骤 1:下载包
打开命令行界面,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本
$ composer require hautzi/system-mail-bundle
此命令要求您全局安装了 Composer,如 Composer 文档中的安装章节所述。
步骤 2:启用包
然后,通过在您的项目 app/AppKernel.php
文件中添加以下行来启用此包
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Hautzi\SystemMailBundle\HautziSystemMailBundle(), ); // ... } // ... }
配置选项
目前,您可以提供默认数据
# in your app/config.yml hautzi_system_mail: # defaults that are set for each mail, use blank # if you don't want to set this defaults: subject: ~ replyTo: ~ from: email: ~ name: ~ to: email: ~ name: ~ cc: email: ~ name: ~ bcc: email: ~ name: ~
示例
$systemMailer = $container->get('system_mailer'); // sends out AppBundle/Resources/emails/registration/confirmUser.xml.twig $systemMailer->send('emails/registration/confirmUser.xml.twig', ['user' => $user]); // force locale of sent mail (when the recipient speaks another language than the user in the session) $systemMailer->send('emails/info-mail.xml.twig', ['user' => $user], 'de'); // attach file to mail (or do something else with the Swift_Message instance) $systemMailer->send('emails/message-with-pdf.xml.twig', [], null, function (\Swift_Message $message) { $message->attach(\Swift_Attachment::fromPath('my-document.pdf')) });
许可证
此包采用 MIT 许可证。请参阅包根目录中的 LICENSE
文件中的完整许可证。