Help with email attachments

Hi all,

I’m looking for some help with a flow I’m working on, it essentially takes emails in via SMTP and then pushes them to an API, I’ve got it working with the text payload of the message but now need to figure out how to include any possible email attachments.

Could someone please point me in the right direction?
Thanks

The email node's help tab gives you all the information you need.
Can you be more specific on what issue you are having ?

From the node's help tab :

Any attachments supplied in the incoming email can be found in the msg.attachments property. This will be an array of objects where each object represents a specific attachments.

As you noticed the body of the email is in msg.payload
any attachments should be in msg.attachments

add a Debug node after your email node and set it to show Complete msg object
to see the full msg structure.

image

So I've done some more research, and I need to include the attachments within my payload being posted to the graph API.

I believe I'll need to construct the payload into a format to go into the payload itself using a ForEach loop.

Here is an example of the Graph API being used with SendMail via Exchange Online:

{
"message": {
"subject": "Meet for lunch?",
"body": {
"contentType": "Text",
"content": "The new cafeteria is open."
},
"toRecipients": [
{
"emailAddress": {
"address": "meganb@contoso.onmicrosoft.com"
}
}
],
"attachments": [
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "attachment.txt",
"contentType": "text/plain",
"contentBytes": "SGVsbG8gV29ybGQh"
}
]
}
}

As you can see the attachments portion at the bottom, I need to somehow perform a foreach and store it in a variable and place it within attachments =

I've had a rough go at it and have come out with something like this:

var attachments = ;
function addAttachments() {
msg.attachments.forEach(attachments => {
attachments.push({
'@odata.type': "#microsoft.graph.fileAttachment",
name: msg.attachments.filename,
contentType: msg.attachments.contentType,
contentBytes: msg.attachments.content,
});

})}

I'm sure you'll tell me I'm way off, but any help is greatly appreciated :rofl:
I'm quite new to JS....historically more of a PowerShell man, but hey we all have to learn somewhere.

Maybe something like this ?

let graphMsg = {}

graphMsg.payload = {
    "message": {
        "subject": msg.header.subject,
        "body": {
            "contentType": "Text",
            "content": msg.payload
        },
        "toRecipients": [
            {
                "emailAddress": {
                    "address": "xxx@xxx.onmicrosoft.com"  // final destination ??
                }
            }
        ],
        "attachments": Array.isArray(msg.attachments) ? msg.attachments.map(att => {
            return {
                "@odata.type": "#microsoft.graph.fileAttachment",
                "name": att.filename,
                "contentType": att.contentType,
                "contentBytes": att.content
            }
        }) : []
    }
};



return graphMsg;

EDIT: from what i read in the docs (if indeed i was reading the correct docs :wink: )
contentBytes has to be base64 ?

so try also

"contentType": att.contentType,
"contentBytes": Buffer.from(att.content).toString('base64')

Thank you so much! this is working now, I think I should invest in a Udemy course on Javascript, any recommendations for learning material?

super .. im glad it worked :+1:

Tutorials (by Brad Traversy & theNetNinja)
JavaScript Crash Course For Beginners
JavaScript Higher Order Functions & Arrays
Modern JavaScript Tutorial

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.