1

I know this question has been asked before, but I haven't seen anything recent so am hoping for a fix. Currently I am sending an email with an html body that contains two image tags. Originally I was just doing something like

<img width="100" src="http://cdn1-www.dogtime.com/assets/uploads/gallery/30-impossibly-cute-puppies/impossibly-cute-puppy-2.jpg" >

but now am just trying to use a local image and use base64 encoding, so something like

<img width="100" src="data:image/png;base64,...>

So far I've had success with that from within iOSs mail but once I open it in Gmail or Outlook or anything else it isn't working.

Has any one found a solution to this?

My code...

let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients([String]())
mailComposer.setSubject("email with image in body")
mailComposer.setMessageBody(getHtmlBody(), isHTML: true)
self.presentViewController(mailComposer, animated : true, completion: nil)

getHtmlBody(){
    let imgData:NSData = UIImagePNGRepresentation(UIImage(named : "logo")!)
    let dataString = imgData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

    var html = "<!DOCTYPE html>"
    html = html + "<html>"
    html = html + "<body>" 
    html = html + "<h1>Hello World, look at this image <img width=\"100\" src=\"data:image/png;base64,\(dataString)\"></h1>"
    html = html + "</body>"
    html = html + "</html>" 
    return html 
}
Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
John
  • 1,808
  • 7
  • 28
  • 57

1 Answers1

0

Sending html body with img tag whose src is base64 is the right approach to achieve inline image attachments.

Faced a similar issue earlier, Frankly i dont have a solution for it. However some mail clients like Outlook, Gmail consider sending base64 image as a malicious approach since the contents of the image cannot be scanned to identify if the image has inappropriate content. this would sometimes cause to move the mail to spam by default. In some email clients it doesn't seem to be an issue.

RamaKrishna Chunduri
  • 1,100
  • 1
  • 11
  • 15