diff --git a/docs/use-cases/attachments.md b/docs/use-cases/attachments.md index dcca84863..223c63343 100644 --- a/docs/use-cases/attachments.md +++ b/docs/use-cases/attachments.md @@ -21,3 +21,62 @@ const msg = { ], }; ``` + +Reading and converting a local PDF file. + +```js +import fs from 'fs'; + +fs.readFile(('Document.pdf'), (err, data) => { + if (err) { + // do something with the error + } + if (data) { + const msg = { + to: 'recipient@test.org', + from: 'sender@test.org', + subject: 'Attachment', + html: '

Here’s an attachment for you!

', + attachments: [ + { + content: data.toString('base64'), + filename: 'some-attachment.pdf', + type: 'application/pdf', + disposition: 'attachment', + contentId: 'mytext', + }, + ], + }; + } +}); +``` + +If you are using a PDF URL: + +```js +import request from 'request'; + +request(fileURl, { encoding: null }, (err, res, body) => { + if (err) { return err; } + if (body) { + const textBuffered = Buffer.from(body); + + const msg = { + to: 'recipient@test.org', + from: 'sender@test.org', + subject: 'Attachment', + html: '

Here’s an attachment for you!

', + attachments: [ + { + content: textBuffered.toString('base64'), + filename: 'some-attachment.pdf', + type: 'application/pdf', + disposition: 'attachment', + contentId: 'mytext', + }, + ], + }; + // send msg here + } +}); +```