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?

Comments

lambic created an issue. See original summary.

lambic’s picture

Issue summary: View changes
adamps’s picture

Status: Active » Postponed (maintainer needs more info)

Please provide much more detail on the steps to reproduce. How did you send the email? What is the relevant configuration - email policy, text formats, etc. The aim is so that another person can follow your instructions from a newly created site and definitely hit the bug.

ollaankoodeis’s picture

I faced the same issue today. Configured Symfony mailer to Drupal 9 site and was trying to send Test email with inline styles in body content and also commerce order receipt with template with some inline styles. For some reason inline styles are not rendering on email. I debugged that the inline styles exist on template_preprocess_email functions $email->getBody() content (symfony_mailer.module).

Might be also configuration issue but did not succeed to find a working solution in couple of hours investigation so changed to Swift mailer and with it the same commerce order receipt template worked right away - inline styles rendered correctly.

joelseguin’s picture

I've noticed the same issue with Drupal 9+ and Commerce. My workaround was to revert to the previous version of Symfony Mailer (1.0.0-beta1) although there seems to be an issue with sending a copy of the receipt to an email using the bc field (which I'm investigating at the moment).

mstevetodd’s picture

Reverting to 1.0.0-beta1 also worked for me. Thanks!

gilbertdelyon’s picture

Same issue here.
I spent a couple hours looking into my code and then found this thread.
Downgrade to 1.0.0beta1 works.

php 8.1.12
Drupal 9.4.8

Notice: After downgrade to 1.0.0beta1 (via composer) I get an error message at each mail sending process "Cannot send mail - Please contact site admin".
Mails are sent and received properly, so I do not understand what happens. May be some mistake in my code, but I had no error with SM1.1 beta2 (and no html tags!)
uninstall, re-install 1.0.0beta1 -> itworks!

adamps’s picture

This difficulty with solving this issue is that there are many possible causes: the styles could be stripped by a text format, or by a particular policy configuration, or for specific types of email only. There still isn't a clear description of steps to reproduce - in fact I guess that different people are seeing different problems.

#5-7 are a regression caused by the recent 1.1 beta. I would definitely like to know more about that. The original bug was on alpha11 so it's clearly different.

For us to get a fix to this issue first we will need to understand it. If you'd like to help then please try to explain how to hit the bug. Starting with a test site that shows the bug, please simplify as much as possible. Remove email policies, change your template to the minimum possible, change the text formats to the default. Then try to write a set of instructions that will also show the bug, starting from an empty site.

gilbertdelyon’s picture

@AdamPS,
Thanks for answer,
I'll do my best to perform some test in the next days and let you know the result.

in the meantime time please notice:
Without changing anything else in my code:

  • It works with 1.0.0-beta1.Mail content is displayed as expected.
  • It doesn't work with 1.1.0-beta2. Mail content is displayed without style, but WITH ADDITION of " */ plus some empty lines" at the begining.

See attached screen crops.

If it can help I can send you the source code of the email in both cases.

gilbertdelyon’s picture

@AdamPS,
Steps to reproduce
PHP 8.1.12
Drupal 9.4.8
SM 1.1.0-beta2

In my custommodule.module file
I have a simple hook_mail() like this:

/**
 * Implements hook_mail()
 * Email sending for the node2notifs module 
 */

function node2notifs_mail($key, &$message, $params) {
  switch ($key) {
    case 'mail_notif':
      $message['from'] = \Drupal::config('system.site')->get('mail');
      $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed; delsp=yes';
      $message['subject'] = $params['subject'];
      $message['body'][] =  node2notifs_mailbodybuilder( $params); //See function here-under

      if(isset($params['Bcc'])){
          $message['headers']['Bcc'] = $params['Bcc']; 
      }//fin if

      $message['options'] = [];
      if (isset($params['options']) && !empty($params['options'])) {
        foreach ($params['options'] as $key => $value) {
          $message['options'][$key] = $value;
        }
      }
      break;
      
  }//fin switch
}//fin function


/**
 * Very simplified Function that builds html email body
 * No specifis Policy, no specific template
 * $params is no used for the test
 */
 
