Skip to content

Inline mail CSS via AssetMapper instead of static file path - #242

Open
jonasdekeukelaere wants to merge 3 commits into
masterfrom
inline-css-mails
Open

Inline mail CSS via AssetMapper instead of static file path#242
jonasdekeukelaere wants to merge 3 commits into
masterfrom
inline-css-mails

Conversation

@jonasdekeukelaere

@jonasdekeukelaere jonasdekeukelaere commented Aug 12, 2026

Copy link
Copy Markdown
Member

Summary

  • Add MailCssExtension exposing a mail_css() Twig function that reads the compiled styles/mail.scss asset through AssetMapper
  • Use mail_css() in Mail/base.html.twig instead of referencing the compiled CSS file directly via content('/../var/sass/mail.output.css')
  • Requires symfony/asset-mapper: ^8.0

🤖 Generated with Claude Code

Summary by Sourcery

Inline mail template CSS via AssetMapper-backed Twig function instead of a hardcoded compiled CSS file path.

New Features:

  • Introduce a MailCss Twig extension exposing a mail_css() function to retrieve the compiled mail stylesheet via AssetMapper.

Enhancements:

  • Register the MailCss Twig extension in the service container for use in mail templates.
  • Update mail base template to use the mail_css() function for inlining styles rather than reading from a static filesystem path.

Build:

  • Add symfony/asset-mapper as a runtime dependency to support asset-based CSS retrieval for mail templates.

Add MailCssExtension exposing a mail_css() Twig function that reads the
compiled mail.scss asset through AssetMapper, and use it in
Mail/base.html.twig instead of referencing the compiled CSS file
directly. Requires symfony/asset-mapper.
@sourcery-ai

sourcery-ai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

This PR replaces the mail template’s direct reference to a compiled CSS file with a Twig function that fetches the compiled mail styles via Symfony’s AssetMapper, wiring a new Twig extension into the service container and adding the AssetMapper dependency.

Sequence diagram for mail_css Twig function inlining CSS

sequenceDiagram
    participant TwigTemplate as Mail_base_html_twig
    participant MailCssExtension
    participant AssetMapperInterface
    participant inline_css as inline_css_filter

    TwigTemplate->>MailCssExtension: mail_css()
    MailCssExtension->>AssetMapperInterface: getAsset(styles/mail.scss)
    AssetMapperInterface-->>MailCssExtension: Asset(content)
    MailCssExtension-->>TwigTemplate: getMailCss() content
    TwigTemplate->>inline_css: inline_css(mail_css() content)
Loading

File-Level Changes

Change Details Files
Introduce a Twig extension that exposes a mail_css() function backed by AssetMapper for retrieving the compiled mail stylesheet content.
  • Create MailCssExtension as a readonly class with an injected AssetMapperInterface dependency.
  • Define a Twig function mail_css marked safe for HTML that returns the content of the styles/mail.scss asset via AssetMapper.
src/Twig/MailCssExtension.php
Register the new Twig extension as a service so it is available in Twig templates.
  • Add MailCssExtension use statement in the services configuration.
  • Register a framework.mail_css_extension service and tag it as a twig.attribute_extension.
config/services.php
Switch the mail base template to inline CSS using the new mail_css() Twig function instead of a hardcoded path to the compiled CSS file.
  • Update the inline_css filter usage in the mail base template to call mail_css() rather than content() on a fixed filesystem path.
templates/Mail/base.html.twig
Add Symfony AssetMapper as a required dependency to support asset resolution in the new Twig extension.
  • Include symfony/asset-mapper:^8.0 in the main Composer require section.
composer.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider handling the case where AssetMapperInterface::getAsset('styles/mail.scss') returns null to avoid potential runtime errors when the asset is missing or misconfigured.
  • If mail_css() is called multiple times in a single request, you may want to cache the asset content in the extension to avoid repeated reads from the asset mapper.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider handling the case where `AssetMapperInterface::getAsset('styles/mail.scss')` returns `null` to avoid potential runtime errors when the asset is missing or misconfigured.
- If `mail_css()` is called multiple times in a single request, you may want to cache the asset content in the extension to avoid repeated reads from the asset mapper.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@tijsverkoyen

Copy link
Copy Markdown
Member

Wouldn't it be better to have a more generic Twig function? Which allows to pass a path. Eg: asset_content('styles/mail.scss').

Rename MailCssExtension to AssetContentExtension and mail_css() to
asset_content(string $path), allowing any AssetMapper asset path to be
read rather than only the mail stylesheet.
@jonasdekeukelaere

Copy link
Copy Markdown
Member Author

@tijsverkoyen Good call — done in d5ac878: renamed MailCssExtension to AssetContentExtension and the Twig function to asset_content(string $path), so the template now calls asset_content('styles/mail.scss').

Comment thread src/Twig/AssetContentExtension.php Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants