简体   繁体   English

Base64 HTML嵌入图像在邮寄时不显示

[英]Base64 HTML embedded images not showing when mailed

I am embedding images that have been base64 encoded in HTML as follows: 我正在嵌入已经在HTML中编码base64的图像,如下所示:

[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! 查看交付邮件中的原始HTML显示:src =“cid:(null)”任何帮助将不胜感激!

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). 问题是,由于一些奇怪的原因,base64编码的图像不能包含新行(超奇怪!我知道)。 I'm guessing you are using the NSData+Base64 by Matt Gallagher? 我猜你正在使用Matt Gallagher的NSData + Base64? So was I! 我也是! This category creates a multi-line base64 string. 此类别创建多行base64字符串。 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. 通过将NewBase64Encode的第三个参数替换为false,您将获得单行base64字符串,这对我有用。 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. 使用此函数对UIImage的NSData进行编码工作正常。 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. 该图像将作为数据URI附加到电子邮件中。 However, not all email clients will display the embedded image. 但是,并非所有电子邮件客户端都会显示嵌入的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM