17

I am trying to embed a bunch of small images in an html table which I want to send using MFMailComposeViewController. After doing some search I figured out almost everyone is suggesting "NSData+Base64 by Matt Gallagher" class to encode the photo in Base64encoding and embedding it into html. While it looks to work on iPhone and with some email servers but Gmail and Yahoo do not show the images embedded this way. This problem is mentioned here as well.

I was wondering if anyone has a better solution for this. I don't want to upload photos on the web and put the link in the html code. I would like the image to be attached to the email and show up as an inline image within html text,table,...

Community
  • 1
  • 1
Ali
  • 829
  • 1
  • 8
  • 9
  • I posted an answer to a question that may help. http://stackoverflow.com/questions/8836706/base64-html-embedded-images-not-showing-when-mailed/10481035#10481035 – fsaint May 07 '12 at 11:13

3 Answers3

17

This unfortunately cannot be done currently in the way that you desire. Inline images in HTML email use separate MIME parts that are referenced via a content-id from the body of the message. MFMailComposeViewController doesn't give you control over the MIME structure of the message, and doesn't let you add inline referenced content parts.

Embedding image data into <img> tags as base64 can work on some combinations-- it's partly a function of email client and partly of the browser ultimately used to render it, but as you have noticed, it's not broadly portable.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • 3
    It seems that even mail app (which seems to use MFMailComposeViewController) cannot embed photos within text and sends them as attachment. So, you are most probably right that it is impossible. What a shame! Thanks anyway. – Ali Nov 23 '10 at 14:53
6

I'm not sure about adding to HTML, but here's how I add images as attachments:

UIImage *image = [UIImage imageNamed:@"myImage.png"];
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;

NSData *data = UIImagePNGRepresentation(image);
[composer addAttachmentData:data mimeType:@"image/png" fileName:@"curse"];
[composer setMessageBody:@"" isHTML:NO];
[self presentModalViewController:composer animated:YES];
[composer release];

Now all you need is to change the body to be HTML and a way to reference the attached image from the HTML.

jbenet
  • 3,030
  • 3
  • 22
  • 25
  • 3
    Although this is how you add image attachments, the question was asking in fact how to reference the attached images in the HTML (which isn't actually possible). – Ben Zotto Nov 21 '10 at 18:31
  • @quixoto: the question states: "I would like the image to be attached to the email", i answered that part. I even state: "Now all you need is to change the body to be HTML and a way to reference the attached image from the HTML." A partial solution is always better than no solution. – jbenet Nov 21 '10 at 18:36
  • 2
    Understood. For what this is worth: I'm not sure that partial solutions to problems with clear answers have a significant place here. Stack Overflow is a huge community, and there are almost always people who know the answer out there. Leaving incomplete answers often just adds noise. (You can always leave a comment on the question for clarification or adding some info without giving a full answer, etc.) – Ben Zotto Nov 21 '10 at 22:39
  • 4
    "Now all you need is to change the body to be HTML and a way to reference the attached image from the HTML." How? That's the question. – mahboudz Nov 21 '11 at 01:33
3

Upload the image to some hosting site, copy the link and put that to

<img src='the link.png' /> html on your email message body.
Allan Macatingrao
  • 2,071
  • 1
  • 20
  • 28