Guest User

Untitled

a guest
Sep 20th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. var nodemailer = require('nodemailer');
  2.  
  3. // create reusable transporter object using the default SMTP transport
  4. let transporter = nodemailer.createTransport({
  5. host: 'XXXXXX',
  6. port: 465,
  7. secure: true, // secure:true for port 465, secure:false for port 587
  8. auth: {
  9. user: 'XXXXXXX',
  10. pass: 'XXXXXXX'
  11. }
  12. });
  13.  
  14. // setup email data with unicode symbols
  15. let mailOptions = {
  16. from: '"me 👻" <aXXXXXXX.com>', // sender address
  17. to: 'XXXXXXXX.com', // list of receivers
  18. subject: 'Test Mail from ME using NodeJS', // Subject line
  19. text: 'Hello world ?', // plain text body
  20. html: '<b>Hello world ?</b>' // html body
  21. };
  22.  
  23. // send mail with defined transport object
  24. transporter.sendMail(mailOptions, (error, info) => {
  25. if (error) {
  26. return console.log(error);
  27. }
  28. console.log('Message %s sent: %s', info.messageId, info.response);
  29. });
  30.  
  31. var express=require('express');
  32. var nodemailer = require("nodemailer");
  33. var app=express();
  34. var bodyParser = require('body-parser');
  35. app.use(bodyParser.json()); // for parsing application/json
  36. app.use(bodyParser.urlencoded({ extended: true }));
  37. /*
  38. Here we are configuring our SMTP Server details.
  39. STMP is mail server which is responsible for sending and recieving email.
  40. */
  41. var smtpTransport = nodemailer.createTransport({
  42. host: 'XXXXXX',
  43. port: 465,
  44. secure: true, //secure : true for 465, secure: false for port 587
  45. auth: {
  46. user: 'XXXXXXXXXX',
  47. pass: 'XXXXXXXXXX'
  48. }
  49. });
  50. var rand,mailOptions,host,link;
  51. /*------------------SMTP Over-----------------------------*/
  52.  
  53. /*------------------Routing Started ------------------------*/
  54.  
  55. app.get('/',function(req,res){
  56. res.sendfile('index.html');
  57. });
  58.  
  59. app.get('/send',function(req,res){
  60. rand=Math.floor((Math.random() * 100) + 54);
  61. host=req.get('host');
  62. link="http://"+req.get('host')+"/verify?id="+rand;
  63. mailOptions={
  64. to : req.query.to,
  65. subject : "Please confirm your Email account",
  66. html : "Hello,<br> Please Click on the link to verify your email.<br><a href="+link+">Click here to verify</a>"
  67. }
  68. console.log(mailOptions);
  69. smtpTransport.sendMail(mailOptions, (error, response) => {
  70. if(error){
  71. console.log(error);
  72. res.end("error");
  73. }else{
  74. console.log("Message sent: " + response.message);
  75. res.end("sent");
  76. }
  77. });
  78. });
  79.  
  80. app.get('/verify',function(req,res){
  81. console.log(req.protocol+":/"+req.get('host'));
  82. if((req.protocol+"://"+req.get('host'))==("http://"+host))
  83. {
  84. console.log("Domain is matched. Information is from Authentic email");
  85. if(req.query.id==rand)
  86. {
  87. console.log("email is verified");
  88. res.end("<h1>Email "+mailOptions.to+" is been Successfully verified");
  89. }
  90. else
  91. {
  92. console.log("email is not verified");
  93. res.end("<h1>Bad Request</h1>");
  94. }
  95. }
  96. else
  97. {
  98. res.end("<h1>Request is from unknown source");
  99. }
  100. });
  101.  
  102. /*--------------------Routing Over----------------------------*/
  103.  
  104. app.listen(3000,function(){
  105. console.log("Express Started on Port 3000");
  106. });
  107.  
  108. <html>
  109. <head>
  110. <title>Node.JS Email application</title>
  111. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  112. <script>
  113. $(document).ready(function(){
  114. var from,to,subject,text;
  115. $("#send_email").click(function(){
  116. to=$("#to").val();
  117. $("#message").text("Sending E-mail...Please wait");
  118. $.get("/send",{to: to},function(data){
  119. if(data=="sent")
  120. {
  121. $("#message").empty().html("<p>Email is been sent at "+to+" . Please check inbox !</p>");
  122. }
  123.  
  124. });
  125. });
  126. });
  127. </script>
  128. <style>
  129. #container
  130. {
  131. margin-left:400px;
  132. margin-top:50px;
  133. }
  134. #to,#subject,#content
  135. {
  136. font-family: "Segoe UI";
  137. font-size:18px;
  138. width:530px;
  139. }
  140. h1
  141. {
  142. font-family: "Segoe UI";
  143. font-size:40px;
  144. color: #3b5998;
  145. }
  146. p
  147. {
  148. color:green;
  149. }
  150. #send_email
  151. {
  152. font-size:15px;
  153. font-family: "Segoe UI";
  154. }
  155. #message
  156. {
  157. font-size:18px;
  158. }
  159. </style>
  160. </head>
  161. <body>
  162. <div id="container">
  163. <h1>Email-verification System in Node.js</h1>
  164. <input type="text" id="to" placeholder="Enter E-mail which you want to verify"><br>
  165. <button id="send_email">Send Email</button><br>
  166. <span id="message"></span>
  167. </div>
  168. </body>
  169. </html>
  170.  
  171. smtpTransport.sendMail(mailOptions, function(error, response) {
  172. if (error) {
  173. console.log(error);
  174. res.end("error");
  175. } else {
  176. console.log('Message sent: ' + response.message);
  177. res.end("sent");
  178. };
  179. });
Add Comment
Please, Sign In to add comment