-
Notifications
You must be signed in to change notification settings - Fork 0
/
emailHandler.js
53 lines (43 loc) · 1.5 KB
/
emailHandler.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
'use strict';
const nodemailer = require('nodemailer');
const { MAIL_SERVICE, MAIL_USER, MAIL_PASS, MAIL_PORT, MAIL_HOST } = process.env;
const transport = nodemailer.createTransport({
service: MAIL_SERVICE,
host: MAIL_HOST,
port: MAIL_PORT,
auth: {
user: MAIL_USER,
pass: MAIL_PASS,
},
});
module.exports.sendEmail = async (event) => {
if (event.httpMethod === "POST" && event.body) {
let json = JSON.parse(event.body)
const { toEmail, subject, content } = json
var mailOptions = {
from: "[email protected]",
to: toEmail,
subject: subject,
html: content,
};
let info = await transport.sendMail(mailOptions);
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Email sent: ' + info.response,
data: {
messageId: info.messageId,
previewURL: nodemailer.getTestMessageUrl(info)
},
}, null, 2),
headers: {
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET"
}
};
}
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};