Trying to send a Gmail draft with a Google Drive file attachment via its FileId in Node.js without extra libraries or Base64 conversion. Example code:
const draftMsg = await gmailAPI.users.drafts.create({
userId: currentUser.email,
requestBody: { message: { raw: await encodeEmail(recipientAddress, emailSubject, emailBody) } }
});
export async function encodeEmail(dest, emailSubject, emailBody) {
const parts = [
'Content-Type: text/html; charset=UTF-8',
`To: ${dest}`,
`Subject: ${emailSubject}`,
'',
emailBody
].join('\n');
return Buffer.from(parts)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}