-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpaul.js
71 lines (58 loc) · 2.42 KB
/
bpaul.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
"use strict";
const CronJob = require('cron').CronJob;
const fs = require('fs');
const axios = require('axios');
require('dotenv').config()
const nodemailer = require("nodemailer");
const mailSender = async () => {
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth: {
user: process.env.USER_EMAIL,
pass: process.env.USER_PASSWORD,
}
});
const dataPrepare = require('./dataPrepare');
const mailBody = await dataPrepare.apiFetch();
console.log('Mail Body', mailBody);
if (Object.keys(mailBody).length === 0) {
return;
}
if (Object.keys(mailBody).length !== 0)
fs.writeFileSync('./output.json', JSON.stringify(mailBody), () => console.log('WRITTEN TO FILE'))
const data = fs.readFileSync('./recipients.txt', 'utf8');
if (data === '') {
console.error('NO EMAILS SPECIFIED, please specify email addresses in recipients.txt\nWith emails like, [email protected], [email protected]')
process.exit(0);
}
// send mail with defined transport object
let d = new Date()
let currentTime = d.toLocaleString('en-US', {timeZone: 'Asia/Kolkata'});
let info = await transporter.sendMail({
from: '"Suryashi IT 🖥" <[email protected]>', // sender address
to: `${data.toString()}`, // list of receivers
subject: `💉 CoWin Vaccines Available - ${currentTime}`, // Subject line
text: JSON.stringify(mailBody, null, '\t'), // plain text body
// html: "<h1>Hi!</h1>", // html body
attachments: [{
filename: 'Output.json',
path: './output.json',
contentType: 'application/json'
}]
});
console.log("Message sent: %s", info.messageId);
// Message sent: <[email protected]>
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
const minutes = 30;
const job = new CronJob(`*/${minutes} * * * *`, async () => {
console.log(`------- JOB STARTED (ITERATING IN ${minutes} MINUTE(S)) 🚀 -------\n`)
await mailSender().catch(console.error);
console.log(`\n----------- JOB DONE ✅ -----------`)
}, null, true, 'Asia/Kolkata');
job.start();