cnerta / mailing-bundle
该包已被弃用,不再维护。未建议替代包。
CnertaMailinBundle 是一个 Symfony2 Bundle,它提供了一种使用 Twig 模板轻松发送邮件的方法。
1.0.1
2015-10-13 07:57 UTC
Requires
- php: >=5.3.3
- symfony/swiftmailer-bundle: ~2.3
- symfony/symfony: >=2.5
- twig/extensions: ~1.0
Requires (Dev)
- phpunit/phpunit: ~4.8
- symfony/monolog-bundle: ~2.4
Suggests
- symfony/monolog-bundle: Allows more advanced logging of the application flow
This package is not auto-updated.
Last update: 2020-04-17 17:48:36 UTC
README
CnertaMailingBundle
CnertaMailinBundle 是一个提供使用 Twig 模板发送邮件的简单方法的 Symfony2 Bundle。
使用 Twig 创建您的电子邮件消息,并使用 CnertaMailinBundle 的服务来发送它。
简单、快速,建立在 SwiftMail 等巨人的肩膀上。
安装
Composer
添加到 composer.json 文件中
"require": { //.. "cnerta/mailing-bundle": "dev-master" }
运行
$ composer install cnerta/mailing-bundle
在您的 AppKernel
类中注册此包。
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Cnerta\MailingBundle\CnertaMailingBundle(), ); // ... } // ...
配置包
在 config.yml
中
cnerta_mailing: default_bundle: "FooBundle" # The name of the bundle where the email template are stored active_log: false # Defined if you want to log each sending email from_email: address: exemple@exemple.com sender_name: "My name is"
如何使用
创建邮件模板
- 在您的
src/AppBundle/Resources
中创建一个Mails
文件夹 - 在这个新文件夹中创建一个
BlocksMail.html.twig
- 创建一个
default.html.twig
- 创建一个
default.txt.twig
BlocksMail.html.twig
必须包含邮件中所有对象和正文部分。例如
{% block bar_object %}A mail object{% endblock %} {% block bar_body %} A Body with full of pretty things ! {% endblock %}
default.html.twig
是您邮件的基本模板。例如
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <strong>Hello dear user</strong> <div> {{ body|raw }} {# This is mandatory, it's the body message of your mail #} </div> </body> </html>
default.txt.twig
是您邮件的基本模板。例如
Hello dear user</strong {{ body|raw }} {# This is mandatory, it's the body message of your mail in text version (without HTML elements) #}
发送邮件!
服务名称: cnerta.mailing
在一个 Controller
中
use Cnerta\MailingBundle\Mailing\MailingServiceInterface; use Cnerta\MailingBundle\Mailing\MailParameters; use Cnerta\MailingBundle\Mailing\MailParametersInterface; [...] public function fooAction() { $mailParameters = MailParameters(); $mailParameters ->setTemplateBundle('MyBundle') ->addBodyParameters("user", "User name"); $this->get('cnerta.mailing') ->sendEmail( array("user@exemple.com"), // List of mail address or Symfony\Component\Security\Core\User\UserInterface "template_email", // Name of the block define in `BlocksMail.html.twig` $mailParameters); }
将所有发送的电子邮件记录在特定通道中
有时您需要知道系统发送的电子邮件内容,此包提供了一种记录所有消息的简单方法。
此包使用 mailingbundle
通道来写入日志。
因此,如果您想将特定消息记录到单独的日志文件中,请使用以下配置。
monolog: handlers: myMailingChannel: # log all messages (since debug is the lowest level) level: debug type: stream path: "%kernel.logs_dir%/mailing.log" channels: [mailingbundle]