-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.js
112 lines (102 loc) · 3.02 KB
/
mailer.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const nodemailer = require("nodemailer");
const color = require("cli-color");
//should be for the preview
let transporter = undefined;
const previewUrl = (info) => {
if (nodemailer.getTestMessageUrl) {
return nodemailer.getTestMessageUrl(info);
}
};
const initPreview = async mode => {
if (mode === "mailhog")
return initMailhog();
if (mode === "etheralmail") {
return initEthermail();
}
console.error (color.red("specify if you want mailhog or etheralmail for the preview"),mode);
}
const initMailhog = async () => {
transporter = nodemailer.createTransport({
// todo: put service and host in .env ...or config on proca?
// service: "gmail",
host: "localhost",
port: 1025
});
console.log(color.blue("preview them mail on http://localhost:8025 - needs to run ~/go/bin/MailHog"));
return transporter;
}
const initEthermail = async () => {
return new Promise((resolve, reject) => {
nodemailer.createTestAccount((err, account) => {
if (err) {
console.error("Failed to create a testing account. " + err.message);
reject("Failed to create a testing account. " + err.message);
}
// console.log(account, "test credentials obtained");
// Create a SMTP transporter object
transporter = nodemailer.createTransport({
host: account.smtp.host,
port: account.smtp.port,
secure: account.smtp.secure,
auth: {
user: account.user,
pass: account.pass,
},
});
resolve(transporter);
});
});
};
const init = (config) => {
transporter = nodemailer.createTransport({
host: config.host,
port: 587,
secureConnection: false,
tls: {
ciphers:'SSLv3'
},
//secure: true, // true for 465, false for other ports
auth: {
user: config.user,
pass: config.pass,
},
});
return transporter;
};
const sendDigest = async (email, subject, body, sender) => {
if (!transporter) {
console.error("sender not configured");
process.exit(1);
}
return new Promise((resolve, reject) => {
transporter.sendMail(
{
from: '"' + sender.name + '" <' + sender.email + ">",
to: email,
subject: subject,
//TO DO: add plain
html: body,
},
(err, info) => {
if (err) {
console.log("Error occurred. " + err.message);
reject("Error occurred. " + err.message);
}
return resolve(info);
}
);
});
};
const preview = async (email, subject, body, sender) => {
if (!transporter) {
await initPreview('etheralmail');
}
if (!(transporter.options.host === "smtp.ethereal.email" || transporter.options.host==="localhost")) {
//extra security
console.error(color.red("invalid preview host", transporter.options.host));
throw new Error("invalid transporter for preview");
}
const info = await sendDigest(email, subject, body, sender);
return { url: previewUrl(info), ...info };
};
module.exports = { sendDigest, init, preview, initPreview, previewUrl };