RobertEdelmann1974 / office365_test

0 stars 0 forks source link

Attachments and such #1

Open sbutlerjr opened 1 month ago

sbutlerjr commented 1 month ago

I was recently working on some change over from SMTP to oAuth and thought I'd share some example code that others might find useful, so adding it here.

In most of my code, I have the email attachments as a normal mail plugin attachment object, so I use this to convert it the payload format for graph

    function convertMailAttachmentsToOauthAttachments(){
        var oauthAttachments = []
        for(var i=0; i<attachments.length; i++){
            /** @type {com.servoy.extensions.plugins.mail.client.Attachment} */
            var attachment = attachments[i] 
            var oAuthAttachment = {
                  "@odata.type": "#microsoft.graph.fileAttachment",
                  "name": attachment.getName(),
                  "contentType": attachment.getMimeType(),
                  "contentBytes": utils.bytesToBase64(attachment.getData())
            }
            oauthAttachments.push(oAuthAttachment)
        }
        return oauthAttachments
    }

That could be used here if an attachments parameter were added, like

    if(attachments){
        sendObject.message.attachments = convertMailAttachmentsToOauthAttachments()
    }
RobertEdelmann1974 commented 1 month ago

Thanks for your input.

How large are your attachments? I found that even "moderate" attachments (we're doing documentation of defects, and the pictures from phones are quite large) seem to be too large.

I ended up creating a draft object on the server and add the attachments to that, which allows nearly unlimited sizes. The files get uploaded in chunks and when the process is complete we either send the draft or just let the user send in Outlook.

I think I could extend the example to do that, it would complete the experience

sbutlerjr commented 1 month ago

Ah, interesting. My use case is smaller docs. Usually 1-5 page PDFs and Excel docs.