From dfed0454aaa9f51fe05e7e6f5a58c74dba066842 Mon Sep 17 00:00:00 2001 From: cesargamboa <35382861+cesargamboa@users.noreply.github.com> Date: Tue, 11 Dec 2018 20:20:35 -0800 Subject: [PATCH] attachments.md Added use cases for adding attachments and encode to base 64. From a local PDF file and using an URL (this example was tested using an AWS S3 bucket URL) --- use-cases/attachments.md | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/use-cases/attachments.md b/use-cases/attachments.md index dcca84863..bbf86f019 100644 --- a/use-cases/attachments.md +++ b/use-cases/attachments.md @@ -21,3 +21,60 @@ 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 + } +}); + ``` +