Advertisement
Guest User

Untitled

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