TypeError при попытке отправить электронную почту через nodemailer & mailgun - PullRequest
0 голосов
/ 10 марта 2020

Вечер.

Я пытаюсь получить nodemail и nodemail-mailgun для отправки электронной почты. (В конечном итоге сформируйте результаты отправки).

Я попытался выполнить поиск по SO, а также по обычному Google, и мне не удается найти кого-то с такой же проблемой.

Настройка:

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));

Маршрут:

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

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

  nodemailerMailgun.sendMail({
    from: "no-reply@airsafetynw.com",
    to: 'john.s@airsafetynw.com', // An array if you have multiple recipients.
    cc:'adam.f.wilkinson@gmail.com',
    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!')
    }
  });

});

Ошибка:

asnw> Ошибка: Ошибка типа: Невозможно прочитать свойство 'id' из неопределенного

POST / contact - - ms - -

Я удалил ВСЕ js переменные в моем шаблоне в надежде, что, возможно, одна из них будет неправильной, однако, если я res.send или console.log req.body .variable выше .sendmail они работают просто отлично. Если кто-то знает, где я могу найти «идентификатор», чтобы я мог выяснить, что не определено, я мог бы двигаться вперед.

1 Ответ

0 голосов
/ 10 марта 2020

Вам не нужен nodemailer-mailgun-transport, nodemailer может отправлять почту в одиночку.

вот код, который я использую для отправки электронной почты только с использованием nodemailer, и он отлично работает для меня.

const nodemailer = require('nodemailer')

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

var nodemailer_Option =
{
  from: 'customer@gmail.com',
  to: 'authAdmin@gmail.com',
  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: ' );
    }
});
...