Some Notes for Sending HTML Email in Android

I am in the middle of working on an android app that needs to be able to send a table of records in an email. Ideally, I would be able to send a pretty html table, because that's what the iPhone version of this app can do... and it's apparently very simple to do it "over there".

Using the (obvious) "just start an activity with the right Intent.ACTION_SEND intent" does not work, as android will not pass the <table> tag to whatever intent may want to handle it.  This turns out to be an example of one of those things that, although android can get you close to your solution with a few lines of code, you're gonna have to work a bit for it to get anywhere close to what you want.

This led me to explore exactly which html tags you can pass this way that will end up in the email.

In Android, the basic idea in sending html in email is to take some html in a string, then pass it to Html.fromHtml, and stick what you get back into the Intent.EXTRA_TEXT extra for the intent. The fromHtml method ultimately will call Html.HtmlToSpannedConverter.handleStartTag/handleEndTag, and that is what I looked at to derive the table below (Android 2.1-2.3). 

Note that you could write your own Html.TagHandler to handle other tags, but this won't matter to the Intent that receives the data because it won't know what to do with the information.  I think you'll have to wait on the framework to change.  If you really want all that extra html in an email, you'll need to go low-level and use the java mail api directly.

In my case, since the <tt> tag is supported, I can at least get some very basic "tables" whose columns do not get mangled with a gmail client when sent from the gmail android app.

Mark Murphy compiled the list of supported tags, in the context of what a TextView can do, back in May 2010, and entered an issue for google to add some documentation... which hasn't materialized yet.

HTML Tags Handled by Html.fromHtml
HTML TagComment
<br> 
<p>No styles supported
<div>Treated identically to <p>
<em>Text will be bold - this is a bug in Android
<b>Text will be bold
<strong>Text will be italic - this is a bug in Android
<cite>Text will be italic
<def>Text will be italic
<i>Text will be italic
<big> 
<small> 
<font>Can handle color, face and size
<blockquote>Always inserts paragraph
<tt>Will be monospace
<a> 
<u> 
<sup> 
<sub> 
<hN>Handles heading levels N=1 to 6
<img>You need to write your own Html.ImageGetter for this to work - I've haven't messed with this yet
Anything elseYou can implement a Html.TagHandler to deal with this, but won't matter if data is passed to an external Intent outside of your app

Note - there still be quirks out there - for example, apparently the default email client for 2.2 and earlier couldn't handle html at all (http://code.google.com/p/android/issues/detail?id=16109).
For the time being, the best advice is probably to just avoid html email if you can!

Final note - it is a simple matter to write the html to a file, and then attach the file to the email by using the EXTRA_STREAM extra for the intent.   However, besides adding an extra step for the receiving user to view the information, this requires that the file be readable by any process, which might not be acceptable depending on the nature of the data involved.

2 comments:

  1. How would I send an html email with a fixed width font?

    Are these supported: PRE and CODE and css font-family?

    ReplyDelete
  2. Hi Carol - I'd try <tt>, which IS supported. PRE and CODE do not seem to be (check out the source: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/Html.java)

    ReplyDelete

Popular Posts