-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
63 lines (49 loc) · 1.35 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const express = require('express')
const nodemailer = require('nodemailer');
const smtp = require('nodemailer-smtp-transport');
const app = express()
app.use('/send', async (req, res) => {
const username = process.env.SMTP_USERNAME || null
const password = process.env.SMTP_PASSWORD || null
if (!username || !password) {
throw new Error('SMTP_USERNAME and SMTP_PASSWORD must be set')
}
const transporter = nodemailer.createTransport(smtp({
service: 'gmail',
auth: {
user: username,
pass: password
}
}));
const text = `Just saying hello. \n\n- Your friend, ${username}`;
var mailOptions = {
from: username,
to: username,
// bcc: '<bcc email addres>',
subject: 'starter-nodemailer',
text
};
const sendRes = await transporter.sendMail(mailOptions);
// const errorResponse = {
// statusCode: 500,
// body: JSON.stringify({
// error: error.message,
// }),
// };
// }
// const successResponse = {
// statusCode: 200,
// body: JSON.stringify({
// message: `Email processed succesfully!`
// }),
// };
res.json(sendRes).end()
})
app.use('*', (req, res) => {
res.json({ msg: 'no route handler found' }).end()
})
// Start the server
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`index.js listening on ${port}`)
})