-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmail.js
More file actions
80 lines (69 loc) · 2.38 KB
/
mail.js
File metadata and controls
80 lines (69 loc) · 2.38 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
const fs = require('fs');
const csv = require('csv-parser');
const qrcode = require('qrcode');
const nodemailer = require('nodemailer');
const Jimp = require("jimp");
const csvFilePath = './demo.csv';
const outputDir = './qrcodes';
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
const transporter = nodemailer.createTransport({
service: 'gmail',
host: "smtp.gmail.com",
port: 587,
auth: {
user: '',
pass: ''
}
});
const rows = [];
fs.createReadStream(csvFilePath)
.pipe(csv())
.on('data', (row) => {
rows.push(row);
})
.on('end', async () => {
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const id = row.id;
const email = row.email;
const name = row.name;
const qrCodeData = id;
const qrCodePath = `${outputDir}/${id}.png`;
try {
await qrcode.toFile(qrCodePath, qrCodeData, {
color: {
dark: '#000000',
light: '#FFFFFF',
},
scale: 11,
margin: 2
});
const qrImage = await Jimp.read(qrCodePath);
const font = await Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
qrImage.print(font, 130, qrImage.bitmap.height - 20, id);
await qrImage.writeAsync(qrCodePath);
const mailOptions = {
from: 'nazimfilzer@gmail.com',
to: email,
subject: 'Welcome to TECHNOO',
text: `SUPPP ${name}!!`,
attachments: [{
filename: `${id}.png`,
path: qrCodePath
}]
};
await transporter.sendMail(mailOptions);
console.log(`Sent QR code for id ${id} to ${email}`);
if (i % 2 === 1) {
console.log('Pausing for 10 seconds...');
await new Promise(resolve => setTimeout(resolve, 10000));
console.log('Resuming...');
}
} catch (error) {
console.error(`Failed to send QR code for id ${id} to ${email}: ${error}`);
}
}
console.log('Done generating and sending QR codes');
});