Add support for mail.css

Created on 22 March 2017, over 7 years ago
Updated 11 September 2024, 3 months ago

The D7 version supported adding a file named "mail.css" to the theme, if found it was added during delivery. It would be useful to support this with the D8 version too.

πŸ“Œ Task
Status

Active

Version

2.0

Component

Theming

Created by

πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡­πŸ‡·Croatia portulaca

    @liquidcms it's not working for me, I get a WSOD when I try to send an example mimemail and this in the log:

    Error: Class "LanguageInterface" not found in mytheme_preprocess_mimemail_message() (line 41 of /var/www/html/name/web/themes/custom/mytheme/mytheme.theme)
    …

  • πŸ‡¬πŸ‡§United Kingdom rivimey

    @portulaca It sounds like you need a 'use' statement for LanguageInterface in the file that you have added the code from #10 to. I can't recall the exact place but something of the form:

    use \Drupal\Core\...\...\LanguageInterface;

    Search existing code for examples.

  • πŸ‡­πŸ‡·Croatia portulaca

    @rivimey thank you!

    It's

    use Drupal\Core\Language\LanguageInterface;
    use Drupal\Core\Url;
    

    It's working now, although I'm getting a double slash in the CSS link path https://example.com//themes/custom/…

    Should the variable be without it?

    $variables['mail_css'] = $base_path . 'themes/custom/my_theme/css/mail.css';
    
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia
  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    @malcomio: Are you planning on working on this issue?

  • πŸ‡¬πŸ‡§United Kingdom malcomio

    In the project where we're using mimemail, we ended up adding a preprocess in our theme, as suggested in #10, and I haven't had a chance to make progress on a contributed version.

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia
  • πŸ‡ΊπŸ‡ΈUnited States aytee

    We used the preprocess theme suggestion as in #10. This successfully includes the mail.css file.

  • πŸ‡΅πŸ‡±Poland pawel.traczynski Warsaw

    Hi. The method from #10 is adding a link to the mail.css in the mimemail-message template. From what I saw it did not work in all cases. To actually add the CSS into the template, I have used the below code:

    /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
      $theme_handler = \Drupal::service('theme_handler');
      $theme_path = $theme_handler->getTheme('YOUR_THEME')->getPath();
    
      $css = FALSE;
      // Assuming mail.css is in a /css subdirectory where all other css is.
      $css_path = DRUPAL_ROOT . '/' . $theme_path . '/css/mail.css';
    
      try {
        $css = file_get_contents($css_path);
      }
      catch (Exception $exception) {
        Drupal::logger('YOUR_THEME')->error(t('Failed to get contentns of mail.css file while using mimemail-message template. Exception message: @exception', [
          '@exception' => $exception->getMessage(),
        ]));
      }
    
      if ($css) {
        $variables['css'] = $css;
      }
    

    With this code there is not need to modify the mimemail-message twig template unless you want to add some additional markup in it.

    I hope this helps someone.

Production build 0.71.5 2024