Base64 HTML嵌入图像在邮寄时不显示 [英] Base64 HTML embedded images not showing when mailed

查看:510
本文介绍了Base64 HTML嵌入图像在邮寄时不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在嵌入以HTML格式编码的图像,如下所示:

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];

嵌入的图片在发送之前会显示在新电子邮件中,但不会显示在任何内容中邮件发送到的邮件客户端。我认为草图中正确显示的图像显示嵌入过程是成功的,但我不明白为什么它在交付时不显示。查看已发送邮件中的原始HTML显示:src =cid:(null)任何帮助将不胜感激!

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!

推荐答案

我偶然发现了同样的问题,解决方案相当复杂。可以在电子邮件中嵌入图像。问题是,由于一些奇怪的原因,base64编码的图像不能包含新行(超奇怪!我知道)。我猜你正在使用Matt Gallagher的NSData + Base64?我也是!此类别创建多行base64字符串。该类别中的代码是

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;
}

通过将NewBase64Encode的第三个参数替换为false,您将获得单行base64字符串,这对我有用。我最终在类别中创建了一个新函数(只是为了不打破其他任何东西!)。

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;
}

使用此函数对UIImage的NSData进行编码工作正常。我到目前为止测试的电子邮件客户端都显示了嵌入的图像。希望它适合你!

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!

编辑:正如评论中所指出的,这个解决方案只是部分解决方案。该图像将作为数据URI附加到电子邮件中。但是,并非所有电子邮件客户端都会显示嵌入的图像。

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.

这篇关于Base64 HTML嵌入图像在邮寄时不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