Problem description

The message settings are confusing and over-complex. We are starting a new branch so it's a great time to make it better.

1. The existing defaults are wrong. The main purpose of using this module as the mail formatter is for HTML mails - for plain text might as well use the Default PHP mailer. So HTML should be the default and also a plain text version is a good idea.

2. The GUI is confusing. Improve the descriptions. Improve the terminology, in particular the word "format" is unfortunate because "Text formats" are something else entirely, and something that we do want to configure in this module in future. Remove the setting "Respect format" which is hard to understand how it relates to "Format". Basically there are three options and so we should have a single field with three values: "always plain", "always HTML" and "keep existing".

3. Fix confusing configuration names to match the correct terminology:

  • convert_mode to
    generate_plain</code</li>
      <li><code>format

    to content_type.

  • filter_format to text_format.

Do the same for message parameters, but continue to accept the old values for back-compatibility.

4. Fix some inconsistent and confusing logic.

  • The setting character_set should not be affected configuration of content type (respect_format).
  • The parameter $message['params']['content_type'] should take precedence over any other input - the purpose of these parameters is to allow specific override of the normal settings.

5. Add a GUI for the filter format setting, restricting to formats that escape HTML. This should avoid confusion and improve security concerns about using an unsafe format.

Proposed resolution

Included in the above.

Comments

AdamPS created an issue. See original summary.

geek-merlin’s picture

Thanks for fucusing the reasoning and pointing out what this ultimately is: A D7 zombie.

Let's give it a honorable thanks and put it to rest.

adamps’s picture

Status: Active » Needs review
StatusFileSize
new11.82 KB

Here's a patch. I've not properly tested it yet but it gives an idea what we have in mind.

adamps’s picture

StatusFileSize
new11.84 KB

Status: Needs review » Needs work

The last submitted patch, 4: swiftmailer.respect_format.3125041-4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

adamps’s picture

StatusFileSize
new11.84 KB
adamps’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 6: swiftmailer.respect_format.3125041-6.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

adamps’s picture

Status: Needs work » Needs review
StatusFileSize
new13.82 KB
geek-merlin’s picture

Great stab at this! Code looks quite straightforward.

