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.