Email Attachments without creating a file

Hi, so I want to send invoices to my clients as an email attachment. My idea was to get all the necessary data from my DB, pass the data as props to a React PDF component, and send this PDF as as an attachment to an email using nodemailer. From the nodemailer docs: https://nodemailer.com/message/attachments/ , it seems I need to specify a path to my PDF. But I dont want to create a file, store it on s3 somwhere, send an email with its path only to delete it once the email goes through. Has anyone attempted to do something like this? Any suggestions on my approach to this?
Attachments :: Nodemailer
Nodemailer is a module for Node.js to send emails
No description
2 Replies
Alejo
Alejo17mo ago
Based on the docs you linked, you could base64 encode it to send it
const attachments = [
...
{ // encoded string as an attachment
filename: 'text1.txt',
content: 'aGVsbG8gd29ybGQh',
encoding: 'base64'
},
{ // data uri as an attachment
path: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
},
...
];
const attachments = [
...
{ // encoded string as an attachment
filename: 'text1.txt',
content: 'aGVsbG8gd29ybGQh',
encoding: 'base64'
},
{ // data uri as an attachment
path: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
},
...
];
Or you can also use ReactPDF.renderToStream to attach it as a Buffer (See https://github.com/diegomura/react-pdf/issues/582)
const attachments = [
...
{ // binary buffer as an attachment
filename: 'text2.txt',
content: buffer
}
...
];
const attachments = [
...
{ // binary buffer as an attachment
filename: 'text2.txt',
content: buffer
}
...
];
George
GeorgeOP17mo ago
Thankyou so much, this is really helpful, lifesaver.
Want results from more Discord servers?
Add your server