forked from PTFS-Europe/ems-email
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
42 lines (37 loc) · 1.2 KB
/
lib.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
const Email = require('email-templates');
const nodemailer = require('nodemailer');
const db = require('../ems-db');
const email = new Email();
const smtpOptions = {
send: true,
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT
};
const transporter = nodemailer.createTransport(smtpOptions);
const sendEmail = async ({to, name, queries, type}) => {
try {
let content = await email.renderAll(`${process.env.SCHEMA}/${type}`, {
name,
queries
});
content = {
...content,
from: process.env.FROM,
replyTo: process.env.REPLY_TO,
to
}
const result = transporter.sendMail(content).then(async () => {
// Only update the high mark if the mail send succeeded
await updateMostRecent(queries);
});
return result;
} catch(err) {
console.log(`Email sending failed with ${err}`);
return Promise.reject(err);
}
};
const updateMostRecent = async (queries) => {
console.log(`Updating most_recent_digest for ${queries.length} queries`);
return await db.resolvers.queryuser.updateMostRecentDigests(queries);
};
module.exports = { sendEmail };