Skip to content

Add CSS content support #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config/css-inliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@

'css-files' => [],

/*
|--------------------------------------------------------------------------
| Css Content
|--------------------------------------------------------------------------
|
| Optionally, you may prefer to provide the CSS content as a String.
| This allows for compatibility with Vite by using Vite::content()
| If supplied, this string will be appended after your css-files.
|
*/

'css-content' => null,
];
35 changes: 31 additions & 4 deletions src/CssInlinerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
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 TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;

class CssInlinerPlugin
{
private array $config;

private CssToInlineStyles $converter;

private string $cssToAlwaysInclude;

public function __construct(array $filesToInline = [], ?CssToInlineStyles $converter = null)
public function __construct(array $config, ?CssToInlineStyles $converter = null)
{
$this->cssToAlwaysInclude = $this->loadCssFromFiles($filesToInline);

$this->config = $config;

$this->buildCssContent();

$this->converter = $converter ?? new CssToInlineStyles;
}
Expand All @@ -48,6 +51,30 @@ public function handleSymfonyEvent(MessageEvent $event): void
$this->handleSymfonyEmail($message);
}

private function buildCssContent(): void
{
$this->addCssFromFiles();
$this->addCssFromContent();
}

private function addCssFromFiles(): void
{
$filesToInline = $this->config['css-files'] ?? [];

$this->cssToAlwaysInclude = $this->loadCssFromFiles($filesToInline);
}

private function addCssFromContent(): void
{
$contentToInline = $this->config['css-content'] ?? null;

if (! $contentToInline) {
return;
}

$this->cssToAlwaysInclude .= $contentToInline;
}

private function processPart(AbstractPart $part): AbstractPart
{
if ($part instanceof TextPart && $part->getMediaType() === 'text' && $part->getMediaSubtype() === 'html') {
Expand Down
2 changes: 1 addition & 1 deletion src/LaravelMailCssInlinerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function register(): void
$this->mergeConfigFrom(__DIR__ . '/../config/css-inliner.php', 'css-files');

$this->app->singleton(CssInlinerPlugin::class, function ($app) {
return new CssInlinerPlugin($app['config']->get('css-inliner.css-files', []));
return new CssInlinerPlugin($app['config']->get('css-inliner', []));
});

Event::listen(MessageSending::class, CssInlinerPlugin::class);
Expand Down
20 changes: 17 additions & 3 deletions tests/CssInlinerPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ public function test_it_should_convert_html_body_with_given_css(): void
$this->assertBodyMatchesStub($message, 'converted-html-with-css');
}

public function test_it_should_convert_html_body_with_given_css_content(): void
{
$message = $this->fakeSendMessageUsingInlinePlugin(
(new Email)->html($this->stubs['original-html-with-css']),
[],
file_get_contents(__DIR__ . '/css/test.css')
);

$this->assertBodyMatchesStub($message, 'converted-html-with-css');
}

public function test_it_should_convert_html_body_with_given_css_and_attachment(): void
{
$originalMessage = $this->createMessageToSend(
Expand Down Expand Up @@ -305,13 +316,16 @@ private function getTextFromPart(AbstractPart $part, string $mediaSubType = 'htm
return null;
}

private function fakeSendMessageUsingInlinePlugin(Email $message, array $inlineCssFiles = []): Email
private function fakeSendMessageUsingInlinePlugin(Email $message, array $inlineCssFiles = [], string $inlineCssContent = null): Email
{
$processedMessage = null;

$dispatcher = new EventDispatcher;
$dispatcher->addListener(MessageEvent::class, static function (MessageEvent $event) use ($inlineCssFiles, &$processedMessage) {
$handler = new CssInlinerPlugin($inlineCssFiles);
$dispatcher->addListener(MessageEvent::class, static function (MessageEvent $event) use ($inlineCssFiles, $inlineCssContent, &$processedMessage) {
$handler = new CssInlinerPlugin([
'css-files' => $inlineCssFiles,
'css-content' => $inlineCssContent,
]);

$handler->handleSymfonyEvent($event);

Expand Down