Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newlines from body stripped in some configurations #11

Open
calvarez-ov opened this issue Sep 15, 2020 · 6 comments
Open

Newlines from body stripped in some configurations #11

calvarez-ov opened this issue Sep 15, 2020 · 6 comments

Comments

@calvarez-ov
Copy link

On a Samsung Galaxy S8, Android 9 (and maybe other configurations), newlines aren't included when selecting Gmail. They are included when selecting the default Samsung mail app.

Steps to reproduce:

  • Open the sample app
  • Enter an e-mail body which contains some newlines
  • Click "send email"
  • Select Gmail
  • Expected behavior: the newlines appear in the mail body
  • Actual behavior: the newlines are stripped: all paragraphs run together

Screenshots:
Sample app:

Gmail missing newlines:

Samsung mail app contains newlines ok:

@calvarez-ov
Copy link
Author

calvarez-ov commented Sep 15, 2020

Disclaimer: I'm not actually using this lib, but came across the blog post mentioning it, when troubleshooting issues sending e-mails. This project is a great way to try out different approaches.

I'm convinced it's not possible to have newlines working properly in all configurations! (By configuration, I mean phone model + android version + email client). Depending on how the intent is created, either some configurations will have newlines stripped (like gmail here), or some different configurations will have literal <br> tags present (if you replace \n with <br> as below):

diff --git a/library/src/main/java/de/cketti/mailto/EmailIntentBuilder.java b/library/src/main/java/de/cketti/mailto/EmailIntentBuilder.java
index ccc1687..eb0dfc4 100644
--- a/library/src/main/java/de/cketti/mailto/EmailIntentBuilder.java
+++ b/library/src/main/java/de/cketti/mailto/EmailIntentBuilder.java
@@ -335,6 +335,6 @@ public final class EmailIntentBuilder {
 
     @NotNull
     static String fixLineBreaks(String text) {
-        return text.replaceAll("\r\n", "\n").replace('\r', '\n').replaceAll("\n", "\r\n");
+        return text.replaceAll("\r\n", "\n").replace('\r', '\n').replaceAll("\n", "<br>\r\n");
     }
 }

But I'd be happy to be proved wrong 😅

If not, I guess the approach should be to find a solution that:

  • works correctly on more devices than not
  • isn't too bad on devices where it doesn't work correctly. Maybe having <br> characters in a mail is better than having newlines stripped...... 🤷‍♀️

@calvarez-ov
Copy link
Author

Here's some test results that can maybe help. These were testing our app, which doesn't use this library, but it basically does the same thing as this library does in fixLineBreaks().

The first table is doing what the library does.

The second table is with the patch to include the <br> tag.

What this library does:
Summary:

  • 18/23 ok
  • 4/23 missing newlines
  • 1/23 double mail

Details:

Device  Android version Email app Behavior
Mi A2 Lite 10 Gmail ok
Emulator 10 Gmail ok
Samsung Galaxy S10 10 Gmail ok
Samsung Galaxy S10 10 Outlook ok
Samsung Galaxy S9 9 Gmail ok
Samsung Galaxy S9 9 Default ok
Samsung Galaxy S8 9 Gmail missing newlines
Samsung Galaxy S8 9 Default Samsung mail ok
Samsung Galaxy A3 8 Gmail missing newlines
Samsung Galaxy A3 8 Default Samsung mail ok
Nokia 8 8 Gmail ok
Nokia 8 8 Email ok
Huawei P9 Lite 7 Gmail missing newlines
Huawei P10 7 Gmail ok
Huawei P10 7 Outlook ok
Meizu Note 5 7 Default meizu mail ok
Samsung Galaxy S5 6 Gmail ok
Samsung Galaxy S5 6 Default Samsung mail ok
Samsung Galaxy S7 6 Gmail ok
Samsung Galaxy S7 6 Default double mail: both ok
LG G3 5 Gmail ok
One Plus 5 Gmail missing newlines
One Plus 5 Email ok

Patch to include <br> tag.
Summary:

  • 12/23 ok
  • 10/23 have br tags
  • 1/23 has a double mail

Details:

