Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed image in html in MFMailComposeViewController for iPhone

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,...

like image 428
Ali Avatar asked Nov 21 '10 18:11

Ali


2 Answers

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.

like image 156
Ben Zotto Avatar answered Oct 04 '22 11:10

Ben Zotto


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.

like image 42
jbenet Avatar answered Oct 04 '22 12:10

jbenet