0

I added the needed dns configurations on netlify where my website is hosted and when I go to the DNS records of my domain all the ticks on all the fields(TXT, MX, CNAME) are green so I know the domain is verified. However I am unable to use the domain when sending messages and get this error:

Error: Forbidden
    at IncomingMessage.<anonymous> (C:\Users\MSI\Desktop\mail\node_modules\mailgun-js\lib\request.js:327:17)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (_stream_readable.js:1220:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  statusCode: 401
}

Here is the code that I copied from the mailgun docs and changed the values to my domain and my api key:

const mailgun = require('mailgun-js');
const DOMAIN = 'example.com'; 
//The domain is written in the exact same form (not https://example.com)
const api_key = '****************************************';
const mg = mailgun({ apiKey: api_key, domain: DOMAIN });
const data = {
 from: 'Name <myemail@gmail.com>',
 to: 'myemail@gmail.com',
 subject: 'Hello',
 text: 'Testing some Mailgun awesomness!',
};

mg.messages().send(data, (err, body) => {
 if (err) throw err;
  console.log(body);
});

I have looked through all the stackoverflow questions on this topic it seems like but still didnt find what the issue is (And again, the code above is working with a sandbox domain). help

  • 401 unauthorised error occurred. Means something wrong with api key or from email isn't verified email. – Shahnawaz Kadari Apr 26 '21 at 20:07
  • Can the email adress still be unverified if Im using it to log into my mailgun account? The api key is definetely correct. – Nikita Lafinskiy Apr 26 '21 at 20:36
  • From email adress must be same domain which you owned. ex: your domain is mydomain.com then from email end with same domain name. Ex: no-reply@mydomain.com (gmail won't work) – Shahnawaz Kadari Apr 26 '21 at 21:03
  • So is the data object supposed to look like this:```const data = { from: 'User ', to: 'myEMAIL@gmail.com', subject: 'Hello', text: 'Testing some Mailgun awesomness!', }; ```? – Nikita Lafinskiy Apr 26 '21 at 21:23
  • Yes, like this. – Shahnawaz Kadari Apr 26 '21 at 21:25
  • It still throws the same error though, does the stuff that comes before the domain and the @ matter? You used "no-reply" in the example, is it going to make a difference if I change it to something else? – Nikita Lafinskiy Apr 26 '21 at 21:26
  • Try using sandbox.... (Default domain provided by mailigun), – Shahnawaz Kadari Apr 26 '21 at 21:44
  • It works when I use a sandbox domain but not with a verified domain – Nikita Lafinskiy Apr 26 '21 at 22:57
  • You have to add exactly the TXT entries from the MailGun portal in to your DNS and check it from the MailGun portal. Further you have to follow exactly all steps in "Getting started" in the MailGun portal, until you reach 100%, else you will not be able to send under under your domain... – FredyWenger Jul 01 '21 at 16:43
  • This detailed description may help you: https://stackoverflow.com/questions/68150905/can-i-send-email-with-mailgun-sandbox-domain-under-my-local-os/68542876#68542876 – FredyWenger Jul 27 '21 at 10:13

0 Answers0