-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmailer.js
More file actions
58 lines (49 loc) · 1.71 KB
/
mailer.js
File metadata and controls
58 lines (49 loc) · 1.71 KB
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
'use strict';
let nodeMailer = require('nodemailer'),
ejs = require("ejs"),
transporter = undefined,
fs = require('fs'),
path = require("path");
function initTransporter(){
if(transporter === undefined){
transporter = nodeMailer.createTransport({
host: process.env.MAIL_HOST || 'localhost',
port: process.env.MAIL_PORT || 25,
secure: false,
auth: {
user: process.env.MAIL_USER || '',
pass: process.env.MAIL_PASS || ''
}
});
}
}
exports.sendVerificationMail = function(key, recipient, callback = null){
initTransporter();
const replacementVariables = {
'{{{firstname}}}': recipient.firstname,
'{{{key}}}': key
};
fs.readFile(
path.resolve(__dirname + '/../mails/entry/registerSuccess.txt'),
'utf8',
function(error, data) {
if (error) {
throw error;
}
for (const [key, value] of Object.entries(replacementVariables)) {
data = data.replace(new RegExp(key, 'g'), value)
}
const mailOptions = {
from: '"Human Connection Clock" <contact@human-connection.org>', // sender address
to: recipient.email, // list of receivers
subject: "Human Connection Clock entry successful", // Subject line
text: data,
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});
})
};