orliesaurus / nodemailer-mailgun-transport

nodemailer is an amazing node module to send emails within any of your nodejs apps. This is the transport plugin that goes with nodemailer to send email using Mailgun 🔫
MIT License
880 stars 97 forks source link

TypeError: Cannot read property 'id' of undefined #93

Closed ItsThatFox closed 3 years ago

ItsThatFox commented 4 years ago

I'm trying to send email via Nodemailer and Mailgun via this package. I keep getting this error: data: TypeError: Cannot read property 'id' of undefined at Object.send (/app/node_modules/nodemailer-mailgun-transport/src/index.js:126:51)

This is my code:

**
 * Server dependencies
 */
var app = require('express')();
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
 });
var nodemailer = require('nodemailer');
var mailgun = require('nodemailer-mailgun-transport');
var templateDefault = require('./templates/templatedefault'); 
var logger = require('logger').createLogger('development.log');

/**
 * Socket configuration
 */
var http = require('http').createServer(app);

http.listen(process.env.PORT || 8080, function() {
    console.log('listening on *:8080');
});

var io = require('socket.io')(http, {
    pingInterval: 2000, // How many ms before the client sends a new ping packet
    pingTimeout: 5000, // How many ms without a pong packet to consider the connection closed
});
io.set('origins', '*:*');

/**
 * Sockets connection
 *
 * @param {param} socket Socket information
 */
io.on('connection', function(socket) {
    socket.on('userConnected', function(data) {
        var userId = data.userId;
        socket.userId = userId;
        socket.join(data.userId)
    });
    socket.on('notify', function(notificationData) {
        var notificationJson = JSON.stringify(notificationData);
        var notificationObject;
        try {
            notificationObject = JSON.parse(notificationJson);
            var notificationTemplate = new templateDefault(notificationObject.message.template).templateStructure();
            //Get specified template
            if(notificationTemplate){
            //Send email
            var mailOptions = {
                from: 'admin@MYDOMAIN.net',
                to: notificationObject.message.sendTo,
                subject: notificationObject.message.subject,
                html: notificationTemplate
            };
            notify(io, mailOptions, notificationJson, socket, notificationObject); 
             }else{
                logger.info("Invalid Template");
                socket.broadcast.emit("error", {
                    data: "Invalid json data"
                }); 
             }
        } catch (e) {
            logger.info("Invalid JSON data", e);
            socket.broadcast.emit("error", {
                data: "Invalid json data"
            });
        }
    });
    socket.on('disconnect', function() {
        logger.info("User " + socket.userId + " disconnected");
        socket.leave(socket.userId);
    });
});

/**
 * E-mail authentication
 */
var auth = {
    auth: {
      api_key: 'MYKEY',
      host: "api.eu.mailgun.net/v3",
      domain: 'https://MYDOMAIN.net'
    }
}

/**
 * Send Email
 */
var nodemailerMailgun = nodemailer.createTransport(mailgun(auth));
function notify(io, mailOptions, notificationJson, socket, notificationObject) {
    nodemailerMailgun.sendMail(mailOptions, function (error, info) {
        logger.info("Received data:", notificationJson);
        if (error) {
            logger.info("Error sending email", error);
            socket.emit("error", { data: error });
        }
        else {
            logger.info(notificationObject.message.sendTo + " received an email with the following subject: " + notificationObject.message.subject, info.response);
            socket.emit("success", { data: notificationObject });
        }
    });
}
mohit497 commented 4 years ago

I am also getting the same error

MatteoLazzaretti commented 4 years ago

Schermata 2020-07-16 19:41:44 Same error with this

` ... const auth = { auth: { api_key: API_KEY, domain: DOMAIN } };

const nodemailerMailgun = nodemailer.createTransport(mg(auth));

nodemailerMailgun.sendMail( { from: REMOVED, to: REMOVED, // An array if you have multiple recipients. subject: 'Hey you, awesome!', //You can use "html:" to send HTML email content. It's magic! html: 'Wow Big powerful letters', //You can use "text:" to send plain-text content. It's oldschool! text: 'Mailgun rocks, pow pow!' }, (err, info) => { if (err) { console.log(Error: ${err}); } else { console.log(Response: ${info}); } } );`

MatteoLazzaretti commented 4 years ago

Resolved. DOMAIN is sandboxXXXmailgun.org not https://api.mailgun.net/v3/sandboxXXXmailgun.org