I need to compose a draft email using the GMail APIs with an attachment given a fileId from Google Drive using Google Drive APIs.
I have a working GMail API call that creates a draft email (without an attachment):
const res = await gmail.users.drafts.create({
userId: user.email,
requestBody: {
message: {
raw: await createRawMessage(to, subject, htmlBody),
},
},
});
Here is the code to my createRawMessage
function:
export async function createRawMessage(recipientEmail, subject, body) {
const message = [
`Content-Type: text/html; charset="UTF-8"`,
`to: ${recipientEmail}`,
`subject: ${subject}n`,
body,
].join("n");
return Buffer.from(message)
.toString("base64")
.replace(/+/g, "-")
.replace(///g, "_")
.replace(/=+$/, "");
}
Is there a way to just include a fileId
from Google Drive? I would prefer not downloading the file to Base64Encode the file content since it already is sitting there in Google Drive and I have the fileId
.
Also, I do not want to use another library like NodeMailer.
I have looked at how to send emails with attachments (not creating drafts) to reference that code in my draft function, but I haven’t been able to find a sample that pulls the file from Google Drive. The only examples require downloading the file from Google Drive and then incorporating it into the raw property of the requestBody.message
parameter of the gmail.users.drafts.create
API.
I have this working in a Google Apps Script, but now trying to migrate this functionality to NodeJS.
Deep Trikannad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.