Skip to content

Commit

Permalink
Merge pull request #152 from JaZo/bugfix/css-inline-with-symfony-mailer
Browse files Browse the repository at this point in the history
Add Symfony Mailer CSS inliner plugin
  • Loading branch information
emilsundberg authored Jun 19, 2023
2 parents 89caa19 + d0c88ba commit 9e8648e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/Snowfire/Beautymail/BeautymailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Snowfire\Beautymail;

use Illuminate\Mail\Events\MessageSending;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;

class BeautymailServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -30,10 +32,14 @@ public function boot()

$this->loadViewsFrom(__DIR__.'/../../views', 'beautymail');

try {
$this->app['mailer']->getSwiftMailer()->registerPlugin(new CssInlinerPlugin());
} catch (\Exception $e) {
\Log::debug('Skipped registering SwiftMailer plugin: CssInlinerPlugin.');
if (version_compare($this->app->version(), '9.0', '>=')) {
Event::listen('Illuminate\Mail\Events\MessageSending', 'Snowfire\Beautymail\SymfonyCssInlinerPlugin');
} else {
try {
$this->app['mailer']->getSwiftMailer()->registerPlugin(new SwiftCssInlinerPlugin());
} catch (\Exception $e) {
\Log::debug('Skipped registering SwiftMailer plugin: CssInlinerPlugin.');
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Pelago\Emogrifier\CssInliner;

class CssInlinerPlugin implements \Swift_Events_SendListener
class SwiftCssInlinerPlugin implements \Swift_Events_SendListener
{
/**
* Inline the CSS before an email is sent.
Expand Down
97 changes: 97 additions & 0 deletions src/Snowfire/Beautymail/SymfonyCssInlinerPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Snowfire\Beautymail;

use Illuminate\Mail\Events\MessageSending;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mime\Part\AbstractPart;
use Symfony\Component\Mime\Part\AbstractMultipartPart;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Symfony\Component\Mime\Part\Multipart\MixedPart;
use Symfony\Component\Mime\Part\TextPart;
use Pelago\Emogrifier\CssInliner;

class SymfonyCssInlinerPlugin
{
/**
* @param \Illuminate\Mail\Events\MessageSending $event
*
* @return void
*/
public function handle(MessageSending $event)
{
$message = $event->message;

if (!$message instanceof Email) {
return;
}

$this->handleSymfonyEmail($message);
}

/**
* @param \Symfony\Component\Mime\Email $message
*
* @return void
*/
private function handleSymfonyEmail(Email $message)
{
$body = $message->getBody();

if ($body === null) {
return;
}

if ($body instanceof TextPart) {
$message->setBody($this->processPart($body));
} elseif ($body instanceof AlternativePart || $body instanceof MixedPart) {
$partType = get_class($body);
$message->setBody(new $partType(
...array_map(
function (AbstractPart $part) {
return $this->processPart($part);
},
$body->getParts()
)
));
}
}

/**
* @param \Symfony\Component\Mime\Part\AbstractPart $part
*
* @return \Symfony\Component\Mime\Part\AbstractPart
*/
private function processPart(AbstractPart $part)
{
if ($part instanceof TextPart && $part->getMediaType() === 'text' && $part->getMediaSubtype() === 'html') {
return $this->processHtmlTextPart($part);
} else if ($part instanceof AbstractMultipartPart) {
$partClass = get_class($part);
$parts = [];

foreach ($part->getParts() as $childPart) {
$parts[] = $this->processPart($childPart);
}

return new $partClass(...$parts);
}

return $part;
}

/**
* @param \Symfony\Component\Mime\Part\TextPart $part
*
* @return \Symfony\Component\Mime\Part\TextPart
*/
private function processHtmlTextPart(TextPart $part)
{
return new TextPart(
CssInliner::fromHtml($part->getBody())->inlineCss()->render(),
$part->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8',
'html'
);
}
}

0 comments on commit 9e8648e

Please sign in to comment.