A Wilkinson
A Wilkinson

Reputation: 87

TypeError on attempting to send email via nodemailer & mailgun

I am trying to get nodemail and nodemail-mailgun to send an email. (Eventually form submission results).

I have tried searching on SO as well as just general google and I cannot seem to find someone with the same issue.

The Setup:

const nodemailer = require('nodemailer');
const mg = require('nodemailer-mailgun-transport'),
bodyParser = require('body-parser');


const auth = {
  auth: {
    api_key: 'REMOVED FOR SECURITY',
    domain: 'REMOVED FOR SECURITY'
  }
}

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

The Route:

router.post('/contact', function(req, res, next) {

  console.log('Send Mail...');

  nodemailerMailgun.sendMail({
    from: "[email protected]",
    to: '[email protected]', // An array if you have multiple recipients.
    cc:'[email protected]',
    subject: 'Air Safety NW contact form',
    //You can use "html:" to send HTML email content. It's magic!
    html: '<b>From:</b></br>',
    //You can use "text:" to send plain-text content. It's oldschool!
    text: ""
  }, (err, info) => {
    if (err) {
      console.log(`Error: ${err}`);
    }
    else {
      res.send('Successful!')
    }
  });

});

The Error:

asnw > Error: TypeError: Cannot read property 'id' of undefined

POST /contact - - ms - -

I have removed ALL js variables in my template in hopes that maybe one of them was incorrect however if I res.send or console.log the req.body.variable above the .sendmail they work just fine. If someone knows where I can look for the 'id' so I can figure out what is not defined I might be able to move forward.

Upvotes: 1

Views: 509

Answers (3)

aifodu
aifodu

Reputation: 304

The structure of your DOMAIN variable could be the problem.

Instead of setting DOMAIN variable to https://api.mailgun.net/v3/DOMAIN, simply set it to DOMAIN

For example, set it to mg.website.org and not https://api.mailgun.net/v3/mg.website.org

See Unable to send mail via Mailgun over api or smtp

Upvotes: 1

uko akanowo
uko akanowo

Reputation: 11

Your mailgun auth domain should be of the form sandboxbXXX.mailgun.org not https://app.mailgun.com/app/sending/domains/sandboxbXXX.mailgun.org

Upvotes: 1

Engineer S. Saad
Engineer S. Saad

Reputation: 384

You do not need nodemailer-mailgun-transport, nodemailer is able to send the mail all alone.

here is the code which i use to send email only using nodemailer and it work fine for me.

const nodemailer = require('nodemailer')

const transferEmail_details = nodemailer.createTransport({
  host: 'smtp.gmail.com',
  port: 465,
  secure: true, // use TLS
  auth: {
    user: [email protected],
    pass: admin123
  },
  tls: {
    // do not fail on invalid certs
    rejectUnauthorized: false
  }
})

var nodemailer_Option =
{
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Complain',
  text: 'abc' ,
  html : '<h1>Hello</h1>'
}


transferEmail_details.sendMail(nodemailer_Option, function (err, info) {
    if (err) {
        console.log(err);
    } else {
        console.log(info , 'Message sent: ' );
    }
});

Upvotes: 0

Related Questions