RollerworksMailBundle
This bundle provides Templating and Attachment Decorating for SwiftMailer with Symfony2, (*1)
Template
The SwiftMailer template decorator, handles e-mail messages using the Symfony Templating Component., (*2)
AttachmentDecorator
The SwiftMailer attachment decorator is similar to the Template decorator,
but instead it handles mail attachments., (*3)
Installation
Step 1: Using Composer (recommended)
To install RollerworksMailBundle with Composer just add the following to your
composer.json
file:, (*4)
// composer.json
{
// ...
require: {
// ...
"rollerworks/mail-bundle": "master-dev"
}
}
NOTE: Please replace master-dev
in the snippet above with the latest stable
branch, for example 1.0.*
., (*5)
Then, you can install the new dependencies by running Composer's update
command from the directory where your composer.json
file is located:, (*6)
$ php composer.phar update
Now, Composer will automatically download all required files, and install them
for you. All that is left to do is to update your AppKernel.php
file, and
register the new bundle:, (*7)
<?php
// in AppKernel::registerBundles()
$bundles = array(
// ...
new Rollerworks\Bundle\MailBundle\RollerworksMailBundle(),
// ...
);
Step 1 (alternative): Using deps
file (Symfony 2.0.x)
First, checkout a copy of the code. Just add the following to the deps
file of your Symfony Standard Distribution:, (*8)
[RollerworksMailBundle]
git=http://github.com/rollerworks/RollerworksMailBundle.git
target=/bundles/Rollerworks/Bundle/MailBundle
NOTE: You can add version
tag in the snippet above with the latest stable
branch, for example version=origin/1.0
., (*9)
Then register the bundle with your kernel:, (*10)
<?php
// in AppKernel::registerBundles()
$bundles = array(
// ...
new Rollerworks\Bundle\MailBundle\RollerworksMailBundle(),
// ...
);
Make sure that you also register the namespace with the autoloader:, (*11)
<?php
// app/autoload.php
$loader->registerNamespaces(array(
// ...
'Rollerworks' => __DIR__.'/../vendor/bundles',
// ...
));
Now use the vendors
script to clone the newly added repositories
into your project:, (*12)
$ php bin/vendors install
Step3: Enable the bundle
Finally, enable the bundle in the kernel:, (*13)
``` php
<?php, (*14)
// in AppKernel::registerBundles()
$bundles = array(
// ...
new Rollerworks\Bundle\MailBundle\RollerworksMailBundle(),
// ...
);, (*15)
Congratulations! You're ready!
## Basic Usage (Template decorator)
___See http://swiftmailer.org/docs/plugins.html#decorator-plugin for more in-dept details.___
When creating an new e-mail message add the following.
``` php
<?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);
Basic Usage (Attachment decorator)
Note. This can be used in combination with the Template decorator., (*16)
Also see:, (*17)
- http://swiftmailer.org/docs/plugins.html#decorator-plugin
- http://swiftmailer.org/docs/messages.html#attaching-files
For more in-dept detail., (*18)
When creating an new e-mail message add the following., (*19)
Replacements must be an array or implementation of \Swift_Plugins_Decorator_Replacements
Each key is an e-mail address and the value an array with attachments.
An attachment is either a \Swift_Attachment object or an array with the following keys and data:, (*20)
array('data' => 'raw-file-content',
'filename' => 'some-file.txt',
'type' => 'optional mime-type')
Note: data must not be base64 encoded but provided as-is., (*21)
<?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);