rollerworks / mail-bundle
Swiftmailer, Symfony2 模板/装饰器包
Requires
This package is not auto-updated.
Last update: 2022-02-01 12:20:37 UTC
README
此包为 SwiftMailer 提供了在 Symfony2 下的模板和附件装饰功能
模板
SwiftMailer 模板装饰器,使用 Symfony 模板组件处理电子邮件消息。
附件装饰器
SwiftMailer 附件装饰器与模板装饰器类似,但它处理邮件附件。
安装
步骤 1: 使用 Composer (推荐)
使用 Composer 安装 RollerworksMailBundle,只需将以下内容添加到您的 composer.json
文件中
// composer.json { // ... require: { // ... "rollerworks/mail-bundle": "master-dev" } }
注意:请将上面片段中的 master-dev
替换为最新的稳定分支,例如 1.0.*
。
然后,您可以通过在 composer.json
文件所在的目录中运行 Composer 的 update
命令来安装新的依赖项
$ php composer.phar update
现在,Composer 将自动下载所有必需的文件,并为您安装它们。接下来,您需要更新您的 AppKernel.php
文件,并注册新的包
<?php // in AppKernel::registerBundles() $bundles = array( // ... new Rollerworks\Bundle\MailBundle\RollerworksMailBundle(), // ... );
步骤 1 (替代): 使用 deps
文件 (Symfony 2.0.x)
首先,检出代码副本。只需将以下内容添加到您的 Symfony 标准分布的 deps
文件中
[RollerworksMailBundle] git=http://github.com/rollerworks/RollerworksMailBundle.git target=/bundles/Rollerworks/Bundle/MailBundle
注意:您可以在上述片段中添加 version
标签,例如 version=origin/1.0
。
然后,在您的内核中注册此包
<?php // in AppKernel::registerBundles() $bundles = array( // ... new Rollerworks\Bundle\MailBundle\RollerworksMailBundle(), // ... );
确保您还向自动加载器注册了命名空间
<?php // app/autoload.php $loader->registerNamespaces(array( // ... 'Rollerworks' => __DIR__.'/../vendor/bundles', // ... ));
现在,使用 vendors
脚本将新添加的存储库克隆到您的项目中
$ php bin/vendors install
步骤 3: 启用包
最后,在内核中启用包
<?php // in AppKernel::registerBundles() $bundles = array( // ... new Rollerworks\Bundle\MailBundle\RollerworksMailBundle(), // ... );
恭喜!您已准备就绪!
基本用法(模板装饰器)
有关更详细的信息,请参阅 http://swiftmailer.org/docs/plugins.html#decorator-plugin
创建新的电子邮件消息时,请添加以下内容。
<?php // Replacements must be an array or implementation of \Swift_Plugins_Decorator_Replacements // Each key is an e-mail address and the value an array that is directly passed to render() of the templating engine. $replacements = array( "address1@domain.tld" => array("a" => "b", "c" => "d"), "address2@domain.tld" => array("a" => "x", "c" => "y") ); // Template filename follows the Symfony template resolving convention ([Bundle]:[Dir]:[filename].[type].[ext]). $templates = array( 'html' => 'AcmeHelloBundle:Email:Order.html.twig', 'text' => 'AcmeHelloBundle:Email:Order.txt.twig' ); $templating = $container->get('templating'); $decorator = new \Rollerworks\Bundle\MailBundle\Decorator\TemplateDecorator($templating, $replacements, $templates); $mailer->registerPlugin($decorator);
基本用法(附件装饰器)
注意。这可以与模板装饰器结合使用。
也请参阅
- http://swiftmailer.org/docs/plugins.html#decorator-plugin
- http://swiftmailer.org/docs/messages.html#attaching-files
以获取更详细的信息。
创建新的电子邮件消息时,请添加以下内容。
替换必须是数组或 \Swift_Plugins_Decorator_Replacements 的实现。每个键是一个电子邮件地址,值是一个包含附件的数组。附件可以是 \Swift_Attachment 对象或具有以下键和数据数组的数组
array('data' => 'raw-file-content', 'filename' => 'some-file.txt', 'type' => 'optional mime-type')
注意:数据必须不进行 base64 编码,而应提供为原始数据。
<?php $replacements = array( "address1@domain.tld" => array(new \Swift_Attachment::fromPath('/path/to/image.jpg', 'image/jpeg')), "address2@domain.tld" => array(array('data' => 'Please read me...', 'filename' => 'agreement.txt', 'type' => 'text/plain')) ); $decorator = new \Rollerworks\Bundle\MailBundle\Decorator\TemplateDecorator($replacements); $mailer->registerPlugin($decorator);