-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathemail-nodemailer.js
60 lines (52 loc) · 1.89 KB
/
email-nodemailer.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
/*
--------------------------------------------------------------------------------
- lib/email-nodemailer.js
--------------------------------------------------------------------------------
*/
const nodemailer = require('nodemailer');
/* createTransport() create reusable transporter object using the default
SMTP transport
*/
function createTransport() {
const environmentVariable = [
'NODEMAILER_HOST', // e.g. smtp.gmail.com
'NODEMAILER_PORT', // e.g. 465
'NODEMAILER_EMAIL', // e.g. [email protected]
'NODEMAILER_EMAIL_PW',
];
for (let i = 0; i < environmentVariable.length; i += 1) {
const ev = process.env[environmentVariable[i]];
if (!ev) {
return {
sendMail: () => {
throw new Error(
`Missing environment variable ${environmentVariable[i]}!`,
);
},
};
}
}
return nodemailer.createTransport({
host: process.env.NODEMAILER_HOST, // e.g. 'smtp.ethereal.email'
port: process.env.NODEMAILER_PORT, // e.g. 465
secure: true, // true for 465, false for other ports
auth: {
user: process.env.NODEMAILER_EMAIL,
pass: process.env.NODEMAILER_EMAIL_PW,
},
});
}
async function sendEmail(message) {
if (process.env.SUPPRESS_EMAIL) return;
const transport = createTransport();
const params = {
from: process.env.NODEMAILER_EMAIL, // sender address
to: message.toAddress, // list of receivers e.g. '[email protected], [email protected]'
subject: message.subject,
// text: 'Hello world?', // plain text body
html: message.body, // html body
};
transport.sendMail(params);
}
module.exports = { sendEmail };
/* * * * */