-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailMicroservice1.js
92 lines (72 loc) · 2.32 KB
/
EmailMicroservice1.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
//MicroService1:Will fail
var express = require('express');
var nodemailer = require("nodemailer");
const Handlebars = require('handlebars');
const Verifier = require("email-verifier");
const util = require("util");
const axios = require("axios");
var app = express();
let verifier = new Verifier("at_EGgoaOpT1WEkuXomX6yLR2rqhPaLK");
async function sendBulk2(req) {
let textBody = req.body.text;
let recievers = req.body.contacts
let smtpTransport = nodemailer.createTransport({
service: "gmail",
host: "smtp.gmail.com",
auth: {
user: req.body.email,//"dsosc.sih@gmail.com",
pass: req.body.pass//"dsosc1234"
}
});
let template = Handlebars.compile(textBody);
let recieved = [];
let notRecieved = [];
for (let i = 0; i < recievers.length; i++) {
try {
let res = await axios(`https://emailverification.whoisxmlapi.com/api/v1?apiKey=at_EGgoaOpT1WEkuXomX6yLR2rqhPaLK&emailAddress=${recievers[i].email}`);
recieved.push(recievers[i]);
console.log(recievers[i].name + " Done");
} catch (err) {
notRecieved.push(recievers[i]);
console.log("Error: ");
//console.log(err)
}
}
console.log('recieved', recieved);
console.log('Not recieved', notRecieved)
for (var i = 0; i < recievers.length; i++) {
let data = recievers[i];
let text = template(data);
var mailOptions = {
to: recievers[i].email,
subject: req.body.subject,
text: text,
dsn: {
id: recievers[i].id,
return: 'headers',
notify: ['failure', 'delay'],
recipient: req.body.email
},
html: {
path: 'index.html'
}
}
console.log(mailOptions);
smtpTransport.sendMail(mailOptions)
.then(data => {
// console.log(data)
}).catch(err => {
// console.log(err)
console.log("errorsmtptransport")
});
}
}
app.get('/send', async function (req, res) {
sendBulk2(req)
res.status(200).send({
message: "Processing"
});
});
app.listen(8080, function () {
console.log("Listening on Port 8080...");
});