Append UIImage to mail

319 views Asked by At

I would like to send an e-mail from my app. I have solved the problem so far and the sending also works flawlessly:

func showMailComposer() {
    
    guard MFMailComposeViewController.canSendMail() else {
        return
    }
    
    let composer = MFMailComposeViewController()
    composer.mailComposeDelegate = self
    composer.setToRecipients(["[email protected]"])
    composer.setSubject("example")
    composer.setMessageBody(""

        
       , isHTML: false)
    
    present(composer, animated: true)
}

Now I would like to send a signature, that was made by the user, with the mail. I already exported the signature as UIImage.

Now I do not know how I can attach the UIImage to the mail.

How do you do that?

Many thanks in advance!

1

There are 1 answers

5
Tarun Tyagi On

You can add attachment data using this api - https://developer.apple.com/documentation/messageui/mfmailcomposeviewcontroller/1616885-addattachmentdata

func addAttachmentData(_ attachment: Data, 
              mimeType: String, 
              fileName filename: String)

From the docs -

attachment

The data to attach. Typically, this is the contents of a file that you want to include. This parameter must not be nil.

mimeType

The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be nil.

filename

The preferred filename to associate with the data. This is the default name applied to the file when it is transferred to its destination. Any path separator (/) characters in the filename are converted to underscore (_) characters prior to transmission. This parameter must not be nil.


UPDATE

For the attached screenshot - enter image description here

The call would look like -

composer.addAttachmentData(
    unterschriftFoto?.jpegData(compressionQuality: 0.99) ?? Data(),
    mimeType: "image/jpeg",
    filename: "unterschriftFoto.jpeg" /// or whatever you like to call it
)