简体   繁体   中英

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.

What makes sense to me is to add this code as an argument to the .then callback. 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. The following is my 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.

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')
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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