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

SyntaxError: Unexpected token M in JSON at position 0 #109

Open mdakovac opened 3 years ago

mdakovac commented 3 years ago

I get this error when trying to run the basic example:

nodemailerMailgun.sendMail({
  from: 'myemail@example.com',
  to: 'recipient@domain.com', // An array if you have multiple recipients.
  cc:'second@domain.com',
  bcc:'secretagent@company.gov',
  subject: 'Hey you, awesome!',
  'h:Reply-To': 'reply2this@company.com',
  //You can use "html:" to send HTML email content. It's magic!
  html: '<b>Wow Big powerful letters</b>',
  //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}`);
  }
});

versions:
"nodemailer": "^6.6.1", "nodemailer-mailgun-transport": "^2.1.3", node: 12

full error:

SyntaxError: Unexpected token M in JSON at position 0
>      at JSON.parse (<anonymous>)
>      at x.json (local\node_modules\mailgun.js\dist\mailgun.js:2:37447)
>      at processTicksAndRejections (internal/process/task_queues.js:97:5)
yosefeliezrie commented 3 years ago

getting this exact same error I have tried stripping all code down to the bare bones example as per docs (and you have above) and getting the same error.

Error: SyntaxError: Unexpected token M in JSON at position 0

yosefeliezrie commented 3 years ago

Possibly connected to this issue. https://github.com/mailgun/mailgun-js/issues/134 see this comment https://github.com/mailgun/mailgun-js/issues/134#issuecomment-856135049 it has to do with the format of the domain.

orliesaurus commented 3 years ago

@mdakovac could you try to use your real domain instead of a sandbox domain and report back if that has fixed it? then we can update the docs!

singh-inder commented 3 years ago

I fixed this error like this=>

In the domain, add your app's url, Do not add https , for example => sample.herokuapp.com and add another property url, here we have to add the mailgun api url. Refer to the example below

const auth = {
  auth: {
    api_key: "Your api key",
    domain: "sample.herokuapp.com",
    url: "https://api.mailgun.net/v3"
  }
};

const nodemailerMailgun = nodemailer.createTransport(mg(auth));
priscilaribeiro commented 2 years ago

I had the same error... The following structure solved it for me:

const mg = require("nodemailer-mailgun-transport")

module.exports = async function(req, res){

const mailgunAuth = {
auth: {
api_key: "",
domain: "mydomain.com",
url: "https://api.mailgun.net/v3"
}
}

const smtpTransport = nodemailer.createTransport(mg(mailgunAuth))

smtpTransport.sendMail({
from: 'Now Digital [email@email.com](mailto:email@email.com)',
to: '[emailclient@mail.com](mailto:emailclient@mail.com)',
subject: 'Teste de email!',
html: 'Email enviado com sucesso!',
}, (err, info) => {
if (err) {
console.log(Error: ${err});
res.json({'error': err})
}
else {
console.log(Response: ${info});
res.json('ok')
}
});
}