Hey everyone, I’m stuck with a MailGun integration issue in my NodeJS app. I’m trying to send emails using my custom domain, but I keep getting a 401 forbidden error. Here’s a simplified version of what I’m doing:
const emailSender = require('email-sender-lib');
const MY_DOMAIN = 'mail.mysite.org';
const sender = emailSender({secretKey: 'my-secret-key-here', domain: MY_DOMAIN});
const emailContent = {
from: 'Help Desk <[email protected]>',
to: '[email protected]',
subject: 'Test Email',
text: 'Just testing our email system!'
};
sender.sendMessage(emailContent, (err, response) => {
if (err) console.error('Error:', err);
else console.log('Response:', response);
});
Weirdly, it works fine with the sandbox domain, but not with my actual domain. I’ve double-checked my API key and domain settings. Any ideas what might be causing this? I’m pretty sure I’m missing something obvious, but I can’t figure out what. Thanks for any help!
hey mate, i had a similar issue. check ur domain settings in mailgun. sometimes they need extra verification steps for custom domains. also, make sure ur using the right api key (private one, not public). if that dont work, try flushing dns cache. hope this helps!
Have you verified that your custom domain is properly set up in MailGun? Sometimes the issue lies in the DNS records. Make sure you’ve added all the required TXT and MX records for your domain. Also, double-check that you’re using the correct API key for your specific domain, not just your account’s general key.
Another thing to consider is the API endpoint. If you’re using a EU-based account, you might need to specify a different endpoint in your code. Lastly, ensure your domain is fully verified and active in the MailGun dashboard. There’s often a delay between adding a domain and it becoming fully operational.
If none of these solve the issue, I’d recommend reaching out to MailGun support. They can provide more specific troubleshooting based on your account details.
I’ve encountered this kind of problem before and found that it often boils down to domain verification or API key permissions. For example, one issue might be that the custom domain hasn’t fully propagated in the MailGun dashboard yet, which would cause authentication issues.
Another possibility is that the wrong API key is being used—MailGun uses different keys for sending and receiving, so confirming that you’re using a sending key might solve the problem. If your domain was just added, the changes could take a few hours to take effect, and checking your region-specific settings (like switching to the EU endpoint if necessary) might help. If none of these adjustments work, contacting MailGun support usually provides clear guidance.