Skip to content

Core\Mail\Preview::keptCss

$preview->keptCss(): array<string>

keptCss() returns the list of CSS rules that stay in the <style> element of the mail. The other rules are copied into the elements. You call keptCss() on the result of Core\Mail::preview.

A rule stays when a style attribute cannot do the same thing:

  • an at-rule, such as @media for a small screen or @font-face,
  • a rule with a pseudo-class or a pseudo-element, such as a:hover or p::first-line,
  • a rule that matches no element of the HTML. unusedCss() lists these rules too.

The result has one string for each rule, in the order of the HTML. An empty list means that every rule was copied into the elements.

Some mail programs read a <style> element and some do not. A rule in this list works only in a mail program that reads it. Use keptCss() to see which part of your design depends on it. A test can also check that the list is empty.

The examples below show a @media rule that stays, an a:hover rule that stays, and a test that checks that every rule is copied.

Return value

Returns array<string>. One string for each rule, in the order of the HTML. The list is empty when every rule was copied into the elements.

Changelog

VersionChange
0.0.6Core\Mail::preview builds a mail body as send would and sends nothing. It returns the HTML with its CSS inlined, the plain text, and the CSS rules that stayed in <style>.

Examples

A media rule stays

<?nvs
// Shows that a `@media` rule stays in the `<style>` element of a mail.
// The `@media` rule makes the text larger on a small screen. A `style` attribute cannot
// depend on the screen size, so this rule cannot be copied into the elements.
Core\Mail\Preview $mail = Core\Mail::preview(html`<style>
p { font-size: 14px }
@media (max-width: 600px) { p { font-size: 18px } }
</style>
<p>Your order has shipped.</p>`);
foreach ($mail->keptCss() as string $rule) {
echo $rule, "\n";
}
Output
@media (max-width: 600px) { p { font-size: 18px } }

A hover rule stays

<?nvs
// Shows that a rule for a link under the mouse pointer stays in the `<style>` element.
// `a:hover` applies only while the pointer is on the link, so it cannot be a `style` attribute.
// The rule for `a` is copied into the link.
Core\Mail\Preview $mail = Core\Mail::preview(html`<style>
a { color: #0a5 }
a:hover { text-decoration: underline }
</style>
<p><a href="https://example.com/help">Get help</a></p>`);
foreach ($mail->keptCss() as string $rule) {
echo 'kept: ', $rule, "\n";
}
echo $mail->html(), "\n";
Output
kept: a:hover { text-decoration: underline; }
<html><head><style>a:hover { text-decoration: underline; }</style>
</head><body><p><a href="https://example.com/help" style="color: #0a5;">Get help</a></p></body></html>

Test that every rule is copied

<?nvs
// Shows how to test that every CSS rule of a mail is copied into its elements.
// proof: test
use Core\Test;
class NewsletterTest
{
#[Test]
public function everyRuleIsCopied(): void
{
Core\Mail\Preview $mail = Core\Mail::preview(html`<style>
h1 { font-size: 22px }
p { line-height: 1.5 }
</style>
<h1>News for May</h1>
<p>Three new teas are in the shop.</p>`);
// Each rule matched an element, so no rule is left in the `<style>` element.
Test::assertCount($mail->keptCss(), 0);
}
}
Output
NewsletterTest
✓ everyRuleIsCopied
0 failed, 1 passed, 0 skipped, 0 flaky