为什么我的节点邮件程序代码无法执行?

时间:2019-06-28 02:01:44

标签: node.js reactjs nodemailer

我正在尝试从使用react构建的本地托管网站发送电子邮件。当用户单击表单上的购买按钮时,它会正确执行一些代码,但会完全跳过节点邮件程序代码,而不会引发任何错误。我已经得到节点邮件程序在其自己的.js文件中成功发送电子邮件并直接从命令终端调用该消息的情况。我上周刚开始学习React和node,所以我可以想象这个问题是由于对我应该如何实现node mailer的一种误解造成的。谢谢您的帮助。

axios.post('http://localhost:5000/payment', user)

            .then(function (response) {
                // handle success
                setSuccess(true);
                console.log("before before");
                setPopup("Your ticket has been purchased. You may pick up your ticket at the will-call desk.");
                console.log("before");
                console.log("mailtime");


                let transporter = nodemailer.createTransport({
                  service: 'gmail',
                  auth: {
                    user: 'xxxxx@gmail.com',
                    pass: 'xxxx'
                  },
                });

                var mailOptions = {
                  from: 'tonycalzonecinemas@gmail.com',
                  to: 'myemail@gmail.com',
                  subject: 'Your ticket confirmation',
                  body: 'hello'
                };

                transporter.sendMail(mailOptions, function(error, info){
                    console.log('sending');
                    if (error) {
                      console.log(error);
                    } else {
                      console.log('Email sent: ' + info.response);
                    }
                  });

                console.log("confirmed");
            })

            .catch(function (error) {
                // handle error
                setSuccess(false);
                setPopup("Information is invalid. Please try again")
            })

            .finally(function () {
                console.log("always");
                // always executed
            });

        event.preventDefault() //stops browser clearing form
    }

0 个答案:

没有答案