pelago/emogrifier
Emogrifier converts CSS from blocks and stylesheets into inline style attributes in HTML. Ideal for HTML email rendering in clients with poor CSS support (e.g., Outlook, Gmail), ensuring consistent styling across email and mobile readers.
Mailables or Mailable classes).HtmlPruner, CssToAttributeConverter) for optimization, aligning with Laravel’s service-layer patterns.EmailServiceProvider with a CssInliner facade).@emogrify) to inline CSS during template rendering.Mail::send() with dynamic HTML).:hover, @media queries beyond all|screen|print). Risk mitigated by allowlisting critical selectors.{{ $user->id }}) may break selector matching. Solution: Sanitize/whitelist dynamic selectors.DOMDocument, which can be memory-intensive for malformed HTML. Validate input with Laravel’s Html facade or tidy.CssInliner::fromHtml() on production-scale templates.)@font-face) be handled? (Option: Graceful degradation with addExcludedCssSelector().)Mailable to use CssInliner in build() or render().Swift_Transport for SMTP delivery.@emogrify) to inline CSS during template compilation.CssInliner in unit tests for email services.CssInliner.EmailRenderer service class:
class EmailRenderer {
public function render(Mailable $mailable): string {
$html = $mailable->render();
return CssInliner::fromHtml($html)
->inlineCss($mailable->css())
->render();
}
}
RenderEmailJob) for async processing.HtmlPruner to remove display: none elements and redundant classes.Cache::remember()).@apply sparingly (may generate unsupported selectors).data-user-id) for dynamic styling.@import or external stylesheets).<table> for layouts, not divs).@emogrify($css)).telescope:metrics).log channel.->getDomDocument()->saveHTML() to inspect intermediate HTML.disableStyleBlocksParsing()).:nth-child). Solution: Manual overrides or addExcludedCssSelector().memory_limit for large templates (e.g., 1024M).CssInliner execution.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Malformed HTML input | Crashes DOMDocument |
Validate with filter_var($html, FILTER_VALIDATE_HTML) or tidy. |
| Unsupported CSS selectors | Partial rendering | Log warnings; use addExcludedCssSelector() for unsupported rules. |
| High memory usage | Worker timeouts | Optimize templates; increase memory_limit. |
| Queue job failures | Delayed emails | Implement retries with exponential backoff. |
| CSS variable evaluation failures |
How can I help you explore Laravel packages today?