Style tags in email body get stripped

Created on 5 October 2022, over 1 year ago
Updated 11 October 2023, 9 months ago

Problem/Motivation

I'm trying to send an email with anchor tags in it and the anchor tags are styled to look like buttons with a style attribute. It looks like symfony_mailer strips the style tag out before it sends the email.

Steps to reproduce

Try to send an email containing something like '<a href="http://drupal.org" style="display:inline-block">Drupal</a>'. The resulting email will not have the style attribute.

Proposed resolution

Some way to override that stripping?

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇨🇦Canada lambic

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.

  • 🇨🇦Canada lambic

    I've checked that I'm on the latest version and the attached patch is there, but I'm still having issues with lost style tags.

    Here's my EmailBuilder code:

    $email->setSubject($payload->getSubject())
          ->setTo($to)
          ->setFrom($from)
          ->setReplyTo($reply)
          ->setBody(['#markup' => $htmlMessage])
          ->setTextBody($textMessage);
    

    I've confirmed that I see the style tags before $email->render() gets called in Mailer.php, but I'm not sure how to track it after that as getBody() fails after that point.

    Note also that you can't use devel to debug this because devel also strips out style tags, so I had to do this to debug:

    \Drupal::database()->insert('x')->fields(['y' => $email->getBody()['#markup']])->execute();

  • 🇮🇳India kuntal_d

    Hi @lambic,

    I have got the same issue.
    Please try to send the body like below:

    $body = [
     "#type" => "processed_text",
     "#text" => $htmlMessage,
     "#format" => "full_html"
    ];
    
    $email->setSubject($payload->getSubject())
          ->setTo($to)
          ->setFrom($from)
          ->setReplyTo($reply)
          ->setBody($body)
          ->setTextBody($textMessage);
Production build 0.69.0 2024