Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var nodemailer = require('nodemailer');
- // create reusable transporter object using the default SMTP transport
- let transporter = nodemailer.createTransport({
- host: 'XXXXXX',
- port: 465,
- secure: true, // secure:true for port 465, secure:false for port 587
- auth: {
- user: 'XXXXXXX',
- pass: 'XXXXXXX'
- }
- });
- // setup email data with unicode symbols
- let mailOptions = {
- from: '"me 👻" <aXXXXXXX.com>', // sender address
- to: 'XXXXXXXX.com', // list of receivers
- subject: 'Test Mail from ME using NodeJS', // Subject line
- text: 'Hello world ?', // plain text body
- html: '<b>Hello world ?</b>' // html body
- };
- // send mail with defined transport object
- transporter.sendMail(mailOptions, (error, info) => {
- if (error) {
- return console.log(error);
- }
- console.log('Message %s sent: %s', info.messageId, info.response);
- });
- var express=require('express');
- var nodemailer = require("nodemailer");
- var app=express();
- var bodyParser = require('body-parser');
- app.use(bodyParser.json()); // for parsing application/json
- app.use(bodyParser.urlencoded({ extended: true }));
- /*
- Here we are configuring our SMTP Server details.
- STMP is mail server which is responsible for sending and recieving email.
- */
- var smtpTransport = nodemailer.createTransport({
- host: 'XXXXXX',
- port: 465,
- secure: true, //secure : true for 465, secure: false for port 587
- auth: {
- user: 'XXXXXXXXXX',
- pass: 'XXXXXXXXXX'
- }
- });
- var rand,mailOptions,host,link;
- /*------------------SMTP Over-----------------------------*/
- /*------------------Routing Started ------------------------*/
- app.get('/',function(req,res){
- res.sendfile('index.html');
- });
- app.get('/send',function(req,res){
- rand=Math.floor((Math.random() * 100) + 54);
- host=req.get('host');
- link="http://"+req.get('host')+"/verify?id="+rand;
- mailOptions={
- to : req.query.to,
- subject : "Please confirm your Email account",
- html : "Hello,<br> Please Click on the link to verify your email.<br><a href="+link+">Click here to verify</a>"
- }
- console.log(mailOptions);
- smtpTransport.sendMail(mailOptions, (error, response) => {
- if(error){
- console.log(error);
- res.end("error");
- }else{
- console.log("Message sent: " + response.message);
- res.end("sent");
- }
- });
- });
- app.get('/verify',function(req,res){
- console.log(req.protocol+":/"+req.get('host'));
- if((req.protocol+"://"+req.get('host'))==("http://"+host))
- {
- console.log("Domain is matched. Information is from Authentic email");
- if(req.query.id==rand)
- {
- console.log("email is verified");
- res.end("<h1>Email "+mailOptions.to+" is been Successfully verified");
- }
- else
- {
- console.log("email is not verified");
- res.end("<h1>Bad Request</h1>");
- }
- }
- else
- {
- res.end("<h1>Request is from unknown source");
- }
- });
- /*--------------------Routing Over----------------------------*/
- app.listen(3000,function(){
- console.log("Express Started on Port 3000");
- });
- <html>
- <head>
- <title>Node.JS Email application</title>
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
- <script>
- $(document).ready(function(){
- var from,to,subject,text;
- $("#send_email").click(function(){
- to=$("#to").val();
- $("#message").text("Sending E-mail...Please wait");
- $.get("/send",{to: to},function(data){
- if(data=="sent")
- {
- $("#message").empty().html("<p>Email is been sent at "+to+" . Please check inbox !</p>");
- }
- });
- });
- });
- </script>
- <style>
- #container
- {
- margin-left:400px;
- margin-top:50px;
- }
- #to,#subject,#content
- {
- font-family: "Segoe UI";
- font-size:18px;
- width:530px;
- }
- h1
- {
- font-family: "Segoe UI";
- font-size:40px;
- color: #3b5998;
- }
- p
- {
- color:green;
- }
- #send_email
- {
- font-size:15px;
- font-family: "Segoe UI";
- }
- #message
- {
- font-size:18px;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <h1>Email-verification System in Node.js</h1>
- <input type="text" id="to" placeholder="Enter E-mail which you want to verify"><br>
- <button id="send_email">Send Email</button><br>
- <span id="message"></span>
- </div>
- </body>
- </html>
- smtpTransport.sendMail(mailOptions, function(error, response) {
- if (error) {
- console.log(error);
- res.end("error");
- } else {
- console.log('Message sent: ' + response.message);
- res.end("sent");
- };
- });
Add Comment
Please, Sign In to add comment