Skip to content

Add CSS content support - Fixes #319 #324

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

Closed
wants to merge 1 commit into from
Closed
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
@@ -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,
];
36 changes: 32 additions & 4 deletions src/CssInlinerPlugin.php
Original file line number Diff line number Diff line change
@@ -3,25 +3,29 @@
namespace Fedeisas\LaravelMailCssInliner;

use DOMDocument;
use Illuminate\Contracts\Foundation\Application;
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 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;
}
@@ -48,6 +52,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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If both functions are used to add to the $this->cssToAlwaysInclude perhaps it would be best to also use the .= here as then the order of method calling has no side effects.

Suggested change
$this->cssToAlwaysInclude = $this->loadCssFromFiles($filesToInline);
$this->cssToAlwaysInclude .= $this->loadCssFromFiles($filesToInline);

}

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

if (! $contentToInline) {
return;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Insert this line

if ($contentToInline instanceof \Closure)  $contentToInline = $contentToInline();

In most cases, the css content will only be used (and this code reached) when an email is being prepared for sending, which may be no more than 1% of page loads depending on the project. So for better performance, lazy loading should be allowed.

With the line above inserted, instead of passing Vite::content($asset) directly, we can pass fn() => Vite::content($asset).

$this->cssToAlwaysInclude .= $contentToInline;
}

private function processPart(AbstractPart $part): AbstractPart
{
if ($part instanceof TextPart && $part->getMediaType() === 'text' && $part->getMediaSubtype() === 'html') {
2 changes: 1 addition & 1 deletion src/LaravelMailCssInlinerServiceProvider.php
Original file line number Diff line number Diff line change
@@ -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);
20 changes: 17 additions & 3 deletions tests/CssInlinerPluginTest.php
Original file line number Diff line number Diff line change
@@ -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(
@@ -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);