Skip to content

Instantly share code, notes, and snippets.

@KarthickNcog
Created May 2, 2023 14:18
Show Gist options
  • Select an option

  • Save KarthickNcog/a110f2814be3413cd68b20f992973a01 to your computer and use it in GitHub Desktop.

Select an option

Save KarthickNcog/a110f2814be3413cd68b20f992973a01 to your computer and use it in GitHub Desktop.
Build the raw message content for the forwarded email
// 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