bummzack/swiftmailer-emogrifyplugin

Inline CSS in the HTML output of SwiftMailer using Emogrifier.

1.0.1 2022-03-24 17:44 UTC

This package is auto-updated.

Last update: 2024-04-24 23:57:15 UTC


README

Scrutinizer Code Quality Code Coverage Build Status Latest Stable Version

Inline CSS in the HTML output of SwiftMailer using Emogrifier.

Installation and requirements

Install via composer, using:

composer require bummzack/swiftmailer-emogrifyplugin

Requirements:

  • PHP 7.2+
  • SwiftMailer 6.x
  • Emogrifier 6.x

Usage

By default, the plugin will inline CSS that is part of the HTML, eg. styles defined in <style> tags.

Supplying custom CSS

$plugin = new EmogrifierPlugin();
$plugin->setCss('.customStyle: { color: red; };');

Example

Here's how you could use the plugin to send emails with custom styles loaded from a file:

$plugin = new Bummzack\SwiftMailer\EmogrifyPlugin\EmogrifierPlugin();
$emogrifier->setCss(file_get_contents( /* path to your CSS file */ ));

// Create the Mailer using any Transport
$mailer = new Swift_Mailer(
    new Swift_SmtpTransport('smtp.example.org', 25)
);

// Use Emogrifier plugin to inline styles.
$mailer->registerPlugin($plugin);

$message = new Swift_Message();
$message
    ->setSubject('Your subject')
    ->setFrom(['test@example.com' => 'Test'])
    ->setTo(['receiver@example.com'])
    ->setBody('<p>My custom HTML</p>', 'text/html');

// Send your email
$mailer->send($message);