【问题标题】:How to embed an image in the HTML body of an email in iOS如何在 iOS 中将图像嵌入到电子邮件的 HTML 正文中
【发布时间】:2012-05-10 23:37:40
【问题描述】:

我正在尝试在从 iPad 发送的 HTML 电子邮件的正文中包含图像。这似乎是不可能的。我曾尝试使用 CID 方法,但似乎在 iOS 中无法获取/设置附件的 CID。

我还尝试使用src="data:image/png;base64, blahblahblah" 嵌入图像。当您撰写邮件时,它似乎可以工作,但收到邮件时却没有任何显示。

有什么想法吗?

更多详情: 我们不是在寻找将 JPEG/PNG 附加在电子邮件底部的解决方案。使用[composer addAttachmentData:mimeType:fileName:] 很容易做到这一点。

我们正在寻找一种解决方案,将图像嵌入内联到 HTML 格式的电子邮件中。您可以在该 IMG 标签周围添加一个链接,这样当收件人单击 IMG 时,他/她将被链接到应用程序的 iTunes 页面。

【问题讨论】:

标签: html ios email mfmailcomposeviewcontroller email-attachments


【解决方案1】:

github 下载NSData+base64 类别。

然后执行以下操作:

NSData *imageData = [NSData dataWithContentsOfFile:pathInDocumentDirectory(imagePath)];
NSString *base64String = [imageData base64EncodedString];
NSString *imageString = [NSString stringWithFormat:@"data:image/png;base64,%@", base64String];

最后,将imageString 放入您希望此图像出现的 HTML 正文中。

希望对你有帮助!

【讨论】:

  • 这非常接近,但不太有效,因为像 GMail 这样的一些 Web 客户端不会使用数据 URI 呈现 img 标签。 :-(我用一个小红点PNG作为数据URI进行了测试,图像在iPad邮件客户端和雅虎邮件中显示良好,但在GMail中失败了(尽管对原始电子邮件的检查表明base64 字符串存在)。
  • 我能做的最好的事情就是让我的 IMG 标签指向我在外部网站上托管的 PNG。但这也不是一个很好的解决方案,因为许多 Web 客户端默认会阻止外部图像。无论如何,这个 base64 解决方案非常接近,所以我很高兴获得赏金。 :-)
  • 很遗憾,Gmail web 无法解释它:/
【解决方案2】:

来自iphone email attachment

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Hello"];


// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"]; 
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com",           @"third@example.com", nil]; 
NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; 

[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];  
[picker setBccRecipients:bccRecipients];

// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];

// Fill out the email body text
NSString *emailBody = @"It is raining";
[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];
[picker release];  

【讨论】:

    【解决方案3】:

    要在 gmail 中显示图片,您可以:

    MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
        mailCont.mailComposeDelegate = self; 
    
        NSMutableString *emailBody = [[NSMutableString alloc] initWithCapacity:20];
    
        NSString *linkimg = @"https://idrivethru.com/iDriveThruWeb/faces/javax.faces.resource/idrivethru_logo.png?ln=images";
    
        //Add the image
        [emailBody appendFormat:@"<p><a href = 'https://idrivethru.com/'> <img src='%@' align='centre' alt='iDriveThru.com'> </a></p><br/>", linkimg];
    
        [emailBody appendString:@"<p>This is an email with an embeded image right <b>above</b> this text</p>"];
    
        //NSLog(@"%@",emailBody);
    
        [mailCont setMessageBody:emailBody isHTML:YES];
        [self presentViewController:mailCont animated:YES completion:nil];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 2012-03-30
      • 2010-12-23
      • 2019-08-29
      • 1970-01-01
      • 2011-10-06
      相关资源
      最近更新 更多