PHPackages                             bradietilley/laravel-css-inliner - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. bradietilley/laravel-css-inliner

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

bradietilley/laravel-css-inliner
================================

Converts CSS classes to inline styles within Laravel email

2.0.0(2y ago)31.6k2MITPHPPHP ^8.1

Since Nov 27Pushed 2y ago1 watchersCompare

[ Source](https://github.com/bradietilley/laravel-css-inliner)[ Packagist](https://packagist.org/packages/bradietilley/laravel-css-inliner)[ RSS](/packages/bradietilley-laravel-css-inliner/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (9)Versions (6)Used By (0)

CSS Style Inliner for Laravel
=============================

[](#css-style-inliner-for-laravel)

[![Static Analysis](https://github.com/bradietilley/laravel-css-inliner/actions/workflows/static.yml/badge.svg)](https://github.com/bradietilley/laravel-css-inliner/actions/workflows/static.yml/badge.svg)[![Tests](https://github.com/bradietilley/laravel-css-inliner/actions/workflows/tests.yml/badge.svg)](https://github.com/bradietilley/laravel-css-inliner/actions/workflows/tests.yml/badge.svg)

Overview
--------

[](#overview)

This package leverages `tijsverkoyen/css-to-inline-styles` to convert CSS classes in mailable views to inline styles, for improved email client compatibility.

You can either leverage the automation of this package, or opt to hook into this package's event (or even Laravel's core events) and manually choose what gets converted and with what stylesheets. See Usage section for more details.

Install
-------

[](#install)

Via Composer

```
composer require bradietilley/laravel-css-inliner
```

Usage
-----

[](#usage)

For the purpose of this demonstration we'll use the facade `BradieTilley\LaravelCssInliner\Facades\CssInline`, however if you prefer directly using the instance (like myself) you can swap out `BradieTilley\LaravelCssInliner\Facades\CssInline::` for any of the below:

- `BradieTilley\LaravelCssInliner\Facades\CssInline::`
- `BradieTilley\LaravelCssInliner\CssInliner::singleton()->`
- `app(BradieTilley\LaravelCssInliner\CssInliner::class)->`
- `app()->make(BradieTilley\LaravelCssInliner\CssInliner::class)->`

#### Adding CSS via PHP

[](#adding-css-via-php)

You can at any point (before HTML conversion) define your CSS files or raw CSS that you wish to add to every HTML string or email that is converted by the CSS Inliner. A good example of this is a base stylesheet that all emails should inherit.

```
use BradieTilley\LaravelCssInliner\Facades\CssInline;

CssInline::addCssPath(resource_path('css/email.css'));
CssInline::addCssRaw('body { background: #eee; }');

# Or, you can achieve the same outcome by letting CssInline decide whether it's a path or raw CSS:
CssInline::addCss(resource_path('css/email.css'));
CssInline::addCss('body { background: #eee; }');
```

#### Manual conversion

[](#manual-conversion)

You may wish to manually convert some CSS in an HTML string or Email.

```
use BradieTilley\LaravelCssInliner\Facades\CssInline;
use Symfony\Component\Mime\Email;

CssInline::addCss('.font-bold { font-weight: bold; }');

# Convert an HTML string
$html = CssInline::convert('Bold');
echo $html; // Bold

# Convert an email
$email = new Email();
$email->html('Bold');
CssInline::convertEmail($email);
echo $email->getHtmlBody(); // Bold
```

#### Option: Automatically parse Laravel email (or don't automatically parse Laravel email)

[](#option-automatically-parse-laravel-email-or-dont-automatically-parse-laravel-email)

You may wish to conditionally enable or disable the CSS Inliner for mail sent from Laravel (via `Mail::send()`). To do this, we can leverage the `emailListener` option. Default is `true` (and as such will automatically convert CSS classes found in your emails sent from Laravel).

```
use BradieTilley\LaravelCssInliner\Facades\CssInline;

CssInline::emailListenerEnabled(); // Current state: true or false
CssInline::enableEmailListener(); // Enables option; returns instance of CssInliner
CssInline::disableEmailListener(); // Disables option; returns instance of CssInliner
```

#### Option: Read CSS (style and link elements) from within any of the given HTML or emails

[](#option-read-css-style-and-link-elements-from-within-any-of-the-given-html-or-emails)

You may wish to parse `` or `` stylesheets that are found within the HTML or email, for example if you want to store email-specific CSS within the email view itself. To do this, we can leverage the `cssFromHtmlContent` option. Default is `false`.

```
use BradieTilley\LaravelCssInliner\Facades\CssInline;

CssInline::cssFromHtmlContentEnabled(); // Current state: true or false
CssInline::enableCssExtractionFromHtmlContent(); // Enables option; returns instance of CssInliner
CssInline::disableCssExtractionFromHtmlContent(); // Disables option; returns instance of CssInliner
```

#### Option: After reading CSS (from above), remove the original CSS (style and link elements) from the HTML or email

[](#option-after-reading-css-from-above-remove-the-original-css-style-and-link-elements-from-the-html-or-email)

You may wish to strip out the large `` or `` stylesheets after this package converts the CSS to inline styles, to reduce the payload size of emails sent out from your system. To do this, we can leverage the `cssRemovalFromHtmlContent` option. Default is `false`.

```
use BradieTilley\LaravelCssInliner\Facades\CssInline;

CssInline::cssRemovalFromHtmlContentEnabled(); // Current state: true or false
CssInline::enableCssRemovalFromHtmlContent(); // Enables option; returns instance of CssInliner
CssInline::disableCssRemovalFromHtmlContent(); // Disables option; returns instance of CssInliner
```

```
use BradieTilley\LaravelCssInliner\Facades\CssInline;

CssInline::doSomething();
CssInliner::addCssPath(resource_path('css/email.css'));
CssInliner::addCssRaw('.text-success { color: #00ff00; }');

# Convert your own HTML/CSS
$html = 'Success text';
$html = CssInliner::convert($html);

echo $html; // Success text
```

### Events:

[](#events)

There are four events fired by CssInliner - two for HTML conversion, and all four for Email conversion. The order of which the events are called is:

- 1st: `BradieTilley\LaravelCssInliner\Events\PreEmailCssInlineEvent` (Email only)
- 2nd: `BradieTilley\LaravelCssInliner\Events\PreCssInlineEvent` (Email + HTML)
- 3rd: `BradieTilley\LaravelCssInliner\Events\PostCssInlineEvent` (Email + HTML)
- 4th: `BradieTilley\LaravelCssInliner\Events\PostEmailCssInlineEvent` (Email only)

Listening to events can be done through Laravel's normal means. For example:

```
Event::listen(\BradieTilley\LaravelCssInliner\Events\PreEmailCssInlineEvent::class, fn () => doSomething());
```

Or, you may wish to hook into CSS Inliner using the callback methods: `beforeConvertingEmail`, `afterConvertingEmail`, `beforeConvertingHtml`, `afterConvertingHtml`. These methods accept a callback and are simply a proxy to `Event::listen()` so feel free to treat the callbacks used in the examples below as the second argument to `Event::listen()` of the corresponding CssInliner events.

#### Event: Before Email is Converted (`beforeConvertingEmail`)

[](#event-before-email-is-converted-beforeconvertingemail)

```
CssInliner::beforeConvertingEmail(function (PreEmailCssInlineEvent $event) {
    # You have access to the unconverted-Email and CSS Inliner instance via the event
    $event->email; // instanceof: \Symfony\Component\Mime\Email
    $event->cssInliner; // instanceof BradieTilley\LaravelCssInliner\CssInliner
    echo $event->email->getHtmlBody(); // ...

    # Because this is a 'before' event, you may choose to halt the conversion of this *one* Email
    return $event->cssInliner->halt();
    # Laravel will halt any other event listeners; CSS Inliner will return the Email immediately (and not convert it)
});
```

#### Event: Before HTML is Converted (`beforeConvertingHtml`)

[](#event-before-html-is-converted-beforeconvertinghtml)

```
CssInliner::beforeConvertingHtml(function (PreCssInlineEvent $event) {
    # You have access to the unconverted-HTML and CSS Inliner instance via the event
    $event->html; // string
    $event->cssInliner; // instanceof BradieTilley\LaravelCssInliner\CssInliner
    echo $event->html; // ...

    # Because this is a 'before' event, you may choose to halt the conversion of this *one* HTML string
    return $event->cssInliner->halt();
    # Laravel will halt any other event listeners; CSS Inliner will return the HTML immediately (and not convert it)
});
```

#### Event: After HTML is Converted (`afterConvertingHtml`)

[](#event-after-html-is-converted-afterconvertinghtml)

```
CssInliner::afterConvertingHtml(function (PostCssInlineEvent $event) {
    # You have access to the converted-HTML and CSS Inliner instance via the event
    $event->html; // string
    $event->cssInliner; // instanceof BradieTilley\LaravelCssInliner\CssInliner
    echo $event->html; // ...

    # Because this is an 'after' event, you cannot halt the conversion of the HTML string (unlike the 'before' event)
});
```

#### Event: After Email is Converted (`afterConvertingEmail`)

[](#event-after-email-is-converted-afterconvertingemail)

```
CssInliner::afterConvertingEmail(function (PostEmailCssInlineEvent $event) {
    # You have access to the converted-Email and CSS Inliner instance via the event
    $event->email; // instanceof: \Symfony\Component\Mime\Email
    $event->cssInliner; // instanceof BradieTilley\LaravelCssInliner\CssInliner
    echo $event->email->getHtmlBody(); // ...

    # Because this is an 'after' event, you cannot halt the conversion of the Email (unlike the 'before' event)
});
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 92.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~48 days

Total

5

Last Release

1070d ago

Major Versions

0.2.0 → 1.0.02022-11-28

1.0.0 → 2.0.02023-06-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/f93c7d77d882d4106664cca209c6dccc0e531f307f56dc2247937358d193ef0b?d=identicon)[bradietilley](/maintainers/bradietilley)

---

Top Contributors

[![bradietilley](https://avatars.githubusercontent.com/u/44430471?v=4)](https://github.com/bradietilley "bradietilley (51 commits)")[![owenvoke](https://avatars.githubusercontent.com/u/1899334?v=4)](https://github.com/owenvoke "owenvoke (4 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/bradietilley-laravel-css-inliner/health.svg)

```
[![Health](https://phpackages.com/badges/bradietilley-laravel-css-inliner/health.svg)](https://phpackages.com/packages/bradietilley-laravel-css-inliner)
```

###  Alternatives

[laravel-notification-channels/telegram

Telegram Notifications Channel for Laravel

1.1k3.4M35](/packages/laravel-notification-channels-telegram)[mckenziearts/laravel-notify

Flexible flash notifications for Laravel

1.7k1.1M5](/packages/mckenziearts-laravel-notify)[illuminate/mail

The Illuminate Mail package.

5910.1M391](/packages/illuminate-mail)[fedeisas/laravel-mail-css-inliner

Inline the CSS of your HTML emails using Laravel

5974.6M3](/packages/fedeisas-laravel-mail-css-inliner)[propaganistas/laravel-disposable-email

Disposable email validator

5762.6M6](/packages/propaganistas-laravel-disposable-email)[illuminate/validation

The Illuminate Validation package.

18936.7M1.4k](/packages/illuminate-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
