如何使用节点将表单提交到电子邮件并表达

时间:2016-10-04 07:47:40

标签: node.js forms email express

如何从表单(姓名,电子邮件,附件)收集数据并使用快递将其提交到我的电子邮件。我没有找到关于这个主题的综合文章。我将非常感谢你的帮助。

1 个答案:

答案 0 :(得分:5)

我在考虑你知道如何提交表格

通过节点安装流行模块'nodemailer'发送任何邮件

在项目目录中安装1个nodemailer模块

npm install nodemailer

2 - 来到您正在服务器上处理表单提交数据的控制器

var express = require('express'),
nodemailer = require("nodemailer");

app = express.createServer();

app.use(express.bodyParser());

app.post('/formProcess', function (req, res) {
    var data=req.body;

    var smtpTransport = nodemailer.createTransport("SMTP",{
       service: "Gmail", 
       auth: {
       user: "email@gmail.com",
       pass: "gmailPassword"
       }});

   smtpTransport.sendMail({  //email options
   from: "Sender Name <email@gmail.com>",
   to: "Receiver Name <receiver@email.com>", // receiver
   subject: "Emailing with nodemailer", // subject
   html: "here your data goes" // body (var data which we've declared)
    }, function(error, response){  //callback
         if(error){
           console.log(error);
        }else{
           console.log("Message sent: " + res.message);
       }

   smtpTransport.close(); 
    }); });