function node2notifs_mailbodybuilder( $params) {
    return t('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                <html>
                 <style type="text/css">
                     hr {
                        border: none;
                        height: 1px;
                        background-color: #DDD;
                     }
                 </style>
                 
                 <body style="font-size:14px">
                   INTRODUCTION TEXT                
                   <div style="border: 1px solid #CCC; padding:1%; max-width:700px;background:#F7F7F7; margin-bottom:10px; ">
                         MAIN TEXT
                    </div>
                    FOOTER TEXT
                 </body>
                   
                </html>');
} //fin function 

and the content of recieved email is as follows, without any kind of style:

*/ INTRODUCTION TEXT
MAIN TEXT
FOOTER TEXT

I have no idea about where the */ could come from.

see attached sourcecode of the email

gilbertdelyon’s picture

Another simple test:
I have 2 sites:
Site 1:
PHP 8.1.12
Drupal 9.4.8
SM 1.1.0-beta2
SendMAil Transport
Site 2
Duplicate of site 1 but SM 1.0.0-Beta1

In Configuration Test page Body I paste this very simple content

<p>INTRO</p>

<div style="border: 1px solid #CCC; padding:1%; max-width:700px;background:#F7F7F7; margin-bottom:10px; ">TEXT</div>

<p>FOOTER</p>

If I select Email HTML or Full HTML as text format I receive a test email with styled content only in Site 2 (1.0 beta1) but not styled in site 1 (1.1-Beta2). Policies are set in the same way.

Source Code of Email in site 1 (1.1-Beta2) (NOT at All the same as what I paste in test email body)

--49wOUzhO
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Full HTML

Mailer 1.1 Beta2

INTRO

TEXT
FOOTER

--49wOUzhO
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable


<html>
<body>
<div class=3D"email-type-symfony-mailer email-sub-type-test">
  <table width=3D"100%" cellpadding=3D"0" cellspacing=3D"0">
    <tr>
      <td>
        <div style=3D"padding: 0px 0px 0px 0px;" class=3D"clearfix">
         =20
<p>Full HTML</p>

<p>Mailer 1.1 Beta2</p>

<p>INTRO</p>

<div>TEXT</div>

<p>FOOTER</p>


        </div>
      </td>
    </tr>
  </table>
</div>
</body>
</html>

Source Code of email in Site 2 (1.0 beta1) (is the same as the one I paste in test email body):

--gxpsgfW8
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Text format full HTML
Mailer 1.0 Beta 1
INTRO

TEXT
FOOTER

--gxpsgfW8
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable


<p> Text format full HTML</p>
Mailer 1.0 Beta 1
<p>INTRO</p>

<div style=3D"border: 1px solid #CCC; padding:1%; max-width:700px;backgroun=
d:#F7F7F7; margin-bottom:10px; ">TEXT</div>

<p>FOOTER</p>

--gxpsgfW8--

I hope it can help

gilbertdelyon’s picture

IT WORKS !

I just improved my custom module in this way:
1- canceled the old fashionned hook_mail()
2- added a direct call to SymfonyMailer in my NotificationSender service. (Took time to find a tutorial - I used this one as a starting point )

And it works! (SM 1.1-beta2)

html tags are displayed correctly
no need of SymfonyMailer BC module
No more hook_mail()
Cleaner, simpler... So far so good.

BUT:
In Mailer configuration if I send a test mail with styled content, in Full HTML or Email HTML, the style is still removed.

seutje’s picture

Can confirm the issue only pops up with beta2 and downgrading to beta1 fixed it for me.

damien laguerre’s picture

StatusFileSize
new604 bytes

Can confirm the bug too.
In my case, it's with simplenews.

- Drupal 9.4.8
- simplenews 3.0.0-beta4

The attached patch fixes the bug in my case.

fenstrat’s picture

Version: 1.0.0-alpha11 » 1.x-dev
Status: Postponed (maintainer needs more info) » Needs work
Issue tags: +Needs tests

Confirming that #14 fixes the issue.

Would be nice to add a test for this.

I hit this with commerce, all order receipts loose their inline styles.

adamps’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs tests

Great thanks everyone. The problem seems to come from the call to Xss::filter() in Renderer::ensureMarkupIsSafe(). This removes all inline styles.

I had avoided the Markup class because it is marked as internal. However it does seem to be quite common to use it anyway so we could just ignore that.

It would be nice to have a test, and I would for sure accept a follow on patch containing one. For the moment, during beta, my policy is to allow bug fixes without a test, but require tests for new features.

  • AdamPS committed 65be722 on 1.x
    Issue #3313675 by Damien Laguerre, gilbertdelyon: Style tags in email...
adamps’s picture

Status: Reviewed & tested by the community » Fixed
oxcelot’s picture

Hi, I'm trying to apply this patch using composer but it shows errors and can't apply it.

it shows:

Gathering patches for dependencies. This might take a minute.
  - Installing drupal/symfony_mailer (1.1.0-beta2): Extracting archive
  - Applying patches for drupal/symfony_mailer
    https://www.drupal.org/files/issues/2022-11-14/3313675-14.patch (3313675 - Style tags in email body get stripped)
   Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2022-11-14/3313675-14.patch

and in composer.json this is the configuration:

"patches": {
            "drupal/symfony_mailer": {
                "3313675 - Style tags in email body get stripped": "https://www.drupal.org/files/issues/2022-11-14/3313675-14.patch"
            }
        }

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

lambic’s picture

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 narrowed it down to the render() function in Email.php:

$body = ['#theme' => 'email', '#email' => $this];
$html = $this->renderer->renderPlain($body);

before the renderer call the tags are there, after they are gone.

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();

kuntal_d’s picture

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);