将表单数据拉入nodemailer

时间:2018-02-22 18:02:52

标签: javascript node.js forms nodemailer

我有一个表单,我使用nodemailer向自己发送测试邮件 在"到"和"主题"我希望将用户输入的电子邮件放入表单中,当用户点击提交时,它会向他们发送电子邮件。我已经尝试req.emailreq.body.email并且没有成功。

app.get("/teachers/payment", function (req, res) {
    res.render("teacher_payment.ejs")
});

app.post("/registration", function (req, res) {
    var transporter = nodemailer.createTransport({
        service: 'Gmail'
        , auth: {
            user: 'user'
            , pass: 'pass'
        }
    });

    var mailOptions = {
        from: 'zyyangch@gmail.com'
        , to: 'req.body.email'
        , subject: req.body.email
        , text: 'It works! ✔', //plaintext body
        html: '<a href="http://localhost:3000/complete-profile">Click Here</a>'
    };

    //send mail with defined transport object
    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log(error);
        }
        else {
            console.log('Message sent: ' + info.response);
        }
    });
})

对此有何解决方法?

1 个答案:

答案 0 :(得分:1)

您需要从电子邮件密钥中获取值,不带引号:

 var mailOptions = {
    from: 'zyyangch@gmail.com'
    , to: req.body.email
    , subject: req.body.email
    , text: 'It works! ✔', //plaintext body
    html: '<a href="http://localhost:3000/complete-profile">Click Here</a>'
};