Device  Android version Email app Behavior
Mi A2 Lite 10 Gmail <br> tags
Emulator 10 Gmail ok
Samsung Galaxy S10 10 Gmail <br> tags
Samsung Galaxy S10 10 Outlook ok
Samsung Galaxy S9 9 Gmail <br> tags
Samsung Galaxy S9 9 Default Samsung mail <br> tags
Samsung Galaxy S8 9 Gmail ok
Samsung Galaxy S8 9 Default Samsung mail <br> tags
Samsung Galaxy A3 8 Gmail ok
Samsung Galaxy A3 8 Default Samsung mail <br> tags
Nokia 8 8 Gmail <br> tags
Nokia 8 8 Outlook ok
Huawei P9 Lite 7 Gmail ok
Huawei P10 7 Gmail <br> tags
Huawei P10 7 Outlook ok
Meizu Note 5 7 Default meizu mail ok
Samsung Galaxy S5 6 Gmail ok
Samsung Galaxy S5 6 Default Samsung mail <br> tags
Samsung Galaxy S7 6 Gmail <br> tags
Samsung Galaxy S7 6 Default mail double mail: <br> tags, then ok
LG G3 5 Gmail ok
One Plus 5 Gmail ok
One Plus 5 Email ok

@cketti
Copy link
Owner

cketti commented Sep 15, 2020

The specification for mailto URIs is clear. The body parameter is plain text. Every app that interprets text like <br> in the body parameter as HTML tag is buggy and should be fixed (it's not an extension to the RFC, it's a violation because now <br> means different things based on which "standard" an email client implements). Similarly, every app that ignores \r\n line breaks is buggy and should be fixed.

Gmail used to work fine. At some point, probably earlier this year, they broke their mailto URI handling code (see issue #8). I'm guessing they fixed it, but some Android versions and/or devices still get a buggy version.

I'd strongly recommend against using <br> in a mailto URI. It'll just add another app that violates the official standard and propagates an undocumented shadow standard.
You could experiment with providing the "undocumented" EXTRA_TEXT extra in addition to the text being part of the mailto URI. If you're lucky buggy Gmail versions will use that over the body parameter value.
Another option is leaving out the body parameter in the mailto URI and only providing the text via EXTRA_TEXT. That's a bit of a gamble because it is using undocumented functionality. But this misuse is very common. So probably every client out there will support it. This still has the shadow standard problem, but at least it's not using the mailto URI in a non-standard-compliant way.

@calvarez-ov
Copy link
Author

Thanks for the feedback!

Actually I didn't mention that in addition to the full Uri (with to, subject, and body), we also send EXTRA_TEXT with the original newlines, unmodified. (I think just \n in our case), as well as EXTRA_SUBJECT (Strangely we don't use EXTRA_EMAIL but I'm not sure why 🤔 ). Looks like all these extras are actually documented to work with ACTION_SENDTO, though, here: https://developer.android.com/guide/components/intents-common#Email

We've opted to not include the <br> for pragmatic reasons: there's more configurations with unintended bad behavior with the brs (literal brs appearing) than without (newlines being stripped).

Note that I did play around with removing the body from the uri and only specifying it in EXTRA_TEXT. In that case, on some devices, the body doesn't appear at all 😅 (I don't have data on how many devices had this issue, but it was quite common from memory). Love fragmentation!

@calvarez-ov
Copy link
Author

calvarez-ov commented Sep 15, 2020

btw, indeed the "missing newlines" issue only appeared in Gmail. We also tested other mail clients like samsung mail and outlook too, it's mentioned in the tables in the previous comment. I hadn't noticed that it was only Gmail which had this problem. Thanks for the reference to #8

@cketti
Copy link
Owner

cketti commented Sep 15, 2020

Looks like all these extras are actually documented to work with ACTION_SENDTO, though, here: https://developer.android.com/guide/components/intents-common#Email

I don't consider that page to be part of the API documentation. It's just a guide created by people at Google who probably made the same mistakes as the rest of the internet (consult StackOverflow rather than the API documentation).

FWIW I'm all for changing the API documentation to reflect the reality that everyone is using these extras. But I haven't been successful so far (see https://issuetracker.google.com/issues/36946106).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants