Node.js 如何使用Node提交HTML联系人表单?

Node.js 如何使用Node提交HTML联系人表单?,node.js,forms,email,Node.js,Forms,Email,我在尝试使用我的node express应用程序设置要提交的联系人表单时遇到问题。我已经安装了Nodemailer,但我以前从未使用过它。这是我到目前为止所做的 app.js var express = require('express'); var fs = require('fs'); var bodyParser = require('body-parser'); var nodemailer = require('nodemailer'); var app = express();

我在尝试使用我的node express应用程序设置要提交的联系人表单时遇到问题。我已经安装了Nodemailer,但我以前从未使用过它。这是我到目前为止所做的

app.js

var express = require('express');
var fs = require('fs');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var app = express();


app.use(express.static(process.cwd() + '/public'));
app.use(bodyParser.urlencoded({extended: true}));

app.get('/', function(req, res){
    res.send(fs.readFileSync('./views/index.html', 'utf8'));

});

app.post('/', function(req, res) {
    if(req.body.mail == "" || req.body.subject == "") {
        res.send("Error: Email & Subject should not be Blank");
        return false;
    }

var smtpTransport = nodemailer.createTransport("SMTP", {
    host: "smtp.gmail.com", 
    secureConnection: true, 
    port: 465, 
        auth: {
            user: '',
            pass: ''
        }
});

var mailOptions = {
    from: "Node Emailer - <email@gmail.com>",
    to: req.body.email, 
    subject: req.body.subject + " -",
    html: "<b>"+req.body.description+"<b>"
}
smtpTransport.sendMail(mailOptions, function(error, response) {
    if (error) {
        res.send("Email could not be sent due to error:" +error);
    }else {
        res.send("Email has been sent successfully");
    }
});
});

app.listen(process.env.PORT || 3000, function() {
    console.log("LISTENING!");
});
var express=require('express');
var fs=需要('fs');
var bodyParser=require('body-parser');
var nodeEmailer=require('nodeEmailer');
var-app=express();
app.use(express.static(process.cwd()+'/public');
use(bodyParser.urlencoded({extended:true}));
app.get('/',函数(req,res){
res.send(fs.readFileSync('./views/index.html','utf8');
});
app.post(“/”,函数(请求,res){
如果(req.body.mail==“”| | req.body.subject==“”){
res.send(“错误:电子邮件和主题不应为空”);
返回false;
}
var smtpTransport=nodemailer.createTransport(“SMTP”{
主机:“smtp.gmail.com”,
安全连接:正确,
港口:465,
认证:{
用户:“”,
通行证:“”
}
});
var mailpoptions={
发件人:“节点电子邮件发送者-”,
发送至:req.body.email,
主题:req.body.subject+“-”,
html:“+req.body.description+”
}
发送邮件(邮件选项,函数(错误,响应){
如果(错误){
res.send(“由于错误:+错误,无法发送电子邮件”);
}否则{
res.send(“电子邮件已成功发送”);
}
});
});
app.listen(process.env.PORT | | 3000,函数(){
console.log(“监听!”);
});
联系方式

<form action='/' method='post' class='contact-form commentsblock'>

  <div>
       <label for='g52-name' class='grunion-field-label name'>Name<span>
       (required)</span></label>

       <input type='text' name='g52-name' id='g52-name' value='' 
       class='name'  
       required aria-required='true'/>

  </div>

   <div>
        <label for='g52-email' class='grunion-field-label email'>Email<span>
         (required)</span></label>
        <input type='email' name='g52-email' id='g52-email' value='' 
         class='email'  required aria-required='true'/>
   </div>

    <div>
        <label for='g52-website' class='grunion-field-label 
        url'>Website</label>
        <input type='text' name='g52-website' id='g52-website' value='' 
        class='url'  />
    </div>

<div>
        <label for='contact-form-comment-g52-comment' class='grunion-field-label textarea'>Comment<span>(required)</span></label>
        <textarea name='g52-comment' id='contact-form-comment-g52-comment' rows='20' class='textarea'  required aria-required='true'></textarea>
    </div>
    <p class='contact-submit'>
        <input type='submit' value='Submit' class='pushbutton-wide'/>
</form>

名称
(必选)
电子邮件
(必选)
网站
评论(必填)


我没有太多使用Node发送邮件的经验。我有一个一页的网站,我想做的就是能够发送电子邮件。如何将表单与节点连接

通过
req.body
检查的所有字段都需要与
HTML元素的
name=”“
属性匹配。您似乎没有在脚本中获取任何这些值,有时会随意查看
req.body.email
req.body.mail
。如果您在提交时
console.log(req.body)
,您将看到如下内容:

{
  "g52-name": "...",
  "g52-email": "...",
  "g52-website": "...",
  "g52-comment": "..."
}
这就是表单内容所在的位置

app.post('/', function(req, res) {

  var subject = req.body["g52-name"];
  var mail = req.body["g52-email"];
  var website = req.body["g52-website"];
  var description = req.body["g52-comment"];

  if (!subject || !mail) {
    res.send("Error: Email & Subject should not be Blank");
    return false;
  }

  // rest of email-sending code here
  // ...

}

通过
req.body
检查的所有字段都需要与
HTML元素的
name=”“
属性匹配。您似乎没有在脚本中获取任何这些值,有时会随意查看
req.body.email
req.body.mail
。如果您在提交时
console.log(req.body)
,您将看到如下内容:

{
  "g52-name": "...",
  "g52-email": "...",
  "g52-website": "...",
  "g52-comment": "..."
}
这就是表单内容所在的位置

app.post('/', function(req, res) {

  var subject = req.body["g52-name"];
  var mail = req.body["g52-email"];
  var website = req.body["g52-website"];
  var description = req.body["g52-comment"];

  if (!subject || !mail) {
    res.send("Error: Email & Subject should not be Blank");
    return false;
  }

  // rest of email-sending code here
  // ...

}

请定义您遇到的问题/错误?问题是我不知道如何使用app.post with Nodemailer将我的HTML表单连接到app.js文件。现在一切都已解决。请定义您遇到的问题/错误?问题是我不知道如何使用app.post with Nodemailer将我的HTML表单连接到app.js文件。现在一切都解决了。