+++ b/src/Plugin/Mail/SwiftMailer.php
@@ -607,13 +532,13 @@ class SwiftMailer implements MailInterface, ContainerFactoryPluginInterface {
+      if (!$is_markup && ($format == SWIFTMAILER_FORMAT_HTML)) {

You hardly ever want to use == in PHP. Use ===.

+++ b/src/Plugin/Mail/SwiftMailer.php
@@ -269,10 +265,14 @@ class SwiftMailer implements MailInterface, ContainerFactoryPluginInterface {
+            $existing_format = preg_match('/.*\;/U', $header_value, $matches) ? trim(substr($matches[0], 0, -1)) : $header_value;

What the heck does this? If it need be that complicated, let's add a code comment. Or is it simply rtrim($header_value, ';')?

berdir’s picture

First, more or less random thoughts:

Not sure if we should treat this and #3125041: Clarify and simplify message settings as separate issues, as usually you'd use them together. Disable respect format and configure a text format so that all plain mails are converted to basic HTML.

A bit hard to keep it separate when reviewing/discussing. As @AdamPS said in #3125042: [META] New & improved format conversions, this was a valid option to send out simple but decently looking e-mails that did not require custom code or extra modules and a lot of configuration/overrides. I'm pretty sure that there are plenty of sites out there where the user.module e-mail configuration contains huge chunks of HTML, because it works in combination with those settings. And that's not really a security issue for those e-mails because configuring them requires administer users permission anyway. But yes, it might be for others if they put unvalidated user input directly in the mail body.

I'm not sure about the exact attack vectors, since e-mail HTML has to be treated as unsafe anyway as everyone can just send you whatever they want directly anyway. But I suppose you could at least trick site admins into doing things as they might trust the e-mails they receive from Drupal and what they show as links and so on.

If we'd keep the other setting then this would indeed be fine to remove, since the other would create markup objects of everything anyway then. If we want to remove that too it's a bit more complicated IMHO.

I agree that these features and options are tricky, but the problem is that the mail API is pretty weird and only saw very limited changes in D8, so it's a bit too simplified to say it's just a D7 leftover and is fine to remove everything. There are a few cases where core actually does pass through markup objects, for example contact_mail() #2666160: contact_mail() casts rendered markup to string, but most e-mails don't and swiftmailer doesn't actually define the API, so it has to deal with what it gets (or rely on other modules and custom code to do so, but that does seem like a bit of a UX regression?)

I still feel like it would be acceptable in terms of security to make the format setting visible, remove this and have very clear descriptions and instructions in the UI that say that you either need to make sure that the only e-mails that are sent contain only trusted content (which I think is the case for most sites) and that misconfiguring it is not a security issue. Having it in the UI actually gives us a place to explain it, as exposed to people using config import/export or drush to change it. Core isn't different, nothing stops you from granting anonymous users access to the Full HTML format, the only "protection" is the help text that says "Improper text format configuration is a security risk. Learn more on the Filter module help page." on /admin/config/content/formats.

adamps’s picture

Issue summary: View changes

Thanks @Berdir. That makes sense.

There's one part that I don't understand, which is why you feel the two issues are related. With my current level of understanding, this issue is safe and removes something that's of no use. The other issue is complicated and we need to think carefully about what to do considering the points that you raise. I imagine that your comments here apply to the other issue.

NB This issue will make the code run as if "Respect format" is always false. From reading your comments I wonder if you imagine the opposite??

If you feel we might need this setting then please can you explain a case where it is useful to have "Respect format" set to true? I explain in the issue summary "proposed resolution" that I believe there are better solutions available.

berdir’s picture

Yeah, I missed that this would make the setting always FALSE. But wouldn't that mean that it would be impossible to *not* send some mails als plain text and some as HTML? I'm not sure if that should really be the goal here, I'm sure some prefer to only send plain text HTML mails, also because sending out good looking HTML mails takes time.

Per your own suggestion, I would propose that we first have real-life functional tests at least for contact and user e-mails, to see what exactly is being sent with common setting combinations for format, respect format, and filter format (plain text | basic html)? And it might also be a good idea to include an integration test with simplenews, which has per-newsletter settings to send or not send HTML mails, removing the respect format setting would break those configuration settings?

geek-merlin’s picture

> But wouldn't that mean that it would be impossible to *not* send some mails als plain text and some as HTML?

Hmm, good point. Re-thinking about this.

adamps’s picture

@Berdir yes good points.

But wouldn't that mean that it would be impossible to *not* send some mails als plain text and some as HTML?

See the IS. Can use mailsystem to configure mails that should be plain text to use the default PHP formatter not swiftmailer.

simplenews, which has per-newsletter settings to send or not send HTML mails, removing the respect format setting would break those configuration settings?

That's a good case. It will be important to know what simplenews sets for the content type header and whether the body implements MarkupInterface.

I propose that it's not the job of Swiftmailer Core to allow detailed per module/id config of the format. However there are perhaps three valid cases:

  • Always HTML (convert if needed)
  • Always Plain (convert if needed)
  • Respect existing

A complication in the third case is how to decide that the existing format is. Drupal Core sometimes sends mails with body containing markup but with content header of plain.

adamps’s picture

Title: Remove "Respect format" setting » Clarify and simplify message settings
Issue summary: View changes

So in conclusion, the function being offered here is fine, it's just the presentation that's confusing. As we are starting a new major version we have a chance to fix this. I've updated the IS accordingly.

adamps’s picture

Issue summary: View changes
adamps’s picture

StatusFileSize
new18.82 KB

I've tried to simplify and remove some really baffling behaviour but minimise breakage to existing sites.

The idea is the same as we have been discussing in other issues: the mail plug-in should not be globally altering content. Instead we can do that case-by-case at the application layer, see #3125477: Add sub-module for more advanced format conversions.

Let's see how the patch works. Comments welcome.

Status: Needs review » Needs work

The last submitted patch, 18: swiftmailer.respect_format.3125041-18.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

adamps’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new23.96 KB

No I discarded too much. Here's a new version that should be good for BC.

Status: Needs review » Needs work

The last submitted patch, 20: swiftmailer.respect_format.3125041-20.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

adamps’s picture

Status: Needs work » Needs review
StatusFileSize
new26.37 KB

Status: Needs review » Needs work

The last submitted patch, 22: swiftmailer.respect_format.3125041-22.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

adamps’s picture

Status: Needs work » Needs review
StatusFileSize
new26.42 KB
adamps’s picture

OK I took out one more part, so now it's just some remapping in preparation for other issues that allow configuration of text format.

adamps’s picture

Issue summary: View changes
adamps’s picture

StatusFileSize
new25.69 KB

Minor optimisation

Status: Needs review » Needs work

The last submitted patch, 27: swiftmailer.respect_format.3125041-27.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

adamps’s picture

Status: Needs work » Needs review
StatusFileSize
new26.25 KB
adamps’s picture

Issue summary: View changes

Patch coming up soon to match the latest IS. I feel this one is nearly ready now.

Sorry it's been another of my "evolving" issues that keep changing title. At first I wanted to cut some options. After some detailed research and checking I concluded that the existing settings each have a purpose - just that it wasn't clearly explained.

adamps’s picture

Issue summary: View changes
StatusFileSize
new27.78 KB

Status: Needs review » Needs work

The last submitted patch, 31: swiftmailer.respect_format.3125041-31.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

adamps’s picture

Status: Needs work » Needs review
StatusFileSize
new30.26 KB
adamps’s picture

Issue tags: +Plan to commit

OK I think I'm ready on this one now. Comments welcome. I will commit after 1 more week anyway because this blocks working on other issues.

This issue doesn't remove any features but it does make things clearer and safer.

  • We still have respect_format, but it's been made easier to understand and clearer that it's not recommended
  • We still have filter_format, but it now has a GUI that protects from unwise options.
geek-merlin’s picture

Nice!

I did not thoroughly review it yet, but the configuration sounds more intuitive really. Will look at this later again.
And yes, this should not block other patches for too long.

adamps’s picture

Couple of small fixes

adamps’s picture

This issue is blocking a stable release and also a beta that supports Drupal 9. After waiting one month I propose that we need to move on, so I will commit it at the start of next week. It would be great to have any more comments first, thanks.

berdir’s picture

We have a task to try it out in our project, but didn't get to it yet. Hectic times :) I'll try to have a quick look at least over the weekend.

adamps’s picture

Great thanks Berdir. Yes we are all busy. Hopefully it will be worth it to get a stable release with D9 support.

geek-merlin’s picture

When that patch was uploaded, i thoroughly reviewed it.
I thought a can test it soon-ish and give feedback then, but then also got hectic times.

So let's at least give feedback on the code: I like it code-wise as a big win.
So when someone did a manual test, let's get it in and build on it.

  • AdamPS committed cb66ed2 on 8.x-2.x
    Issue #3125041 by AdamPS, geek-merlin, Berdir: Clarify and simplify...
adamps’s picture

Status: Needs review » Fixed
Issue tags: -Plan to commit

Thanks @geek-merlin. We need to press on as this is blocking progress to D9 support and stable release.

Now that the general principle and ideas are supported I propose that's enough to commit. If someone tests it and finds a bug then we can raise another issue to fix it.

berdir’s picture

I've been testing this and 2.x in general a bit over the last days and it seems to work well.

adamps’s picture

@Berdir Great thanks for confirmation. One last key non-BC issue #3126935: Improvements to plaintext conversion before I create a beta later in the week if you have a chance to look.

Status: Fixed » Closed (fixed)

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