I am using the SendMailer’s API to automatically send a user an email with an attachment after payment through stripe, but am struggling to upload the file because of Glitch’s asset URL. Currently, have it working and sending a regular email without the attachment. Any advice on executing the first two lines would be a huge help!
pathToAttachment = ${__dirname}/attachment.pdf;
attachment = fs.readFileSync(pathToAttachment).toString(“base64”);
const msg = {
to: '[email protected]',
from: '[email protected]',
subject: 'Sending with SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
attachments: [
{
content: attachment,
filename: "attachment.pdf",
type: "application/pdf",
disposition: "attachment"
}
]
};
filename.pdf is a filename at your local storage, It used when your project has “that” file. Unfortunately, Every file that you uploaded to glitch, It will converted as RAW URL. Fortunately, You can request the URL and do this:
const { get } = require("https");
get("https://cdn.glitch.com/hsishdhsishdhwuahdhsj", response => {
var data = [];
response.on("data", chunk => data.push(chunk));
response.on('end', () => {
// Concat the arrayBuffer
var buff = Buffer.concat(data);
// Make it as base64
var base64 = buff.toString("base64");
// Do something....
});
});
Hop! If you feels this code is too long for you, You can use node-fetch
const fetch = require("node-fetch");
fetch("https://cdn.glitch.com/abdisbaishfidjdhd").then(res => res.buffer()).then(BUFF => {
// Make it as base64
var base64 = BUFF.toString("base64");
// Do something....
});
Just to be clear of what @Yonle said, the Assets folder is a virtual folder that does not exist locally in a project. Files uploaded gets uploaded to Glitch’s CDN (cdn.glitch.com/*).