generated from othneildrew/Best-README-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Email.js
135 lines (116 loc) · 5.13 KB
/
Email.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
async function Email() {
// Your code to send email goes here
var nodeoutlook = require('./nodejs-nodemailer-outlook.js');
const fs = require('fs');
const path = require('path');
const XLSX = require('xlsx');
const truckerEmailDictionary = {
'BL': '[email protected]',
'SDL': '[email protected]',
'SLP': '[email protected]'
};
function getPdfFilesInDirectory(directory) {
const listOfFiles = fs.readdirSync(directory);
const txtFiles = listOfFiles.filter(file => path.extname(file).toLowerCase() === '.pdf');
const list_file_paths = txtFiles.map(file => path.join(directory, file));
return list_file_paths;
}
function formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `<span class="math-inline">\{day\}\-</span>{month}-${year}`;
}
let email_receiver;
let trucker;
function createHtmlTable(selected_row) {
let table = '<table>';
// add table headers
table += '<tr>';
for (let key in selected_row[0]) {
table += '<th>' + key + '</th>';
}
table += '</tr>';
// add table data
for (let row of selected_row) {
table += '<tr>';
for (let key in row) {
if (key === 'RequestedETA') {
const cellValue = row[key];
if (cellValue instanceof Date) {
table += '<td>'+formatDate(cellValue)+'</td>'; //use existing
} else {
table += '<td>'+formatDate(new Date(row[key]))+'</td>'; //parse format as new Date
}
} else if (key === 'Trucker') {
const trucker = row[key];
//trucker = row[key];
email_receiver = truckerEmailDictionary[trucker];
table += '<td>'+trucker+'</td>';
} else {
table += '<td>' + row[key] + '</td>';
}
}
table += '</tr>';
}
table += '</table>';
return table;
}
const dir_pdf = 'C:/Users/john.tan/nodejs-nodemailer-outlook/pdf/';
const list_file_paths = getPdfFilesInDirectory(dir_pdf);
//console.log(list_file_paths);
//Read the excel file
const xlsx_dir = "C:/Users/john.tan/Downloads/orders.xlsx";
const workbook = XLSX.readFile(xlsx_dir);
const sheet_name_list = workbook.SheetNames;
const sheet_name = sheet_name_list[0];
const sheet = workbook.Sheets[sheet_name];
const df = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name]);
console.log(df);
function getValueByKey(dictionary, key) {
return dictionary[key];
}
//Create a list of order_numbers from df column 'OrderNumber'
const list_order_numbers = df.map(row => row['OrderNumber']);
//For each order_number in list_order_numbers
for (const order_number of list_order_numbers) {
// Select row where column 'OrderNumber' == order_number and format as table
const selected_row = df.filter(row => row['OrderNumber'] === order_number);
const table = createHtmlTable(selected_row);
//const table = JSON.stringify(selected_row, null, 2);
const pdf_to_attach = order_number;
//const pdf_to_attach_path = list_file_paths.filter(file_path => file_path.includes(pdf_to_attach))[0];
const pdf_to_attach_path = 'https:/raw.githubusercontent.com/JohnTan38/emmyPDF/main/'+pdf_to_attach+'.pdf';
console.log(pdf_to_attach_path);
//const email_receiver = getValueByKey(truckerEmailDictionary, trucker);
if (pdf_to_attach_path) {
nodeoutlook.sendEmail({
auth: {
user: "[email protected]",
pass: "Realmadrid8983@"
},
from: '[email protected]',
to: email_receiver,
subject: 'OnshoreCustomer Order Confirmation '+pdf_to_attach,
html: '<b>Details confirmation.\n</b>'+ table,
text: 'Attached.\n'+ table,
attachments: [
//{
//filename: pdf_to_attach_path,//.split("\\").pop(),
//path: pdf_to_attach_path
//},
{ // use URL as an attachment
filename: pdf_to_attach+'.pdf',
path: 'https://raw.githubusercontent.com/JohnTan38/emmyPDF/main/'+ pdf_to_attach+'.pdf'
},
],
onError: e => console.log(e),
onSuccess: i => console.log(i)
});
} else {
console.log('PDF not found ${pdf_to_attach}');
}
}
return 'Email sent successfully!';
}
module.exports = { Email };