Skip to content

Instantly share code, notes, and snippets.

@wpottier
Created April 7, 2016 13:45
Show Gist options
  • Select an option

  • Save wpottier/4d493d4ff53f9abc1701ee64af035cdc to your computer and use it in GitHub Desktop.

Select an option

Save wpottier/4d493d4ff53f9abc1701ee64af035cdc to your computer and use it in GitHub Desktop.
Swiftmailer Css Inliner plugin
<?php
namespace AppBundle\Swiftmailer\Plugins;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
class CssInlinerPlugin implements \Swift_Events_SendListener
{
/**
* @var CssToInlineStyles
*/
private $converter;
/**
* @param CssToInlineStyles $converter
*/
public function __construct(CssToInlineStyles $converter)
{
$this->converter = $converter;
}
/**
* @param \Swift_Events_SendEvent $event
*/
public function beforeSendPerformed(\Swift_Events_SendEvent $event)
{
$message = $event->getMessage();
if ($message->getContentType() === 'text/html') {
$this->parse($message);
}
foreach ($message->getChildren() as $part) {
if (0 !== strpos($part->getContentType(), 'text/html')) {
continue;
}
$this->parse($part);
}
}
/**
* Do nothing
*
* @param \Swift_Events_SendEvent $event
*/
public function sendPerformed(\Swift_Events_SendEvent $event)
{
// Do Nothing
}
/**
* @param \Swift_Mime_MimeEntity $mimeEntity
*
* @throws \TijsVerkoyen\CssToInlineStyles\Exception
*/
protected function parse(\Swift_Mime_MimeEntity $mimeEntity)
{
$this->converter->setCSS('');
$this->converter->setHTML($mimeEntity->getBody());
$mimeEntity->setBody($this->converter->convert());
}
}
services:
css_inliner:
class: 'TijsVerkoyen\CssToInlineStyles\CssToInlineStyles'
public: false
calls:
- ['setUseInlineStylesBlock', [true]]
swift_css_inliner_plugin:
class: 'AppBundle\Swiftmailer\Plugins\CssInlinerPlugin'
public: false
arguments:
- '@css_inliner'
tags:
- {name: 'swiftmailer.default.plugin'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment