-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
49 lines (41 loc) · 1.37 KB
/
server.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
49
require("dotenv").config()
const express = require("express")
const path = require("path")
const sgMail = require("@sendgrid/mail")
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
const app = express()
if (
!process.env.TEST_EMAIL_TO_ADDRESS ||
!process.env.TEST_EMAIL_FROM_ADDRESS ||
!process.env.SENDGRID_API_KEY ||
!process.env.TEST_ENDPOINT
) {
console.log(
"Make sure to set the following environment variables for your application:\n‣ TEST_EMAIL_TO_ADDRESS\n‣ TEST_EMAIL_FROM_ADDRESS\n‣ SENDGRID_API_KEY\n‣ TEST_ENDPOINT\n",
)
process.exit()
}
app.get("/", async (req, res) => {
res.sendFile(path.join(__dirname, "/index.html"))
})
app.get(`/${process.env.TEST_ENDPOINT}`, async (req, res) => {
const msg = {
to: process.env.TEST_EMAIL_TO_ADDRESS,
from: process.env.TEST_EMAIL_FROM_ADDRESS,
subject: "My Test Email",
text: "This is a test email sent by your Node.js application when you visited the test endpoint",
html: "This is a <strong>test</strong> email sent by your Node.js application when you visited the test endpoint",
}
try {
await sgMail.send(msg)
} catch (error) {
console.error(error)
if (error.response) {
console.error(error.response.body)
}
}
res.send("Email sent")
})
app.listen(process.env.PORT, () => {
console.log(`Hello World Application is running on port ${process.env.PORT}`)
})