-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello-background.js
48 lines (40 loc) · 1.1 KB
/
hello-background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const { jsPDF } = require('jspdf')
const nodemailer = require('nodemailer')
const sgTransport = require('nodemailer-sendgrid-transport')
const util = require('util')
const options = {
auth: {
api_key: process.env.SENDGRID_API_TOKEN
}
}
const client = nodemailer.createTransport(sgTransport(options))
exports.handler = async function(event) {
const { content, destination } = JSON.parse(event.body)
const attachment = Buffer.from(
new jsPDF().text(content, 10, 10).output('arraybuffer')
)
const email = {
from: '[email protected]',
to: '[email protected]',
subject: 'Hello',
text: 'Hello world',
html: '<b>Hello world</b>',
attachments: [
{
content: attachment,
filename: 'attachment.pdf',
type: 'application/pdf',
disposition: 'attachment'
}
]
}
const info = await client.sendMail(email, (err, info) => {
if (err) {
console.log(err)
} else {
console.log('Message sent')
}
})
console.log(util.inspect(info, false, null, true /* enable colors */))
console.log(`PDF report sent: ${info}`)
}