2017-06-22 26 views
0

這是我的示例代碼來實現電子郵件驗證。作爲server.js嘗試使用的NodeJS

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); 
}); 

使用上面的例子,我準備用網頁來實現工作正常..命名。

這是一個修改..命名爲server1.js

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"); 
}); 

現在的index.html

<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> 

當我輸入電子郵件,並點擊發送,它顯示在網頁和成功消息以及在控制檯以及

但是,我沒有收到任何郵件..哪裏的server.js的實施工作就好..

我不知道我在想什麼,請幫我解決它。

回答

0

使用nodemailer發送的電子郵件可能被電子郵件公司阻止,例如'GMAIL',因爲它無法確定該郵件發送的郵件的安全性。

我遇到了與nodemailer類似的問題,我不得不讓一個Gmail用戶允許來自不受信任方的電子郵件。

+0

在server.js中,gmail也正常工作,因爲我改變了選項允許安全的應用程序..此外,我沒有使用Gmail郵寄的目的,有一些私人託管服務.. ..這也適用於server.js –

0

只是

smtpTransport.sendMail(mailOptions, function(error, response) { 
    if (error) { 
     console.log(error); 
     res.end("error"); 
    } else { 
     console.log('Message sent: ' + response.message); 
     res.end("sent"); 
    }; 
}); 

這應該是作品改變smtpTransport功能。

+0

lemme嘗試和儘快給您回覆 –