Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mdakovac opened this issue Jun 17, 2021 · 5 comments
Open

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

mdakovac opened this issue Jun 17, 2021 · 5 comments

Comments

@mdakovac
Copy link

mdakovac commented Jun 17, 2021

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
Copy link

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
Copy link

Possibly connected to this issue. mailgun/mailgun.js#134 see this comment mailgun/mailgun.js#134 (comment) it has to do with the format of the domain.

@orliesaurus
Copy link
Owner

@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!

@inderrr
Copy link

inderrr commented Aug 22, 2021

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
Copy link

priscilaribeiro commented Mar 25, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants