Created
May 2, 2023 14:18
-
-
Save KarthickNcog/a110f2814be3413cd68b20f992973a01 to your computer and use it in GitHub Desktop.
Build the raw message content for the forwarded email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Build the raw message content for the forwarded email | |
| let rawContent = `Content-Type: message/rfc822\n` + | |
| `Content-Disposition: attachment; filename="original.eml"\n` + | |
| `Content-Transfer-Encoding: base64\n\n` + | |
| `${res.data.raw}`; | |
| // Iterate over the original message's attachments and add them to the forwarded email | |
| res.data.payload.parts.forEach(part => { | |
| if (part.body.attachmentId) { | |
| rawContent += `\nContent-Type: ${part.mimeType}; name="${part.filename}"\n` + | |
| `Content-Disposition: attachment; filename="${part.filename}"\n` + | |
| `Content-Transfer-Encoding: base64\n\n` + | |
| `${part.body.data}`; | |
| } | |
| }); | |
| // Create a new message that includes the original message and its attachments as attachments | |
| gmail.users.messages.send({ | |
| userId: 'me', | |
| requestBody: { | |
| to: recipient, | |
| subject: subject, | |
| message: { | |
| raw: Buffer.from(rawContent).toString('base64') | |
| } | |
| } | |
| }, (err, res) => { | |
| if (err) { | |
| console.error(err); | |
| return; | |
| } | |
| console.log(`Message forwarded to ${recipient} with ID: ${res.data.id}`); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment