Skip to content

Commit bf8072e

Browse files
committed
feat: finish bg fn example
1 parent 4876dcc commit bf8072e

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

js/functions/hello-background.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
const { jsPDF } = require('jspdf')
22
const nodemailer = require('nodemailer')
33
const sgTransport = require('nodemailer-sendgrid-transport')
4+
const util = require('util')
45

56
const options = {
67
auth: {
7-
api_user: '[email protected]',
8-
api_key: process.env.SENDGRID_API_KEY
8+
api_key: process.env.SENDGRID_API_TOKEN
99
}
1010
}
1111

1212
const client = nodemailer.createTransport(sgTransport(options))
1313

1414
exports.handler = async function(event) {
15-
console.log(event)
16-
1715
const { content, destination } = JSON.parse(event.body)
18-
console.log(`Sending PDF report to ${destination}`)
1916

20-
const report = Buffer.from(
17+
const attachment = Buffer.from(
2118
new jsPDF().text(content, 10, 10).output('arraybuffer')
2219
)
23-
const info = await client.sendMail({
20+
21+
const email = {
2422
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>',
2827
attachments: [
2928
{
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'
3333
}
3434
]
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+
}
3543
})
3644

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}`)
3848
}

js/src/views/Home.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export default {
1414
methods: {
1515
fetchBackgroundFunction() {
1616
axios
17-
.post('/.netlify/functions/hello-background', { my: 'payload' })
17+
.post('/.netlify/functions/hello-background', {
18+
content: 'Hello from Netlify!',
19+
destination: '[email protected]'
20+
})
1821
.then(response => {
1922
console.log(response)
2023
})

0 commit comments

Comments
 (0)