gabrieltheodoropoulos/EmailComposer

AttachmentData Example

JulesMoorhouse opened this issue · 4 comments

Hi,

This looks great, I've been having a look through the documentation, I've seen the method descriptions.
However, It would be great to see a full example of how to add AttachmentData / files etc.

Thanks,

Jules.

Hi Jules, thank you! This blog post (also on Medium) where I'm going through the implementation of the EmailComposer and then demonstrate how to use it might be helpful for you.

Thanks @gabrieltheodoropoulos

I was really looking for an example project, where I could quickly play around and evaluate.
It seems common to see example code with a package repo.

Specially the file format and mime string etc.

Hi Jules,

To include attachments to the email data, create an instance of the AttachmentData type for every single piece of data or file you want to attach. You must provide it with the following:

  • The actual data that will be attached to the email. It should be a Data object.
  • The type of data that you're attaching; that is the mime type and a quick search on the web will give you back lists of mime types. See here for example. It should be a string value.
  • A name for the attachment you are adding. That's the file name that the recipient is going to see. It's a plain string value.

Right next you can see an example of making use of the AttachmentData. In this scenario there are two files I want to attach to the email; an image that resides in the Assets catalog, and a PDF file that exists in the application's bundle for convenience.

Once I prepare the image and PDF content as Data objects properly, I initialize two AttachmentData instances where I'm passing that data, the proper mime type as a string, and a file name of my choosing. Then I create the final EmailData instance where I include the attachments, as well as all the other necessary email info.

init() {
    let image = UIImage(named: "avatar")
    let pdfURL = Bundle.main.url(forResource: "colorsFile", withExtension: "pdf")
            
    guard let imageData = image?.jpegData(compressionQuality: 1),
          let url = pdfURL,
          let pdfData = try? Data(contentsOf: url)
    else { return }
    
    let imageAttachment = EmailData.AttachmentData(data: imageData,
                                                   mimeType: "image/jpeg",
                                                   fileName: "avatar.jpg")
    
    let pdfAttachment = EmailData.AttachmentData(data: pdfData,
                                                 mimeType: "application/pdf",
                                                 fileName: "colorsInfo.pdf")
    
    emailData = EmailData(subject: "Some files for you!",
                          recipients: ["some@recipient.xyz"],
                          body: "Hi there, I'm sending you two files to check out.",
                          isBodyHTML: false,
                          attachments: [imageAttachment, pdfAttachment])
}

After that, you're good to go and use the email data in the custom view modifier that sends the email:

Button("Send email") {
    showEmailComposer = true
}
.emailComposer(isPresented: $showEmailComposer,
               emailData: emailData) {
    // Do something when the email composer sheet is dismissed...
}

I hope this helps clear things out.

Awesome, thanks so much.