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?
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | 3313675-14.patch | 604 bytes | damien laguerre |
Comments
Comment #2
lambic commentedComment #3
adamps commentedPlease 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.
Comment #4
ollaankoodeis commentedI 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.
Comment #5
joelseguinI'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).
Comment #6
mstevetodd commentedReverting to 1.0.0-beta1 also worked for me. Thanks!
Comment #7
gilbertdelyon commentedSame 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".uninstall, re-install 1.0.0beta1 -> itworks!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!)
Comment #8
adamps commentedThis 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.
Comment #9
gilbertdelyon commented@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:
See attached screen crops.
If it can help I can send you the source code of the email in both cases.
Comment #10
gilbertdelyon commented@AdamPS,
Steps to reproduce
PHP 8.1.12
Drupal 9.4.8
SM 1.1.0-beta2
In my
custommodule.modulefileI have a simple hook_mail() like this:
and the content of recieved email is as follows, without any kind of style:
I have no idea about where the
*/could come from.see attached sourcecode of the email
Comment #11
gilbertdelyon commentedAnother 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
If I select
Email HTMLorFull HTMLas 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)
Source Code of email in Site 2 (1.0 beta1) (is the same as the one I paste in test email body):
I hope it can help
Comment #12
gilbertdelyon commentedIT 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.
Comment #13
seutje commentedCan confirm the issue only pops up with beta2 and downgrading to beta1 fixed it for me.
Comment #14
damien laguerre commentedCan 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.
Comment #15
fenstratConfirming 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.
Comment #16
adamps commentedGreat thanks everyone. The problem seems to come from the call to
Xss::filter()inRenderer::ensureMarkupIsSafe(). This removes all inline styles.I had avoided the
Markupclass 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.
Comment #18
adamps commentedComment #19
oxcelot commentedHi, I'm trying to apply this patch using composer but it shows errors and can't apply it.
it shows:
and in composer.json this is the configuration:
Comment #21
lambic commentedI'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:
I've narrowed it down to the render() function in Email.php:
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();Comment #22
kuntal_d commentedHi @lambic,
I have got the same issue.
Please try to send the body like below: