Download the PHP package bradietilley/laravel-css-inliner without Composer
On this page you can find all versions of the php package bradietilley/laravel-css-inliner. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bradietilley/laravel-css-inliner
More information about bradietilley/laravel-css-inliner
Files in bradietilley/laravel-css-inliner
Package laravel-css-inliner
Short Description Converts CSS classes to inline styles within Laravel email
License MIT
Informations about the package laravel-css-inliner
CSS Style Inliner for Laravel
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
Via Composer
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
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.
Manual conversion
You may wish to manually convert some CSS in an HTML string or Email.
Option: Automatically parse Laravel email (or don't 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).
Option: Read CSS (style and link elements) from within any of the given HTML or emails
You may wish to parse <style>
or <link>
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
.
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 <style>
or <link>
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
.
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:
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 HTML is Converted (beforeConvertingHtml
)
Event: After HTML is Converted (afterConvertingHtml
)
Event: After Email is Converted (afterConvertingEmail
)
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
Contributing
Please see CONTRIBUTING for details.
All versions of laravel-css-inliner with dependencies
illuminate/contracts Version ^10.0
illuminate/support Version ^10.0
symfony/mime Version ^6.1
tijsverkoyen/css-to-inline-styles Version ^2.2