Advertisement
Guest User

Untitled

a guest
Apr 8th, 2017
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.03 KB | None | 0 0
  1. router.post('/sendmail', function(req, res) { // sending mail
  2.  
  3. /*
  4. *
  5. * Task : Run Server and fetch multiple emails from DB to send reminder
  6. * Invoke all the email task at once and update DB once the email is sent
  7. */
  8.  
  9. var listofemails = [];
  10. var listofempids = [];
  11. var listofempnames = [];
  12. var empname;
  13. var empid;
  14. var j = -1;
  15. var y = moment().format('YYYY');
  16. var m = moment().subtract(1, 'months').format('MMMM');
  17.  
  18. var dbs = monk('localhost:27017/payroll');
  19. var collection = dbs.get('employee');
  20. collection.find({
  21. $and: [{
  22. "month": m
  23. }, {
  24. "year": y
  25. }]
  26. }, function(e, docs) {
  27.  
  28. if (e) {
  29. console.log(e);
  30. } else {
  31.  
  32. // console.log(docs);
  33.  
  34.  
  35. }
  36. for (var i = 0; i < docs.length; i++) {
  37. listofemails.push(docs[i].emailid);
  38. listofempids.push(docs[i].empid);
  39. listofempnames.push(docs[i].employeename);
  40.  
  41. // console.log(emaillist);
  42.  
  43.  
  44. }
  45.  
  46. // Will store email sent successfully.
  47.  
  48. //var listofemails = [list];
  49.  
  50. var success_email = [];
  51. // Will store email whose sending is failed.
  52. var failure_email = [];
  53.  
  54. var transporter;
  55.  
  56. /* Loading modules done. */
  57.  
  58.  
  59. function massMailer() {
  60. var self = this;
  61. transporter = nodemailer.createTransport({
  62. host: 'smtp.gmail.com',
  63. port: 587,
  64. auth: {
  65. user: 'myhub@aditya.ac.in',
  66. pass: 'Thub@123'
  67. },
  68. tls: {
  69. rejectUnauthorized: false
  70. },
  71. debug: true
  72. });
  73. // Fetch all the emails from database and push it in listofemails
  74. self.invokeOperation();
  75. };
  76.  
  77. /* Invoking email sending operation at once */
  78.  
  79. massMailer.prototype.invokeOperation = function() {
  80. var self = this;
  81. /*async.each(listofempnames,self.SendEname,function(){
  82.  
  83. });
  84. async.each(listofempids,self.SendEmpid,function(){
  85.  
  86. });*/
  87. async.each(listofemails, self.SendEmail, function() {
  88. console.log(success_email);
  89. console.log(failure_email);
  90. });
  91.  
  92.  
  93. }
  94. /*massMailer.prototype.SendEname = function(Ename,callback) {
  95.  
  96. async.waterfall([
  97. function(callback) {
  98.  
  99. },
  100. ],function(){
  101. //When everything is done return back to caller.
  102. callback();
  103. });
  104.  
  105. }
  106. massMailer.prototype.SendEmpid = function(Empids,callback) {
  107. empid=Empids;
  108. async.waterfall([
  109. function(callback) {
  110.  
  111. },
  112. ],function(){
  113. //When everything is done return back to caller.
  114. callback();
  115. });
  116.  
  117. }*/
  118.  
  119. /*
  120. * This function will be called by multiple instance.
  121. * Each instance will contain one email ID
  122. * After successfull email operation, it will be pushed in failed or
  123. success
  124. array.
  125. */
  126.  
  127. massMailer.prototype.SendEmail = function(Email, callback) {
  128. console.log("Sending email to " + Email);
  129. var self = this;
  130. self.status = false;
  131.  
  132. var months = ["January", "February", "March", "April", "May", "June",
  133. "July", "August", "September", "October", "November", "December"
  134. ];
  135.  
  136. var monthInt = new Date().getMonth();
  137. var year = new Date().getFullYear();
  138. var monthString = months[monthInt - 1];
  139.  
  140. var filename = monthString + '-' + year + '-' + listofempids[j + 1] + '.pdf';
  141. var a = monthString + ' ' + year;
  142. // console.log(filename,a);
  143.  
  144. var msg = "<!DOCTYPE html><html><head><meta name='viewport'
  145. content='width=device-width'><meta http-equiv='Content-Type'
  146. content='text/html; charset=UTF-8'><title>Payslip Email</title><style
  147. type='text/css'>@media only screen and (max-width: 800px)
  148. {table[class=body] h1 {font-size: 28px !important;table[class=body]
  149. p,table[class=body] ul,table[class=body] ol,table[class=body]
  150. td,table[class=body] span,table[class=body] a {font-size: 16px !important;
  151. table[class=body] .wrapper,table[class=body] .article {padding: 10px
  152. !important; }table[class=body] .content {padding: 0 !important;
  153. }table[class=body] .container {padding: 0 !important;width: 100%
  154. !important; }table[class=body] .main {border-left-width: 0
  155. !important;border-radius: 0 !important;border-right-width: 0 !important;
  156. }table[class=body] .btn table {width: 100% !important; }table[class=body]
  157. .btn a {width: 100% !important; }table[class=body] .img-responsive
  158. {height: auto !important;max-width: 100% !important;width: auto
  159. !important; }}@media all {.ExternalClass {width: 100%;
  160. }.ExternalClass,.ExternalClass p,.ExternalClass span,.ExternalClass
  161. font,.ExternalClass td,.ExternalClass div {line-height: 100%; }.apple-link
  162. a {color: inherit !important;font-family: inherit !important;font-size:
  163. inherit !important;font-weight: inherit !important;line-height: inherit
  164. !important;text-decoration: none !important; }.btn-primary table td:hover
  165. {background-color: #34495e !important; }.btn-primary a:hover {background-
  166. color: #34495e !important;border-color: #34495e !important; } }.style1
  167. {font-size: 9px}</style></head><body class='' style='background-
  168. color:#f6f6f6;font-family:sans-serif;-webkit-font-
  169. smoothing:antialiased;font-size:14px;line-height:1.4;margin:0;padding:0;-
  170. ms-text-size-adjust:100%;-webkit-text-size-adjust:100%;'><table
  171. border='0' cellpadding='0' cellspacing='0' class='body' style='border-
  172. collapse:separate;mso-table-lspace:0pt;mso-table-rspace:0pt;background-
  173. color:#f6f6f6;width:100%;'><tr><td style='font-family:sans-serif;font-
  174. size:14px;vertical-align:top;'>&nbsp;</td><td class='container'
  175. style='font-family:sans-serif;font-size:14px;vertical-
  176. align:top;display:block;max-width:580px;padding:10px;width:580px;Margin:0
  177. auto !important;'><div class='content' style='box-sizing:border-
  178. box;display:block;Margin:0 auto;max-width:580px;padding:10px;'><!-- START
  179. CENTERED WHITE CONTAINER --><span class='preheader'
  180. style='color:transparent;display:none;height:0;max-height:0;max-
  181. width:0;opacity:0;overflow:hidden;mso-
  182. hide:all;visibility:hidden;width:0;'></span><table class='main'
  183. style='border-collapse:separate;mso-table-lspace:0pt;mso-table-
  184. rspace:0pt;background:#fff;border-radius:3px;width:100%;'><tr><td
  185. class='wrapper' style='font-family:sans-serif;font-size:14px;vertical-
  186. align:top;box-sizing:border-box;padding:20px;'><table border='0'
  187. cellpadding='0' cellspacing='0' style='border-collapse:separate;mso-
  188. table-lspace:0pt;mso-table-rspace:0pt;width:100%;'><tr><td style='font-
  189. family:sans-serif;font-size:14px;vertical-align:top;'><p align='right'
  190. style='font-family:sans-serif;font-size:14px;font-
  191. weight:normal;margin:0;Margin-bottom:15px;'><img
  192. src='http://ecourses.aec.edu.in/thub/images/myhub_aditya.png' width='84'
  193. height='50' alt='AdityaLogo'><p style='font-family:sans-serif;font-
  194. size:14px;font-weight:normal;margin:0;Margin-bottom:15px;'>Hi"+"
  195. "+listofempnames[j+1]+",<p style='font-family:calibri;font-
  196. color:#295890;font-size:14px;font-weight:normal;margin:0;Margin-
  197. bottom:15px;'>Please find your payslip for the month of"+"
  198. "+monthString+" "+year+" "+"attached with the mail.<p>Regards,<br><img
  199. src='http://ecourses.aec.edu.in/thub/images/myhublogo.png' width='84'
  200. height='50' alt='MyHubLogo'><br>Payroll processing system</p><br><p
  201. style='font-family:sans-serif;font-size:10px;font-
  202. weight:normal;margin:0;Margin-bottom:15px;'> Your PDF Payslip is
  203. delivered to your inbox, and you will be able to open the PDF attachment,
  204. only if you have Adobe reader version 7.0.8 and above. If Adobe Reader is
  205. not installed / upgraded in your PC. <a
  206. href='https://acrobat.adobe.com/in/en/acrobat/pdf-reader.html'>click
  207. here</a>.</p><div align='justify'><br></div><p style='font-family:sans-
  208. serif;font-size:10px;font-weight:normal;margin:0;Margin-bottom:15px;'>The
  209. information contained in this electronic message and any attachments to
  210. this message are intended for the exclusive use of the addressee(s) and
  211. may contain proprietary, confidential or privileged information. If you
  212. are not the intended recipient, you should not disseminate, distribute or
  213. copy this e-mail. Please notify the sender immediately and destroy all
  214. copies of this message and any attachments. WARNING: Computer viruses can
  215. be transmitted via email. The recipient should check this email and any
  216. attachments for the presence of viruses. The management accepts no
  217. liability for any damage caused by any virus transmitted by this email.
  218. </p><table border='0' cellpadding='0' cellspacing='0' class='btn btn-
  219. primary' style='border-collapse:separate;mso-table-lspace:0pt;mso-table-
  220. rspace:0pt;box-sizing:border-box;width:100%;'><tbody><tr><td align='left'
  221. style='font-family:sans-serif;font-size:14px;vertical-align:top;padding-
  222. bottom:15px;'><center><table border='0' cellpadding='0' cellspacing='0'
  223. style='border-collapse:separate;mso-table-lspace:0pt;mso-table-
  224. rspace:0pt;width:100%;width:auto;'><tbody><tr><td style='font-
  225. family:sans-serif;font-size:14px;vertical-align:top;background-
  226. color:#ffffff;border-radius:5px;text-align:center;background-color:;'>
  227. </td></tr></tbody></table></center></td></tr></tbody></table><p
  228. style='font-family:sans-serif;font-size:14px;font-
  229. weight:normal;margin:0;Margin-bottom:15px;'></p><p style='font-
  230. family:sans-serif;font-size:11px;font-weight:normal;margin:0;Margin-
  231. bottom:15px;text-align:center;'>Have a Good Day!.</p></td></tr></table>
  232. </td></tr></table><!-- START FOOTER --><div class='footer'
  233. style='clear:both;padding-top:10px;text-align:center;width:100%;'><table
  234. border='0' cellpadding='0' cellspacing='0' style='border-
  235. collapse:separate;mso-table-lspace:0pt;mso-table-
  236. rspace:0pt;width:100%;'><tr><td class='content-block' style='font-
  237. family:sans-serif;font-size:14px;vertical-align:top;color:#999999;font-
  238. size:12px;text-align:center;'><span class='apple-link'
  239. style='color:#999999;font-size:12px;text-align:center;'>Aditya
  240. Educational Institutions,Surampalem,533437</span><br>This email is
  241. system generated, please do not respond to this email.</td></tr><tr><td
  242. class='content-block powered-by' style='font-family:sans-serif;font-
  243. size:14px;vertical-align:top;color:#999999;font-size:12px;text-
  244. align:center;'>Powered by <a href='http://thub.ac.edu.in'
  245. style='color:#3498db;text-decoration:underline;color:#999999;font-
  246. size:12px;text-align:center;text-decoration:none;'>T-HUB team</a>.</td>
  247. </tr></table></div><!-- END FOOTER --><!-- END CENTERED WHITE CONTAINER
  248. --></div></td><td style='font-family:sans-serif;font-size:14px;vertical-
  249. align:top;'>&nbsp;</td></tr></table></body></html>";
  250. // waterfall will go one after another
  251. // So first email will be sent
  252. // Callback will jump us to next function
  253. // in that we will update DB
  254. // Once done that instance is done.
  255. // Once every instance is done final callback will be called.
  256. j++;
  257. async.waterfall([
  258. function(callback) {
  259. var mailOptions = {
  260. from: '"myHUB " <myhub@aditya.ac.in>',
  261. to: Email,
  262. subject: 'Payslip for the month of' + ' ' + ' ' + monthString + ' ' + '
  263. '+year,
  264. text: 'Payslip', // plain text body
  265. html: msg, // html body
  266.  
  267. attachments: [
  268. //{data:msg, alternative:true},
  269. {
  270. path: "C:/users/admin/downloads/" + filename,
  271. type: "application/pdf",
  272. name: filename
  273. }
  274. ]
  275.  
  276. };
  277. transporter.sendMail(mailOptions, function(error, info) {
  278. if (error) {
  279. console.log(error)
  280. failure_email.push(Email);
  281. } else {
  282. self.status = true;
  283. success_email.push(Email);
  284. }
  285. callback(null, self.status, Email);
  286. });
  287. },
  288. function(statusCode, Email, callback) {
  289. console.log("Mail sent for " + Email + "With " + statusCode);
  290.  
  291. callback();
  292. }
  293.  
  294. ], //
  295.  
  296. function(j) {
  297. //When everything is done return back to caller.
  298. if (j == 6) {
  299. transporter.close();
  300. } else {
  301. callback();
  302. }
  303. });
  304. // res.json("hai");
  305.  
  306. }
  307.  
  308. new massMailer(); //lets begin
  309. });
  310. });
  311.  
  312. transporter = nodemailer.createTransport({
  313. host: 'smtp.gmail.com',
  314. port: 565,
  315. pool : true,
  316. auth: {
  317. user: 'myhub@aditya.ac.in',
  318. pass: 'Thub@123'
  319. },
  320. tls: {
  321. rejectUnauthorized: false
  322. },
  323. debug: true
  324. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement