-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (65 loc) · 2.08 KB
/
app.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
require('dotenv').config();
const csv = require('csv-parser');
const fs = require('fs');
const nodemailer = require('nodemailer');
const results = [];
const headers = ['name', 'label', 'created_at', 'email', 'has_website', 'website_url', 'district', 'phone_number'];
const sentEmailsFile = 'sent_emails.json';
let sentEmails = [];
if (fs.existsSync(sentEmailsFile)) {
sentEmails = JSON.parse(fs.readFileSync(sentEmailsFile, { encoding: 'utf8' }));
}
let emailTemplate;
let emailObject;
let transporter = nodemailer.createTransport({
service: process.env.TRANSPORTER_SERVICE,
auth: {
user: process.env.TRANSPORTER_MAIL,
pass: process.env.TRANSPORTER_PASS,
},
});
let noWebsite = JSON.parse(fs.readFileSync('templates/noWebsite.json', { encoding: 'utf8' }));
let hasWebsite = JSON.parse(fs.readFileSync('templates/hasWebsite.json', { encoding: 'utf8' }));
const sendMail = ({ name, email, has_website }) => {
const sentEmail = sentEmails.find((e) => e.email === email);
if (sentEmail) {
console.log(`Email already sent to ${email}, skipping...`);
return Promise.resolve();
}
const { object, msg } = has_website === 'Non' ? noWebsite : hasWebsite;
const emailTemplate = msg.replace('{ENTERPRISE_NAME}', name);
const mailOptions = {
from: 'Andrew Mathieu',
to: email,
subject: object,
text: emailTemplate,
};
return transporter.sendMail(mailOptions).then(() => {
const sentDate = new Date().toISOString();
sentEmails.push({ email, sentDate });
fs.writeFileSync(sentEmailsFile, JSON.stringify(sentEmails, null, 2));
console.log(`Email sent to ${email}`);
});
};
// Lecture du fichier CSV
fs.createReadStream('data.csv')
.pipe(csv(headers))
.on('data', (data) => {
if (!data.phone_number) {
delete data.phone_number;
}
if (!data.website_url) {
delete data.website_url;
}
results.push(data);
})
.on('end', () => {
const promises = results.map((prospect) => sendMail(prospect));
Promise.all(promises)
.then((results) => {
console.log('All emails sent successfully');
})
.catch((error) => {
console.error('Error sending emails:', error);
});
});