2

I have been trying to send an email using a post request without luck. I keep getting a 401 (UNAUTHORIZED) error. Here is my code:

  axios.post('https://api.mailgun.net/v3/MY-DOMAIN/messages', {
    data: new URLSearchParams({
      from: 'from',
      to: 'to',
      subject: 'subject',
      html: 'html'
    }),
    auth: {
      username: 'api',
      password: 'MY-API-KEY'
    },
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  }).then(function (response) {
    console.log(response.data)
  }).catch(function (error) {
    console.log(error.response)
  })

I've been sending post requests using axios to other API's fine. Any ideas? Thanks.

EDIT: Here is the mailgun.js method I was using to create messages (which worked) but I couldn't send attachments

  var mg = mailgun.client({username: 'api', key: 'MY-API-KEY'})

  mg.messages.create('MY-DOMAIN', payload).then(msg => console.log(msg)) // logs response data
  .catch(err => console.log(err)) // logs any error

EDIT 2: Mailgun Response

Unfortunately, you would experience issues using Javascript for things like authentication and the Access-Control-Allow-Headers. You might find that authentication (which we require) will not work with the Access-Control-Allow-Headers. We do this intentionally to forbid sending messages from front-end applications because in many cases users would hard-code their API keys and thereby expose their authentication information publicly to the Internet.

WillMac997
  • 113
  • 3
  • 9
  • Two problems... You are sending data using `params` which become query parameters but Mailgun is expecting an `application/x-www-form-urlencoded` request body. Axios by default sends data as JSON. – Phil Oct 15 '19 at 23:35
  • FYI, instead of `params`, try `data: new URLSearchParams({ from, to, subject, html })` – Phil Oct 15 '19 at 23:41
  • I've revised the code to to use data and added Content-Type to the header but still encounter the same error. `data: new URLSearchParams({ from: payload.from, to: payload.to, subject: payload.subject, html: payload.html }), headers: { 'Content-Type': 'application/x-www-form-urlencoded' }` – WillMac997 Oct 16 '19 at 01:51
  • Then perhaps your credentials are incorrect. Can you verify them anywhere? Also, please update the code in your question – Phil Oct 16 '19 at 01:57
  • They are the same credentials I was using to create messages using the mailgun.js library but I was unable to send attachments that way. I'm using axios now hoping to fix that. I've updated with the mailgun.js library method for reference. – WillMac997 Oct 16 '19 at 02:37
  • From looking at the API `curl` examples and Postman integration, it looks like they're expecting a `multipart/form-data` request even though it doesn't explicitly state this. Try [this answer](https://stackoverflow.com/a/47630754/283366) – Phil Oct 16 '19 at 05:42
  • Thank you for your help but I have just received word from mailgun. I've updated the post with their response. – WillMac997 Oct 16 '19 at 20:07
  • Sheesh, would be nice if they added that to their documentation. In that case, you'll need to implement something server-side – Phil Oct 16 '19 at 21:57

0 Answers0