开发者

Display local image in iPhone HTML mail

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 开发者_JAVA技巧I should put there? I do appreciate your insights! Regards Sjakelien


Unfortunately this is not supported by the iPhone 3.x APIs.

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


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:image001.jpg@01CACC43.7035CE50">

The attachment sub-part looks like:

Content-Type: image/jpeg;
    name="image001.jpg"
Content-Transfer-Encoding: base64
Content-ID: <image001.jpg@01CACC43.7035CE50>

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.


I found this post, that answers most of my questions: http://www.iphonedevsdk.com/forum/iphone-sdk-development/25021-embedding-image-email-body.html.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