# Help with email attachments

**URL:** <https://discourse.nodered.org/t/help-with-email-attachments/65533>\
**Category:** General\
**Created:** [24 July 2022 15:26 UTC](https://discourse.nodered.org/t/help-with-email-attachments/65533 "2022-07-24T15:26:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![billyballbag](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billyballbag/32/80477_2.png) [@billyballbag](https://discourse.nodered.org/u/billyballbag)\
**Post date:** [24 July 2022 15:26 UTC](https://discourse.nodered.org/t/help-with-email-attachments/65533/1 "2022-07-24T15:26:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [24 July 2022 19:23 UTC](https://discourse.nodered.org/t/help-with-email-attachments/65533/2 "2022-07-24T19:23:34Z")

</div>

> [@billyballbag](#):
>
> Could someone please point me in the right direction?

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f72d07e67b865c9a0dc3899175a5a336c5ec3e6.png)

---

<div class="post-metadata">

**Author:** ![billyballbag](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billyballbag/32/80477_2.png) [@billyballbag](https://discourse.nodered.org/u/billyballbag)\
**Post date:** [25 July 2022 18:22 UTC](https://discourse.nodered.org/t/help-with-email-attachments/65533/3 "2022-07-25T18:22:18Z")

</div>

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](mailto: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 🤣  
I'm quite new to JS....historically more of a PowerShell man, but hey we all have to learn somewhere.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [25 July 2022 20:00 UTC](https://discourse.nodered.org/t/help-with-email-attachments/65533/4 "2022-07-25T20:00:31Z")

</div>

Maybe something like this ?

```auto
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](https://docs.microsoft.com/en-us/graph/api/resources/fileattachment?view=graph-rest-1.0#properties) (if indeed i was reading the correct docs 😉 )  
contentBytes has to be base64 ?

so try also

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

```

---

<div class="post-metadata">

**Author:** ![billyballbag](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billyballbag/32/80477_2.png) [@billyballbag](https://discourse.nodered.org/u/billyballbag)\
**Post date:** [25 July 2022 21:07 UTC](https://discourse.nodered.org/t/help-with-email-attachments/65533/5 "2022-07-25T21:07:04Z")

</div>

> [@UnborN](#):
>
> `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?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [25 July 2022 21:14 UTC](https://discourse.nodered.org/t/help-with-email-attachments/65533/6 "2022-07-25T21:14:59Z")

</div>

super .. im glad it worked 👍

Tutorials (by Brad Traversy & theNetNinja)  
[JavaScript Crash Course For Beginners](https://www.youtube.com/watch?v=hdI2bqOjy3c)  
[JavaScript Higher Order Functions & Arrays](https://www.youtube.com/watch?v=rRgD1yVwIvE)  
[Modern JavaScript Tutorial](https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 August 2022 21:15 UTC](https://discourse.nodered.org/t/help-with-email-attachments/65533/7 "2022-08-08T21:15:57Z")

</div>

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