-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (48 loc) · 1.68 KB
/
index.js
File metadata and controls
57 lines (48 loc) · 1.68 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
const excelToJson = require('convert-excel-to-json');
const settings = require('./settings.js')
const nodemailer = require('nodemailer');
const fs = require('fs');
const baseHTML = fs.readFileSync(`${__dirname}/${settings.messageHTML}`, {
encoding: 'utf-8'
});
let transporter = nodemailer.createTransport(settings.smtpClient);
function loadFromExcel() {
const result = excelToJson({
sourceFile: `${__dirname}/${settings.excel.inputFile}`,
header: {
rows: settings.excel.skipHeader
},
sheets: [settings.excel.sheetName]
});
return result[settings.excel.sheetName];
}
function formatMessage(text, data) {
Object.keys(data).forEach(key => {
text = text.replace(`$$${key}$$`, data[key]);
});
return text;
}
const recipients = loadFromExcel();
async function sendMails() {
for (let i = 0; i < recipients.length; i++) {
const recipient = recipients[i];
const attachmentID = 1 + i;
const to = recipient[settings.excel.emailAdressColumn];
const html = formatMessage(baseHTML, recipient);
const { fromMail, fromName, subject, attachmentName } = settings.mail;
let info = await transporter.sendMail({
from: `"${fromName}" <${fromMail}>`,
to,
subject,
html,
attachments: [
{ // file on disk as an attachment
filename: attachmentName,
path: settings.attachmentPath(attachmentID)
},
]
});
console.log(`${attachmentID}/${recipients.length}: sent ${settings.attachmentPath(attachmentID)} to ${to}`);
}
}
sendMails();