简体   繁体   English

条带化付款成功后,Nodemailer不发送电子邮件

[英]Nodemailer not sending e-mail after successful stripe payment charge

I am trying to send an email to myself after a purchase has been made via stripe. 通过条纹购买后,我试图向自己发送电子邮件。 I currently have a personal and business email that I am using for this. 我目前有个人和企业电子邮件,我正在为此使用。 I am new to node.js, and I'm confused as to why this is not working. 我是node.js的新手,我对为什么它不起作用感到困惑。

What makes sense to me is to add this code as an argument to the .then callback. 对我而言有意义的是将此代码添加为.then回调的参数。 When testing this out locally, the success page renders, however no emails are sent and the two console.log's at the bottom are not being outputted to the console. 在本地进行测试时,将显示成功页面,但是不会发送电子邮件,并且底部的两个console.log不会输出到控制台。 The following is my app.js 以下是我的app.js

const express = require('express');
const stripe = require('stripe')('mystripekey');
const bodyParser = require('body-parser');
const exphps = require('express-handlebars');
const nodemailer = require('nodemailer')

var app = express();

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: 'myemail@gmail.com',
      pass: 'mypassword'
    }
});

app.engine('handlebars', exphps({defaultLayout: 'main'}));
app.set('view engine', 'handlebars')

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.use(express.static(`${__dirname}/public`));

app.get('/', (req, res) => {
    res.render('index');
});

app.post('/charge', (req, res) => {
    const amount = 25000;

    stripe.customers.create({
        email: req.body.stripeEmail,
        source: req.body.stripeToken
    })
    .then(customer => stripe.charges.create({
        amount,
        description: 'Advertisement',
        currency: 'usd',
        customer: customer.id
    }))
    .then(charge => {

        // This is where I'm getting confused
        res.render('success')

        var mailOptions = {
            from: req.body.stripeEmail,
            to: 'mybizemail@gmail.com',
            subject: 'A purchase was made',
            text: req.body.stripeEmail + ' made a purchase!'
          };

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

});

I expected either an error message to be logged or the email sent to be logged but that is not happening. 我希望记录错误消息或发送电子邮件记录,但这没有发生。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

I think the node mailer code is never getting called. 我认为节点邮件程序代码永远不会被调用。 Try putting the res.render in callback in transporter.sendMail. 尝试将res.render放在transporter.sendMail的回调中。

Like so: 像这样:

.then(charge => {
    console.log("charge>", charge);
    var mailOptions = {
        from: req.body.stripeEmail,
        to: 'mybizemail@gmail.com',
        subject: 'A purchase was made',
        text: req.body.stripeEmail + ' made a purchase!'
    };
    console.log("mailOptions>", mailOptions);  
    transporter.sendMail(mailOptions, function(error, info){
        console.log("error1>", error);
        console.log("info>", info);
        if (error) {
          console.log(error);
          res.render('error')
        } else {
          console.log('Email sent: ' + info.response);
          res.render('success')
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM