user1026605
user1026605

Reputation: 1430

Send email with HTML content

I'm trying to send an email with an HTML body. I have a String that contains some valid html:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
</head>
<body>%s</body>
</html>

from which I replace %s by some HTML.

Then I try to send this as an HTML email with the following code:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/html");
    intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
    intent.putExtra(Intent.EXTRA_TEXT, myhtmlcontent);
    context.startActivity(Intent.createChooser(intent, context.getText(R.string.share)));

but I only receive the html source code as plain text

Now if I replace

intent.putExtra(Intent.EXTRA_TEXT, myhtmlcontent);

with

intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(myhtmlcontent));

I receive a valid HTML email, but all my images have been replaced with some special character. Is there a way to keep my original html content and display it as a HTML email ?

Here's my full HTML code

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body><p><a href="http://www.dailymars.net/wp-content/uploads/2014/05/Ant-Man.jpg"><img
        class="aligncenter size-full wp-image-55602"
        src="http://www.dailymars.net/wp-content/uploads/2014/05/Ant-Man.jpg" alt="Ant-Man"
        width="720" height="302"/></a></p>

<p>Les enfants, l&rsquo;heure est grave. Vous l&rsquo;avez compris car vous l&rsquo;avez lu dans le
    titre de la news (eh ouaip!), <strong>Edgar Wright</strong> ne réalisera finalement pas <em>Ant
        Man</em>. Alors qu&rsquo;il était rattaché au projet depuis 2006 et qu&rsquo;il en avait
    écrit le scénario avec <strong>Joe Cornish</strong>, <strong>Marvel </strong>a annoncé que le
    réal avait quitté le navire. La faute à quoi? Des &laquo;&nbsp;différences créatives&nbsp;&raquo;,
    soit la meilleure excuse qu&rsquo;Hollywood aie jamais inventé.</p>

<p><strong>Marvel </strong>a par ailleurs ajouté que le départ de <strong>Wright </strong> ne
    changerait rien au casting ni à la date de sortie du film et qu&rsquo;un remplaçant, dont le nom
    devrait bientôt tomber, avait déjà été engagé. Dommage, ça n&rsquo;aura pas la même saveur.</p>

<p><em>Ant Man</em> avec <strong>Paul Rudd, Michael Douglas, Evangeline Lilly, Corey Stoll, Michael
    Pena</strong> et <strong>Patrick Wilson.</strong> Mais sans <strong>Edgar Wright</strong>.
    Sortie en juillet 2015</p>

<p style="text-align: right;"><em>Source: <a
        href="http://variety.com/2014/film/news/edgar-wright-leaves-marvels-ant-man-1201190458/">Variety</a></em>
</p>
</body>
</html>

Upvotes: 2

Views: 2524

Answers (2)

CommonsWare
CommonsWare

Reputation: 1007658

The interpretation of any Intent is up to the receiver. There are many ACTION_SEND handlers, including many email apps. There are no requirements for the HTML being sent out by the email app to be identical to the HTML that you supply.

In particular, few (if any) email apps will support HTML that contains some of the structures that your HTML contains, such as:

  • <meta>
  • class attributes
  • style attributes

That is because most email apps will be using EditText to allow the user to edit the email, and most email apps will use Html.fromHtml() to generate the Spanned to supply to EditText as the starting text. Html.fromHtml() does not support the things I cited. This blog post cites what was supported back in 2010, and AFAIK it has not changed much since then.

So, if you stick to HTML tags and attributes that work with Html.fromHtml(), you will maximize the odds that your HTML will be sent intact, or close to intact. However, there is no guarantee that it will, as that is up to the email app author, not you.

Upvotes: 2

amey91
amey91

Reputation: 572

What you have done seems right. Just upload the images on your server and give actual links to your images in the BODY of the email using the < img src="/link/to/your/image" /> tag.

Upvotes: 0

Related Questions