How to setup SMTP server on nodejs

匿名 (未验证) 提交于 2019-12-03 00:44:02

问题:

I've been trying to setup a simple form that will send an email to the user upon submission. I've been working with nodejs and have installed nodemailer to help aid me. I'm using gmail as the SMTP server, and was encountering some issues. Here is the html:

<title> Schedule an Appointment - Name </title> <link rel="stylesheet" type="text/css" href="public/styles.css"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="public/script.js"></script> 

<ul id="nav">     <li><a href="page.html"> Home </a><br>     <br>     <li><a class="active" href=""> Appointment </a> <br>     <br>     <li><a href="specsPage.html"> Build </a> <br>     <br>     <li><a href="contactPage.html"> Contact </a> <br>      <br> </ul>  <div id="schedulePage">     <h4> Schedule an Appointment </h4>      <div id="fullForm">              First Name: <input id="firstname" type="text" name="firstname" placeholder="First Name..."> <br>             <br>             Last Name: <input id="lastname" type="text" name="lastname" placeholder="Last Name..."> <br>             <br>             Email: <input id="email" type="text" name="email" placeholder="Email..."> <br>             <br>             Phone Number: <input id="phone" type="text" name="phone" placeholder="Callback Number..."> <br>             <br>             <input id="send_email" type="submit" name="submit" value="Submit">             <span id="message"></span>       </div> </div> 

And the scripts:

$(document).ready(function(){ var from,to,subject,text; $("#send_email").click(function(){           to=$("#email").val();     firstname=$("#firstname").val();     lastname=$("#lastname").val();     $("#message").text("Sending E-mail...Please wait");     $.get("http://localhost:4000/send",{to:to,subject:firstname,text:lastname},function(data){     if(data=="sent"){         $("#message").empty().html(" Email is been sent at " + to + " . Please check inbox !");     }      }); }); 

回答1:

First, you need to enable less secure application in Gmail How to enable less secure apps in google

then install node-mailer

npm i nodemailer

paste this function in your code

     var nodemailer = require('nodemailer');     // create reusable transporter object using the default SMTP transport     var transporter = nodemailer.createTransport('smtps://yourmail%40gmail.com:yourpass@smtp.gmail.com');      // setup e-mail data with unicode symbols  function sendmail(to,subject,text,html,info) {  var mailOptions = {     from: '"Testing Mail" <youmail@gmail.com>', // sender address     to: to, // list of receivers     subject: subject, // Subject line     text: text, // plaintext body     html: html  // html body }; transporter.sendMail(mailOptions, function(error, infoo){     var res={         responsecode:''     }     if(error){         res.responsecode=401;         return info(new Error(res.responsecode),null);     }     res.responsecode=200;     info(null,res.responsecode+infoo.response); });} 

now call the function

app.get('/send',function (req,res) { //pass value from your html form here sendmail('to', 'subject', 'body', '<h1>its Html</h1>', function (err, info) {     if (err) {         console.log("Error is " + err)         return     }     console.log("Email sent. " + info) })}) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!