How to Generate a Gmail Draft in Node.js With a Google Drive File Attachment Using Its FileId

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(/=+$/, '');
}

In my experience, the main challenge when trying to attach a Google Drive file to a Gmail draft is that the Gmail API expects the attachment content to be included within the MIME message itself, meaning it needs to be encoded in Base64 even if you already want to avoid extra libraries. I attempted a workaround by fetching the drive file’s content via the Drive API, but ultimately I had to include some form of data conversion. Although you may try linking to the drive file within the email body, if the goal is a true attachment using a FileId reference alone, it appears the Gmail API does not support that directly without handling the content conversion.