This bundle provides Templating and Attachment Decorating for SwiftMailer with Symfony2
The SwiftMailer template decorator, handles e-mail messages using the Symfony Templating Component.
The SwiftMailer attachment decorator is similar to the Template decorator, but instead it handles mail attachments.
To install RollerworksMailBundle with Composer just add the following to your
composer.json
file:
// 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.*
.
Then, you can install the new dependencies by running Composer's update
command from the directory where your composer.json
file is located:
$ 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:
<?php
// in AppKernel::registerBundles()
$bundles = array(
// ...
new Rollerworks\Bundle\MailBundle\RollerworksMailBundle(),
// ...
);
First, checkout a copy of the code. Just add the following to the deps
file of your Symfony Standard Distribution:
[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
.
Then register the bundle with your kernel:
<?php
// in AppKernel::registerBundles()
$bundles = array(
// ...
new Rollerworks\Bundle\MailBundle\RollerworksMailBundle(),
// ...
);
Make sure that you also register the namespace with the autoloader:
<?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:
$ php bin/vendors install
Finally, enable the bundle in the kernel:
<?php
// in AppKernel::registerBundles()
$bundles = array(
// ...
new Rollerworks\Bundle\MailBundle\RollerworksMailBundle(),
// ...
);
Congratulations! You're ready!
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
// 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);
Note. This can be used in combination with the Template decorator.
Also see:
- http://swiftmailer.org/docs/plugins.html#decorator-plugin
- http://swiftmailer.org/docs/messages.html#attaching-files
For more in-dept detail.
When creating an new e-mail message add the following.
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:
array('data' => 'raw-file-content',
'filename' => 'some-file.txt',
'type' => 'optional mime-type')
Note: data must not be base64 encoded but provided as-is.
<?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);