-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail.js
79 lines (69 loc) · 2.16 KB
/
mail.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
// @ts-check
const nodemailer = require("nodemailer")
const { MAIL_USER, MAIL_PASS, MAIL_FROM } = require("./config")
const isProd = process.env.NODE_ENV === "production"
class Transporter {
async _createInstance() {
let options
if (!isProd) {
// Generate test SMTP service account from ethereal.email
const testAccount = await nodemailer.createTestAccount()
options = {
host: "smtp.ethereal.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user,
pass: testAccount.pass,
},
}
} else {
options = {
service: "SendinBlue", // no need to set host or port etc.
// host: "smtp-relay.sendinblue.com",
// port: 587,
// secure: false, // true for 465, false for other ports
auth: {
user: MAIL_USER,
pass: MAIL_PASS,
},
}
}
return nodemailer.createTransport(options)
}
async send({ to, from = MAIL_FROM, subject, text, html, templateId }) {
const self = await this._createInstance()
const info = await self.sendMail({
to,
from,
subject,
text,
html,
templateId,
})
if (!isProd) {
console.log("Message sent: %s", info.messageId)
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info))
}
}
}
/**
* Sends an email
* @param {object} options
* @param {string} options.to "Recipient email address"
* @param {string} options.subject "Email subject line"
* @param {string} [options.from] "Senders email address"
* @param {string} [options.text] "Email text contents"
* @param {string} [options.html] "Email html contents"
* @param {string} [options.templateId] "Template ID if supported"
*/
const mail = async ({ to, from, subject, text, html, templateId }) => {
if (!isProd) {
subject = `[${process.env.NODE_ENV}] ${subject}`
}
console.log("TODO: put mailer back online")
// const mailer = new Transporter()
// await mailer.send({ to, from, subject, text, html, templateId })
}
module.exports = mail