5

I'm working hard in my app, but I got a problem. Need insert a image in my HTML code from Swift to send a email.

this is my code, but of course doesn't work!

var h = "<html><body leftmargin=15% topmargin=2% marginwidth=7% marginheight=7%>"
h = h + "<img width=100px src=logoICR.png>"
h = h + "</body></html>"
Dharma
  • 3,007
  • 3
  • 23
  • 38
Gilko
  • 69
  • 1
  • 5
  • Have you seen this https://stackoverflow.com/questions/12210571/how-to-add-a-image-in-email-body-using-mfmailcomposeviewcontroller – Prashant Tukadiya Jun 14 '17 at 04:47
  • I dont think you cant send image with in HTML tag.You can only send as attachment – Dharma Jun 14 '17 at 04:49
  • Thak you. I did in email outlook html, But I know the image source, in Swift Xcode, i doña know where si? (Src=??????) – Gilko Jun 18 '17 at 03:58

2 Answers2

9

Please try below code for insert Image inside Email HTML Body.


Code works with Swift 4.0 & Swift 3.1
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate{
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
     
    //MARK: - Send Email
    @IBAction func actionSendEmail(_ sender: Any) {
        
        if !MFMailComposeViewController.canSendMail() {
            print("Mail services are not available")
            return
        }
        let image = UIImage(named:"testImage") // Your Image
        let imageData = UIImagePNGRepresentation(image!) ?? nil
        let base64String = imageData?.base64EncodedString() ?? "" // Your String Image
        let strHtml = "<html><body><p>Header: Hello Test Email</p><p><b><img src='data:image/png;base64,\(String(describing: base64String) )'></b></p></body></html>"
        let composeVC = MFMailComposeViewController()
        composeVC.mailComposeDelegate = self
        // Configure the fields of the interface.
        composeVC.setToRecipients(["address@example.com"])
        composeVC.setSubject("Hello!")
        composeVC.setMessageBody(strHtml, isHTML: true)

        // Present the view controller modally.
        self.present(composeVC, animated: true, completion: nil)
    }
    
    //MARK:- MFMailCompose Delegate Methods
    func mailComposeController(_ controller: MFMailComposeViewController,
                               didFinishWith result: MFMailComposeResult, error: Error?) {
        // Dismiss the mail compose view controller.
        controller.dismiss(animated: true, completion: nil)
    }
    
}
Community
  • 1
  • 1
Anand Nimje
  • 6,163
  • 4
  • 24
  • 43
  • Thank you, but this code does not work for my. I got The code i just need ti find the way of los da image from my image galery (xcasset folder) – Gilko Jun 18 '17 at 04:18
  • This worked for me, make sure to use base64EncodedString and not base64EncodedData, also if you are using the multiline strings, you have to use String(format: args:) and use %@ and put the imageData in the args part without String(describing: ) – Sean Lintern Mar 12 '18 at 15:12
0

Give full path of image's: like

<img width=100px src=http://example.com/images/logoICR.png>
DK Mishra
  • 1
  • 3