-
Notifications
You must be signed in to change notification settings - Fork 115
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
|
||
private function addCssFromContent(): void | ||
{ | ||
$contentToInline = $this->config['css-content'] ?? null; | ||
|
||
if (! $contentToInline) { | ||
return; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Insert this line
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 |
||
$this->cssToAlwaysInclude .= $contentToInline; | ||
} | ||
|
||
private function processPart(AbstractPart $part): AbstractPart | ||
{ | ||
if ($part instanceof TextPart && $part->getMediaType() === 'text' && $part->getMediaSubtype() === 'html') { | ||
|
There was a problem hiding this comment.
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.