reverted: --- b/README.txt +++ a/README.txt @@ -60,9 +60,6 @@ that you can theme e-mails the same way you theme other content. The theme hook you need to use when interacting with the theming system is 'swiftmailer'. -Add custom css to the template with the preprocess hook and convert them to -inline-styles with the corresponding option under the message settings. - 2.1 Theme File The easiest way to theme e-mails is to create a file named diff -u b/src/Form/MessagesForm.php b/src/Form/MessagesForm.php --- b/src/Form/MessagesForm.php +++ b/src/Form/MessagesForm.php @@ -64,14 +64,6 @@ as plain text as this is the content type Drupal by default will apply to all e-mails.'), ); - $form['format']['css_inliner'] = array( - '#type' => 'checkbox', - '#title' => t('Convert CSS to inline styles'), - '#default_value' => $config->get('css_inliner'), - '#description' => t('The css of the rendered template (if set in template) will be parsed to inline styles - to assure solid rendering in all email clients.'), - ); - $form['convert'] = array( '#type' => 'fieldset', '#title' => t('Plain Text Version'), diff -u b/src/Plugin/Mail/SwiftMailer.php b/src/Plugin/Mail/SwiftMailer.php --- b/src/Plugin/Mail/SwiftMailer.php +++ b/src/Plugin/Mail/SwiftMailer.php @@ -6,13 +6,16 @@ use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\UrlHelper; +use Drupal\Core\Asset\LibraryDiscoveryParser; use Drupal\Core\Config\ImmutableConfig; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Mail\MailInterface; +use Drupal\Core\Mail\MailManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Render\Markup; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Site\Settings; +use Drupal\mailsystem\MailsystemManager; use Drupal\swiftmailer\Utility\Conversion; use Exception; use Html2Text\Html2Text; @@ -62,6 +65,16 @@ protected $moduleHandler; /** + * @var \Drupal\Core\Mail\MailManagerInterface + */ + protected $mailManager; + + /** + * @var \Drupal\Core\Asset\LibraryDiscoveryParser + */ + protected $libraryDiscoverParser; + + /** * SwiftMailer constructor. * * @param \Drupal\Core\Config\ImmutableConfig $transport @@ -69,13 +82,17 @@ * @param \Psr\Log\LoggerInterface $logger * @param \Drupal\Core\Render\RendererInterface $renderer * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager + * @param \Drupal\Core\Asset\LibraryDiscoveryParser $library_discover_parser */ - public function __construct(ImmutableConfig $transport, ImmutableConfig $message, LoggerInterface $logger, RendererInterface $renderer, ModuleHandlerInterface $module_handler) { + function __construct(ImmutableConfig $transport, ImmutableConfig $message, LoggerInterface $logger, RendererInterface $renderer, ModuleHandlerInterface $module_handler, MailManagerInterface $mail_manager, LibraryDiscoveryParser $library_discover_parser) { $this->config['transport'] = $transport->get(); $this->config['message'] = $message->get(); $this->logger = $logger; $this->renderer = $renderer; $this->moduleHandler = $module_handler; + $this->mailManager = $mail_manager; + $this->libraryDiscoverParser = $library_discover_parser; } /** @@ -87,7 +104,9 @@ $container->get('config.factory')->get('swiftmailer.message'), $container->get('logger.factory')->get('swiftmailer'), $container->get('renderer'), - $container->get('module_handler') + $container->get('module_handler'), + $container->get('plugin.manager.mail'), + $container->get('library.discovery.parser') ); } @@ -123,15 +142,28 @@ } if ($this->config['message']['css_inliner']) { - $theme_path =\Drupal::service('theme.manager')->getActiveTheme()->getPath(); - $mail_css_path = realpath($theme_path . '/css/mail.css'); - // Doesn't exist a mail.css in the default theme folder under the css folder. - if($mail_css_path) { - $message['body'] = (new CssToInlineStyles())->convert($message['body'], file_get_contents($mail_css_path)); - } else { - // TODO Decide if it should show a non-existing file error or if the - // swiftmailer module should have a default mail.css file and use that one in this case. + // Attempt to use the email theme defined in MailSystem. + if ($this->mailManager instanceof MailsystemManager) { + $theme_name = $this->mailManager->getMailTheme(); + } + // Default to the active theme if MailsystemManager isn't used. + else { + $theme_name = $this->themeManager->getActiveTheme()->getName(); + } + + // Parse libraries defined by the theme. + $libraries = $this->libraryDiscoverParser->buildByExtension($theme_name); + + // If the theme contains a mail library, inline each + // css file defined in the library. + if (!empty($libraries['mail']['css'])) { + foreach ($libraries['mail']['css'] as $library_css) { + $mail_css_path = realpath($library_css['data']); + if (file_exists($mail_css_path)) { + $message['body'] = (new CssToInlineStyles())->convert($message['body'], file_get_contents($mail_css_path)); + } + } } } } only in patch2: unchanged: --- a/config/install/swiftmailer.message.yml +++ b/config/install/swiftmailer.message.yml @@ -1,4 +1,5 @@ format: text/plain respect_format: true +css_inliner: false convert_mode: false character_set: UTF-8 only in patch2: unchanged: --- a/config/schema/swiftmailer.schema.yml +++ b/config/schema/swiftmailer.schema.yml @@ -8,6 +8,9 @@ swiftmailer.message: respect_format: type: boolean label: 'Respect format' + css_inliner: + type: boolean + label: 'Inline CSS' convert_mode: type: boolean label: 'Respect format'