forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.66k stars 339 forks source link

TypeError: Cannot set property 'originalMessage' of undefined #380

Closed 1thrasher closed 4 years ago

1thrasher commented 4 years ago

After the latest round of npm updates (email-templates v6.0.3, nodemailer v6.3.1, nodemailer-mailgun-transport v1.4.0) I've encountered the following email-templates error:

2019-11-07T16:15:54.476Z email-templates message sent

Email Error occurred TypeError: Cannot set property 'originalMessage' of undefined
    at Email.send (/app/node_modules/email-templates/src/index.js:313:5)
    at process._tickCallback (internal/process/next_tick.js:68:7)

(node:35) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

I'm initializing as follows:

const mailgunAuth = {
  auth: {
    api_key: process.env.MAILGUN_API_KEY,
    domain: process.env.MAILGUN_DOMAIN
  }
}
const transporter = nodemailer.createTransport(mg(mailgunAuth));
const email = new Email({
  transport: transporter,
  views: { 
    root: path.join(__dirname, 'templates'),
  },
});

and sending like so:

email.send({
      template: template,
      message: {
        from: `${process.env.SUPPORT_NAME} <${process.env.SUPPORT_EMAIL}>`,
        replyTo: `${fromName} <${fromEmail}>`,
        to: `${toName} <${toEmail}>`,
      },
      locals: {
        d: data
      }
    }).then(function(data) {
      console.log('Email sent', data);
    }).catch(function(err) {
      console.log('Email Error occurred', err);
    });

This works fine on my local dev machine, but not on my production host (Heroku).

Any thoughts? Thank you in advance!

1thrasher commented 4 years ago

Oops, it appears my MAILGUN_DOMAIN environment variable was not set, and nodemailer-mailgun-transport did not return the error.

I ended up switching to a different transporter (mailgun-nodemailer-transport), which returned a proper error that helped me see the omission in my server config. Doh!