|
1 | 1 | const { jsPDF } = require('jspdf')
|
2 | 2 | const nodemailer = require('nodemailer')
|
3 | 3 | const sgTransport = require('nodemailer-sendgrid-transport')
|
| 4 | +const util = require('util') |
4 | 5 |
|
5 | 6 | const options = {
|
6 | 7 | auth: {
|
7 |
| - |
8 |
| - api_key: process.env.SENDGRID_API_KEY |
| 8 | + api_key: process.env.SENDGRID_API_TOKEN |
9 | 9 | }
|
10 | 10 | }
|
11 | 11 |
|
12 | 12 | const client = nodemailer.createTransport(sgTransport(options))
|
13 | 13 |
|
14 | 14 | exports.handler = async function(event) {
|
15 |
| - console.log(event) |
16 |
| - |
17 | 15 | const { content, destination } = JSON.parse(event.body)
|
18 |
| - console.log(`Sending PDF report to ${destination}`) |
19 | 16 |
|
20 |
| - const report = Buffer.from( |
| 17 | + const attachment = Buffer.from( |
21 | 18 | new jsPDF().text(content, 10, 10).output('arraybuffer')
|
22 | 19 | )
|
23 |
| - const info = await client.sendMail({ |
| 20 | + |
| 21 | + const email = { |
24 | 22 |
|
25 |
| - to: destination, |
26 |
| - subject: 'Your report is ready!', |
27 |
| - text: 'See attached report PDF', |
| 23 | + |
| 24 | + subject: 'Hello', |
| 25 | + text: 'Hello world', |
| 26 | + html: '<b>Hello world</b>', |
28 | 27 | attachments: [
|
29 | 28 | {
|
30 |
| - filename: `report-${new Date().toDateString()}.pdf`, |
31 |
| - content: report, |
32 |
| - contentType: 'application/pdf' |
| 29 | + content: attachment, |
| 30 | + filename: 'attachment.pdf', |
| 31 | + type: 'application/pdf', |
| 32 | + disposition: 'attachment' |
33 | 33 | }
|
34 | 34 | ]
|
| 35 | + } |
| 36 | + |
| 37 | + const info = await client.sendMail(email, (err, info) => { |
| 38 | + if (err) { |
| 39 | + console.log(err) |
| 40 | + } else { |
| 41 | + console.log('Message sent') |
| 42 | + } |
35 | 43 | })
|
36 | 44 |
|
37 |
| - console.log(`PDF report sent: ${info.messageId}`) |
| 45 | + console.log(util.inspect(info, false, null, true /* enable colors */)) |
| 46 | + |
| 47 | + console.log(`PDF report sent: ${info}`) |
38 | 48 | }
|
0 commit comments