SyntaxError: Unexpected token M in JSON at position 0
Opened this issue · 5 comments
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)
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
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.
@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!
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));
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')
}
});
}