0
votes

Im using Sendgrid to send emails using Node jS.

However im getting "Empty from email address (required)", even though i have specified the from address.

Mails are sent upon registration for users, but for the below function call, i get the error.

in my Routes/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, [email protected]',
                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 });
    }
});
})

models/orders.js

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

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

Full error stack trace:

Error: Empty from email address (required)

at Request._callback (/var/www/html/Express/Local-MEAN-dev/node_modules/sendgrid/lib/sendgrid.js:88:25) at Request.self.callback (/var/www/html/Express/Local-MEAN-dev/node_modules/request/request.js:188:22) at emitTwo (events.js:106:13) at Request.emit (events.js:194:7) at Request. (/var/www/html/Express/Local-MEAN-dev/node_modules/request/request.js:1171:10) at emitOne (events.js:96:13) at Request.emit (events.js:191:7) at IncomingMessage. (/var/www/html/Express/Local-MEAN-dev/node_modules/request/request.js:1091:12) at Object.onceWrapper (events.js:293:19) at emitNone (events.js:91:20) at 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

1 Answers

0
votes

Ok my mistake was in the sendMail function:

I had to change it to :

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