-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-email-send.js
More file actions
292 lines (273 loc) · 8.06 KB
/
test-email-send.js
File metadata and controls
292 lines (273 loc) · 8.06 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* Test Email Script
*
* This script tests the email system by sending actual emails.
* It simulates DEV/VPN mode to test the intended recipients banner.
*
* Note: NBI mail server only relays to internal domains (@nbi.ac.uk, @tsl.ac.uk)
* Cannot send to external domains like @gmail.com
*
* Usage: node test-email-send.js
*/
const nodemailer = require("nodemailer");
const Email = require("email-templates");
const path = require("path");
const config = require("./config.json");
// Test configuration
const TEST_EMAIL = config.email.testEmail || "deeks@nbi.ac.uk";
// Simulate what production mode would send to
const SIMULATED_RECIPIENTS = {
to: "customer@nbi.ac.uk, admin@nbi.ac.uk, mark.youles@tsl.ac.uk",
cc: "budgetholder@nbi.ac.uk",
};
const templatesDir = path.resolve(__dirname, "views", "email");
const transporter = nodemailer.createTransport({
host: config.email.host,
port: config.email.port,
secure: false,
tls: {
rejectUnauthorized: false,
},
});
console.log("=".repeat(60));
console.log("Email Test Script - DEV/VPN Mode Simulation");
console.log("=".repeat(60));
console.log("SMTP Host:", config.email.host);
console.log("SMTP Port:", config.email.port);
console.log("From:", config.email.from);
console.log("Actual Recipient:", TEST_EMAIL);
console.log("Simulated To:", SIMULATED_RECIPIENTS.to);
console.log("Simulated CC:", SIMULATED_RECIPIENTS.cc);
console.log("=".repeat(60));
// Verify SMTP connection first
console.log("\nVerifying SMTP connection...");
transporter.verify((error, success) => {
if (error) {
console.error("❌ SMTP connection failed:", error.message);
console.error("\nMake sure you are connected to the VPN if required.");
process.exit(1);
}
console.log("✅ SMTP connection successful!\n");
// Create email instance with preview disabled
const email = new Email({
message: {
from: config.email.from,
},
transport: transporter,
views: {
root: templatesDir,
options: {
extension: "ejs",
},
},
juice: true,
juiceResources: {
preserveImportant: true,
webResources: {
relativeTo: templatesDir,
},
},
// Disable preview in browser
preview: false,
send: true,
});
// Comprehensive mock order data for testing
const mockOrder = {
id: "test-order-" + Date.now(),
janCode: "JAN-2024-TEST-001",
username: "testcustomer",
signatory: "budgetholder",
totalQuantity: 5,
totalCost: 25.0,
pricePerUnit: 5,
costCode: "NBI-RESEARCH-2024-001",
createdAt: new Date(),
complete: false,
user: {
username: "testcustomer",
name: "Test Customer Name",
mail: "testcustomer@nbi.ac.uk",
company: "TSL",
},
items: [
{
id: "item-001",
quantity: 2,
type: {
id: "type-001",
name: "pICH47742 - Level 0 Acceptor",
concentration: "100 ng/µL",
synBioID: "SB-0001",
description: "Golden Gate Level 0 acceptor plasmid",
},
},
{
id: "item-002",
quantity: 1,
type: {
id: "type-002",
name: "pICH47751 - Level 1 Acceptor Position 1",
concentration: "75 ng/µL",
synBioID: "SB-0042",
description: "Golden Gate Level 1 acceptor for position 1",
},
},
{
id: "item-003",
quantity: 2,
type: {
id: "type-003",
name: "pAGM4723 - Level 2 Acceptor",
concentration: "50 ng/µL",
synBioID: "SB-0156",
description: "Golden Gate Level 2 acceptor plasmid",
},
},
],
};
const mockUser = {
username: "testcustomer",
name: "Test Customer Name",
mail: "testcustomer@nbi.ac.uk",
company: "TSL",
};
// DEV mode data to simulate what the email service adds
const devModeData = {
_devMode: true,
_intendedRecipients: SIMULATED_RECIPIENTS,
};
// VPN mode data
const vpnModeData = {
_vpnMode: true,
_intendedRecipients: SIMULATED_RECIPIENTS,
};
// Test 1: New Order Email (DEV mode)
console.log("1. Sending test email: new-order (DEV mode - blue banner)...");
email
.send({
template: "new-order",
message: {
to: TEST_EMAIL,
subject: "[DEV] SynBio - New Order",
priority: "high",
},
locals: {
order: mockOrder,
user: mockUser,
baseURL: config.baseURL,
...devModeData,
},
})
.then(() => {
console.log(" ✅ new-order (DEV) sent to", TEST_EMAIL);
// Test 2: Order Ready Email (DEV mode)
console.log(
"\n2. Sending test email: order-ready (DEV mode - blue banner)...",
);
return email.send({
template: "order-ready",
message: {
to: TEST_EMAIL,
subject: "[DEV] Your SynBio order is ready for collection",
priority: "high",
},
locals: {
order: mockOrder,
baseURL: config.baseURL,
...devModeData,
},
});
})
.then(() => {
console.log(" ✅ order-ready (DEV) sent to", TEST_EMAIL);
// Test 3: New Order Email (VPN mode)
console.log(
"\n3. Sending test email: new-order (VPN mode - yellow banner)...",
);
return email.send({
template: "new-order",
message: {
to: TEST_EMAIL,
subject: "[VPN TEST] SynBio - New Order",
priority: "high",
},
locals: {
order: mockOrder,
user: mockUser,
baseURL: config.baseURL,
...vpnModeData,
},
});
})
.then(() => {
console.log(" ✅ new-order (VPN) sent to", TEST_EMAIL);
// Test 4: Order Ready Email (VPN mode)
console.log(
"\n4. Sending test email: order-ready (VPN mode - yellow banner)...",
);
return email.send({
template: "order-ready",
message: {
to: TEST_EMAIL,
subject: "[VPN TEST] Your SynBio order is ready for collection",
priority: "high",
},
locals: {
order: mockOrder,
baseURL: config.baseURL,
...vpnModeData,
},
});
})
.then(() => {
console.log(" ✅ order-ready (VPN) sent to", TEST_EMAIL);
// Test 5: New Order Email (Production mode - no banner)
console.log(
"\n5. Sending test email: new-order (PROD mode - no banner)...",
);
return email.send({
template: "new-order",
message: {
to: TEST_EMAIL,
subject: "[PROD TEST] SynBio - New Order",
priority: "high",
},
locals: {
order: mockOrder,
user: mockUser,
baseURL: config.baseURL,
// No _devMode or _vpnMode flag - simulates production
},
});
})
.then(() => {
console.log(" ✅ new-order (PROD) sent to", TEST_EMAIL);
console.log("\n" + "=".repeat(60));
console.log("All test emails sent successfully!");
console.log("=".repeat(60));
console.log("\nCheck your inbox at:", TEST_EMAIL);
console.log("\nYou should receive 5 emails:");
console.log(" 1. [DEV] New Order - BLUE banner with recipients");
console.log(" 2. [DEV] Order Ready - BLUE banner with recipients");
console.log(
" 3. [VPN TEST] New Order - YELLOW banner with recipients",
);
console.log(
" 4. [VPN TEST] Order Ready - YELLOW banner with recipients",
);
console.log(" 5. [PROD TEST] New Order - NO banner (production style)");
console.log("\nAll emails should show:");
console.log(" - Order: JAN-2024-TEST-001");
console.log(" - Customer: Test Customer Name (testcustomer)");
console.log(" - 3 items totaling 5 units");
console.log(" - Cost code: NBI-RESEARCH-2024-001");
console.log(" - Budget holder: budgetholder");
console.log("=".repeat(60));
process.exit(0);
})
.catch((err) => {
console.error("\n❌ Failed to send email:", err.message);
console.error(err);
process.exit(1);
});
});