-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.js
43 lines (36 loc) · 868 Bytes
/
mailer.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
import sendgrid from '@sendgrid/mail';
import * as dotenv from 'dotenv';
dotenv.config();
sendgrid.setApiKey(process.env.SENDGRID_API_KEY)
const formatHtml = (cars) => {
let result;
cars.forEach(car=> {
result += `
<ul>
<li>Title: ${car.carTitle}</li>
<li>Price: ${car.carPrice}</li>
<li>Status: ${car.carStatus}</li>
<li>Id: ${car.carId}</li>
</ul>
<hr>
`
})
return result
}
const sendEmail = (cars) => {
const msg = {
to: process.env.EMAIL_TO,
from: process.env.EMAIL_FROM,
subject: 'New cars published in Kavak',
text: '',
html: formatHtml(cars),
}
sendgrid.send(msg)
.then(() => {
console.log('Email sent')
})
.catch((error) => {
console.error(error)
})
}
export { sendEmail };