views:

701

answers:

6

Hello there, In my app, I am composing an HMTL email message with the 3.0+ MFMailComposeViewController. To do this, I created an HTML file, with some placeholders. In my code, I read the HTML file, and with replaceOccurrencesOfString, I replace the placeholders with data from the app. In that way, I compose the body of the email I want to send out. This is all working very nicely, except for the fact, that in my HTML file, I have an <img src='imageplaceholderpath' /> tag. Somehow, I cannot figure out, with what I should replace this imageplaceholderpath, in order to refer to an image that resides in my app. Is this a valid approach at all, and if so, what would be the syntax/logic behind the path I should put there? I do appreciate your insights! Regards Sjakelien

A: 

Try getting the path from NSBundle. Something like

[[NSBundle mainBundle]
  pathForResource:@"myImage" ofType:@".jpg"];

NSBundle documentation

matthewc
While this will get a path to an image in your app, that isn't going to do much for the recipient of the email. Even if they were reading the email on an iPhone and had installed your app, Mail probably can't/won't load that image.
Steve Madsen
A: 

I don't think you can embed the images as part of the email in the way a normal email client would. However it seems that you can include the image data directly in the HTML as base64 encoded data. This is quite a non-standard way of doing things, so the email might not display perfectly on all email clients.

See this question for more, and the sample code on this forum post

pheelicks
A: 

I found this post, that answers most of my questions: http://www.iphonedevsdk.com/forum/iphone-sdk-development/25021-embedding-image-email-body.html. Does anybody know how to make sure this works for all (modern) email clients?

Sjakelien
A: 

I don't know if the HTML format is a must have for you, but actually embedding an image in an email can be achieved without using HTML, just with image as attachment.

Just have a look at the way it is achieved here :

http://iphone-dev-tips.alterplay.com/2009/11/attaching-image-of-uiview-to-email.html

the crucial part is this :

// ATTACHING A SCREENSHOT
NSData *myData = UIImagePNGRepresentation(screenshot);
[controller addAttachmentData:myData mimeType:@"image/png" fileName:@"route"];

You get the PNG representation of your UIImage (as NSData) and you attach it yo your email.

yonel
I think HTML is something I need, since I want to have control over the text formatting of the message. More importantly, I want the image to appear inline in the text, at a specific position.
Sjakelien
+1  A: 

Note that using data: URIs won't work across all mail clients. Those that use IE as a rendering engine don't support it at all unless IE8 is installed, and even then, according to Wikipedia, data: URIs are limited to 32 KB maximum.

The very simplest way to get this to work is to put the image on your own server somewhere, and reference it using a full http:// URI. If you can't do that for some reason (maybe the image is generated as part of using your app), then you can try attaching the image as a MIME sub-part and referencing it from the HTML.

My mail client doesn't load remote images automatically, but some spam still has images when I open it. This is how it works:

Attach an image to your mail as suggested by yonel. Somehow you need to also add a Content-ID: header to the sub-part. The contents of this header are then used as the src attribute on your image. My spam message looks like this in the HTML:

<img src="cid:[email protected]">

The attachment sub-part looks like:

Content-Type: image/jpeg;
    name="image001.jpg"
Content-Transfer-Encoding: base64
Content-ID: <[email protected]>

Looking at the documentation for addAttachmentData:mimeType:fileName:, my guess is that you won't be able to get this to work and will have to consider sending the email using raw SMTP.

Steve Madsen
A: 

Unfortunatly this is not supported by the iPhone apis 3.x apis.

ref: http://developer.apple.com/iphone/library/documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html

It would require Content-ID: to be part of the attachment subpart but it is not.

  • (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename
Xav