Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
861 views
in Technique[技术] by (71.8m points)

iphone - Base64 HTML embedded images not showing when mailed

I am embedding images that have been base64 encoded in HTML as follows:

[html appendFormat:@"<html><body><p><b><img src="data:image/png;base64,%@"></b></p></body><html>", base64ImageString];

I then create a new email as follows:

MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
mailVC.mailComposeDelegate = self;
[mailVC setMessageBody:html isHTML:YES];
[self presentModalViewController:mailVC animated:YES];

The embedded image does show up in the new email before it is sent, but is not displayed in any email client to which the mail is delivered. I would think the fact that the image properly shows in the draft shows that the embedding process is successful, but I dont understand why it does not show when delivered. Looking at the raw HTML in the delivered mail shows: src="cid:(null)" Any help would be appreciated please!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I stumbled into this same problem and the solution was rather convoluted. It is possible to embed an image in an email. The problem is that for some weird reason the base64 encoded image must not contain new lines (super odd! I know). I'm guessing you are using the NSData+Base64 by Matt Gallagher? So was I! This category creates a multi-line base64 string. The code in the category is

- (NSString *)base64EncodedString
{
    size_t outputLength;
    char *outputBuffer =
        NewBase64Encode([self bytes], [self length], true, &outputLength);

    NSString *result =
        [[NSString alloc]
            initWithBytes:outputBuffer
            length:outputLength
            encoding:NSASCIIStringEncoding];
    free(outputBuffer);
    return result;
}

By replacing the third parameter of NewBase64Encode to false you will get a single line base64 string and that worked for me. I ended up creating a new function (just not to break anything else!) within the category.

- (NSString *)base64EncodedStringSingleLine
{
    size_t outputLength;
    char *outputBuffer =
    NewBase64Encode([self bytes], [self length], false, &outputLength);

    NSString *result =
    [[NSString alloc]
     initWithBytes:outputBuffer
     length:outputLength
     encoding:NSASCIIStringEncoding];
    free(outputBuffer);
    return result;
}

Using this function to encode the UIImage's NSData worked fine. The email clients I have tested so far all show the embedded image. Hope it works for you!

Edit: As pointed out in the comments, this solution is only partial. The image will be attached as a Data URI in the email. However, not all email clients will display the embedded image.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...