节点js从电子邮件地址发送网格空(必填)

时间:2017-08-07 08:17:03

标签: node.js sendgrid

我使用Sendgrid使用Node jS发送电子邮件。

但即时通过电子邮件地址(必填)"即使我已指定了来自地址。

在用户注册时会发送邮件,但是对于以下函数调用,我会收到错误。

在我的路线/ orders.js

// Start Sendgrid Configuration Settings
var options = {
    auth: {
        api_user: 'user123', // Sendgrid username
        api_key: '$user123' // Sendgrid password
    }
}

var client = nodemailer.createTransport(sgTransport(options));
// End Sendgrid Configuration Settings  

router.post('/changeStatus', (req, res, next) => {
Orders.changeStatus(req.body.status, req.body.id, (err, orders) => {

    if (err) {
        res.json({ success: false, msg: 'No Orders' });
    }
    else {
        Orders.getUserByOrderNo(req.body.id, (err, user) => {
            //Send mail on success
            var email = {
                from: 'Localhost Staff, staff@localhost.com',
                to: user[0].user_id.email,
                subject: 'Welcome to ABC',
                text: 'Hello ' + user[0].user_id.username + ', thank you for registering at Food4Smiles.com. ',
                html: 'Hello<strong> ' + user[0].user_id.username + '</strong>,<br><br>Thank you for registering at ABC.com. '
            };
            // Function to send e-mail to the user
            client.sendMail(user[0].user_id.email, function (err, info) {
                if (err) {
                    console.log(err); // If error with sending e-mail, log to console/terminal
                }
                else {
                    console.log('Mesage sent !!' + info.response);
                }
            });
        })
        res.json({ success: true, order: orders });
    }
});
})

模型/ orders.js

module.exports.getUserByOrderNo = function (orderNo, callback) {

    console.log(orderNo)
    Orders.find({ orderNo: orderNo }).populate('user_id').exec(callback)
}

完整错误堆栈跟踪:

  

错误:从电子邮件地址清空(必填)

     

在Request._callback(/var/www/html/Express/Local-MEAN-dev/node_modules/sendgrid/lib/sendgrid.js:88:25)       在Request.self.callback(/var/www/html/Express/Local-MEAN-dev/node_modules/request/request.js:188:22)       在emitTwo(events.js:106:13)       在Request.emit(events.js:194:7)       在请求。 (/var/www/html/Express/Local-MEAN-dev/node_modules/request/request.js:1171:10)       在emitOne(events.js:96:13)       在Request.emit(events.js:191:7)       在IncomingMessage。 (/var/www/html/Express/Local-MEAN-dev/node_modules/request/request.js:1091:12)       at Object.onceWrapper(events.js:293:19)       在emitNone(events.js:91:20)       在IncomingMessage.emit(events.js:188:7)       at endReadableNT(_stream_readable.js:974:12)       at _combinedTickCallback(internal / process / next_tick.js:80:11)       at process._tickCallback(internal / process / next_tick.js:104:9)

1 个答案:

答案 0 :(得分:0)

好的,我的错误在于sendMail函数:

我不得不将其更改为:

client.sendMail(email, function (err, info){}