happyr / mailer-bundle
Happyr MailerBundle 允许您发送美观的 HTML 邮件。使用 twig 模板设计您的邮件。您甚至可以使用附件。
2.1.6
2018-11-06 12:59 UTC
Requires
- php: ^7.1
- swiftmailer/swiftmailer: ^5.4 || ^6.0
- symfony/http-foundation: ^3.4 || ^4.0
- symfony/translation: ^3.4 || ^4.0
- symfony/yaml: ^3.4 || ^4.0
- twig/twig: ^2.4.8
Requires (Dev)
- mockery/mockery: 1.1
- phpunit/phpunit: ^6.5.9
README
Happyr Mailer Bundle 让您在 Symfony2 应用程序中发送 HTML 邮件变得更容易。此包支持模板渲染和发送附件。
安装
步骤 1:使用 Composer
使用 Composer 安装它!
composer require happyr/mailer-bundle
步骤 2:注册包
要将包注册到您的内核中
<?php // in AppKernel::registerBundles() $bundles = array( // ... new Happyr\MailerBundle\HappyrMailerBundle(), // ... );
步骤 3:配置包
# app/config/config.yml happyr_mailer: // ... from: email: you@company.com name: Your name
您可以在这里找到完整的配置参考。
用法
<?php // AnyController.php public function anyAction(){ $mailer=$this->get('happyr.mailer'); $mailer->send('me@domain.com','AnyBundle:Email:test.html.twig'); }
附件:如果您想发送附件,需要将它们添加到参数数组中。
$this->send($mail, $template, array('user'=>$user, 'attachments'=> array( //two attachments. You must specify either 'data' or 'path' array( 'data'=>$bindaryPdf, 'contentType'=>'application/pdf', 'filename'=>'Invoice.pdf', ), array( 'path'=>$pathToPdf, 'contentType'=>'application/pdf', 'filename'=>'Welcome.pdf', ), )));
邮件头:您可以在消息中添加额外的头信息。
$this->send($mail, $template, array('user'=>$user, 'message_headers'=> array( 'X-Mailgun-Variables' => json_encode(['foobar'=>'baz']) )));
从 Symfony 命令发送邮件
如果您想从 Symfony2 命令发送邮件,您通常会遇到如下错误:您无法创建一个请求对象("request")的无效作用域("request")。
或您无法创建一个服务("templating.helper.assets")的无效作用域("request")。
错误发生是因为您无法访问请求对象。此包可以帮助您伪造请求对象。您需要更改一些配置
# app/config/config.yml happyr_mailer: fake_request: true #default value is false
如果请求对象不存在,我们将帮助您创建它。
更新日志
1.3.0 在不获得如 "您无法创建一个请求对象("request")的无效作用域("request")" 这样的错误之前,无法从控制台命令发送邮件。
1.2.0 您将不再从 swift 获得异常。如果您想捕获异常,请使用 Happyr\MailerBundle\Exceptions\MailException。
您现在可以使用 error_type 配置来选择如何处理错误。
# app/config/config.yml happyr_mailer: error_type: 'exception' #other possible values are 'error', 'warning', 'notice' and 'none'