Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to send email using MailGun Node API

When trying to send a simple test message via mailgun using their Node api I don't get any errors, but I also am not receiving emails nor am I seeing any response in the mailgun logs.

Here is the code I am using ...

const mailgun = require('mailgun-js')({
  apiKey: 'XXXXXXXXXX', 
  domain: 'https://api.mailgun.net/v3/XXXXXXXXX'
})

async function sendInvite() { 
  var email = process.argv[process.argv.length - 1]

  const data = {
    from: '[email protected]',
    to: email,
    subject: 'Welcome!',
    html: '<p>Welcome!</p><p>You are very special!</p>'
  }

  return new Promise((resolve, reject) => {
    mailgun.messages().send(data, (error, body) => {
      if (error) {
        console.error(error)
        reject(error)
        return
      }
      console.log(`Invite sent to ${email}`)
      console.log(data)
      console.log(body)
      resolve();
    })
  })

}

sendInvite().then(() => console.log('Done')).catch((err) => console.error(err))
like image 997
ra9r Avatar asked Mar 25 '26 14:03

ra9r


1 Answers

I discovered that the problem I had was in the format of the domain I was passing into mailgun.

Before I had the following :

const mailgun = require('mailgun-js')({
  apiKey: 'XXXXXXXXXX', 
  domain: 'https://api.mailgun.net/v3/XXXXXXXXX'
})

The problem is that the value for domain should not include the https://api.mailgun.net/v3/ portion of the URL. Instead it should ONLY have your domain, e.g. mail.mydomain.com

like image 63
ra9r Avatar answered Mar 28 '26 02:03

ra9r