0

I get a server response which provides us with HTML to be sent via gmail. This works fine on the iOS counterpart of the app, however the text just comes out as plain text in the Android application.

Here is an example part of the response we get:

<html>
    <p style="color: #5987c6">My Shared Itinerary - John Smith.</p>
    <p>Hello.</p>
    <p>I want to share my Memmingen, DE trip itinerary with you.</p>
    <p>Shared using 
        <span style="color: #5987c6">Blah</span> by BlahBlah
    </p>
</html>

And I have tried doing the following:

final Intent shareIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
shareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(new StringBuilder().append(testBody).toString()));

Where testBody is set to the example I gave above. I was just wondering is it possible to have this work straight away with the response from the server, or will I have to do some reformatting once it is received? Thanks for any help

Ayohaych
  • 5,099
  • 7
  • 29
  • 51

1 Answers1

2

I get a server response which provides us with HTML to be sent via gmail.

No, it will be "sent" by the app of the user's choosing, from any of the apps on the device that support ACTION_SENDTO on a mailto: Uri. Or, the user could choose to not send the email at all.

however the text just comes out as plain text in the Android application

It is up to the app that the user chooses to handle your request to determine what to do with your content.

You can maximize the odds of this working by:

  1. Using EXTRA_HTML_TEXT along with EXTRA_TEXT, where EXTRA_HTML_TEXT has the raw HTML

  2. Using HTML that fits with what Html.fromHtml() supports, which notably does not include span tags or style attributes.

Neither of these steps guarantees that Gmail, or any other app, will necessarily faithfully render and use your HTML-formatted message, but they should help.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Sorry, your right, I just meant that it will be sent by gMail as an example. But yeah any mail client pretty much. Hmm so if I want the mail to render correctly I will have to change what the server response is, and omit anything not supported – Ayohaych Feb 27 '15 at 15:33
  • @goodgamerguy: Your HTML also needs to "gracefully degrade", as best as possible, for cases where all the HTML is stripped out. I am sure that there are email clients that totally ignore HTML tags when they present the email prose in an `EditText` in some sort of "compose" activity/fragment. There's nothing you can do about that, as you don't control the email clients. You could send the emails yourself, using the Android JavaMail port that's floating around or the equivalent, though then you start running into security issues. Or, have the server send the email. – CommonsWare Feb 27 '15 at 15:38
  • I'm not sure how to get it working, I added in: shareIntent.putExtra(Intent.EXTRA_HTML_TEXT, Html.fromHtml("text")); Just to test, but the body of the email shows up as blank. – Ayohaych Feb 27 '15 at 15:49
  • @goodgamerguy: Try the actual HTML, not the output of `Html.fromHtml()`. Also, while I would *hope* that Gmail is supporting `EXTRA_HTML_TEXT`, as it has been around a while, I have no idea if it does or not, and so it may fall back to `EXTRA_TEXT` (and there you can try the `Html.fromHtml()` output). – CommonsWare Feb 27 '15 at 15:58
  • Doesn't seem GMail supported EXTRA_HTML_TEXT. I tried doing what you said here: shareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("text")); However, it just showed the words "text" in the email. Not coloured or anything. – Ayohaych Feb 27 '15 at 16:13
  • Managed to get it working by changing the #ff123456 to just say red. I'm guessing it doesn't support custom colours? – Ayohaych Feb 27 '15 at 16:23
  • @goodgamerguy: Hmmm... by my reading of the `fromHtml()` logic, it should. IIRC, though, CSS/HTML colors are usually just six-digit `RRGGBB` values, not `AARRGGBB`, so you might try dropping the leading `ff`. – CommonsWare Feb 27 '15 at 16:28
  • Yeah actually my bad, I didn't remove the CSS way of declaring color, it was or something when it should of been Only thing left to try do now is to get background colours behind text working, but I don't think that would be possible – Ayohaych Feb 27 '15 at 16:36
  • @goodgamerguy: Yes, `Html.fromHtml()` does not support background colors. Now, you're welcome to try [my CWAC-RichTextUtils library](https://github.com/commonsguy/cwac-richtextutils), which does support background colors as part of an XHTML->`Spanned` conversion. However, what an *email client* might do when it encounters a `BackgroundColorSpan` in the `Spanned` is indeterminate. – CommonsWare Feb 27 '15 at 16:43
  • That's unfortunate, if I could get the background colour it would be perfect. Hmm I will look into your library, thanks. Do you know if it works with gMail? – Ayohaych Feb 27 '15 at 16:44
  • @goodgamerguy: I can convert appropriately-constructed XHTML into a `Spanned`, and I know that the `Spanned` can work in an `EditText` (since I have [another library](https://github.com/commonsguy/cwac-richedit) that adds rich text editing to `EditText`). However, I have not attempted to pass that `Spanned` to a third-party app for consumption, let alone to Gmail specifically. Gmail may display the background color in its editor, but whether it actually uses it when sending the email, I cannot say. – CommonsWare Feb 27 '15 at 16:49
  • Alright, well I'm going to have a chat with the other lads on the team and see what they feel is the best option. Thanks for the help man – Ayohaych Feb 27 '15 at 17:02