Display local image in iPhone HTML mail

自闭症网瘾萝莉.ら 提交于 2019-11-29 05:12:33
Xav

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.

Sjakelien
pheelicks

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!