2

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>
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
user1026605
  • 1,633
  • 4
  • 22
  • 58
  • Check this out: http://stackoverflow.com/a/5216927/812598 – GoRoS May 24 '14 at 17:09
  • It doesn't help. I know displaying images will require the user to accept it. My problem is that I'm either receiving my html source code as plain text or I'm receiving HTML but images are stripped away by Html.fromHtml()... – user1026605 May 24 '14 at 17:18

2 Answers2

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yes Html.fromHtml() is almost working for me. It's just too bad it's removing the img tags... I looking for a way to keep them if possible – user1026605 May 24 '14 at 18:01
  • @user1026605: Well, the `src` attribute is the only attribute of yours that might possibly be honored. Even that requires the host app to implement an `ImageGetter` that can retrieve the image from the Web, and not all email apps will do that. – CommonsWare May 24 '14 at 18:03
  • Using this answer I understand why it's not possible to get the images through intents : http://stackoverflow.com/a/6476890/1026605 – user1026605 May 24 '14 at 18:24
  • @user1026605: That would only apply for the case where you ran `Html.fromHtml()` yourself and put the `Spanned` in the `Intent` extra. That would not affect passing HTML source in the `Intent` extra. – CommonsWare May 24 '14 at 18:27
  • 1
    Yes but this is the only thing that seems to work (partially). If I call intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(myhtmlcontent)); then I get the text formatted correctly but loses the images, and if I call ntent.putExtra(Intent.EXTRA_TEXT, myhtmlcontent); I just get my html source code displayed as plain text... – user1026605 May 24 '14 at 22:17
  • @user1026605: Yeah, I am seeing that in the other answers on this topic. I'm kinda stunned they settled on using `Spanned` extras for this. It's certainly not how I would have done it. On the other hand, nobody asked me for my opinion... :-) – CommonsWare May 24 '14 at 22:47
  • do you have some reference, link ? – Francisco Corrales Morales Jan 09 '15 at 01:12
  • @FranciscoCorralesMorales: For what? The fact that there are many email apps can be determined simply by looking at the Play Store, for example. – CommonsWare Jan 09 '15 at 01:17
  • @FranciscoCorralesMorales: In the blog post that I linked to, I point out that `` tags are supported by `Html.fromHtml()`. That is still true, as indicated by [the source to the `Html` class](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/text/Html.java). If you have further concerns about `` tag handling in `Html.fromHtml()`, ask a separate Stack Overflow question. – CommonsWare Jan 09 '15 at 12:01
0

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.

amey91
  • 542
  • 6
  • 17
  • 1
    It doesn't help. With the 1st solution I'm receiving my html source code and with the second images are stripped away – user1026605 May 24 '14 at 17:17
  • I see. The problem might be html/text. Try to use intent.setType("application/image"). Refer: http://stackoverflow.com/questions/14457457/android-intent-send-an-email-with-image-attachment – amey91 May 24 '14 at 17:43
  • It doesn't change anything. Still receiving plain html source code – user1026605 May 24 '14 at 17:52