Swift Mailer Emogrify Plugin
, (*1)
Inline CSS in the HTML output of SwiftMailer using Emogrifier., (*2)
Installation and requirements
Install via composer, using:, (*3)
composer require bummzack/swiftmailer-emogrifyplugin
Requirements:, (*4)
- PHP 5.6+
- SwiftMailer 5.x
- Emogrifier 3.x
Usage
By default, the plugin will inline CSS that is part of the HTML, eg. styles defined in <style>
tags.
You can instantiate the plugin with your own Emogrifier
instance or change properties of the emogrifier instance.
For a list of options, please head over to the Emogrifier documentation., (*5)
Please note, that the plugin is using one instance of Emogrifier
to convert all message-parts,
so the settings you make apply to all converted html parts., (*6)
Supplying custom CSS
$plugin = new EmogrifierPlugin();
$plugin->getEmogrifier()->setCss('.customStyle: { color: red; };');
Please note: Calling setHtml
on the Emogrifier instance doesn't have an effect, since it will be replaced with
the message body!, (*7)
Example
Here's how you could use the plugin to send emails with custom styles loaded from a file:, (*8)
$emogrifier = new Pelago\Emogrifier();
$emogrifier->setCss(file_get_contents( /* path to your CSS file */ ));
// Create the Mailer using any Transport
$mailer = new Swift_Mailer(
new Swift_SmtpTransport('smtp.example.org', 25)
);
// Use Emogrifier plugin to inline styles. You can pass the emogrifier instance as a parameter
$mailer->registerPlugin(new Bummzack\SwiftMailer\EmogrifyPlugin\EmogrifierPlugin($emogrifier));
$message = new Swift_Message();
$message
->setSubject('Your subject')
->setFrom(['test@example.com' => 'Test'])
->setTo(['receiver@example.com'])
->setBody('
My custom HTML, (*9)
', 'text/html');
// Send your email
$mailer->send($message);