0
header = ds_map_create();
header[? "Content-Type"] = "application/json";
header[? "api"] = "My api key that im censoring :)";
json = ds_map_create()
    {
    json[? "from"] = "mailgun@lapaihui.org";
    json[? "to"] = "My email that im also censoring :]";
    json[? "subject"] = "Dear user";
    json[? "text"] = "This is your game talking";
    }
http_request("https://api.mailgun.net/v3/lapaihui.org/messages","POST",header,json);

basically im trying to send an email using mailgun api but something is just not working if any netcode gods can help i will greatly apreciete it and credit it!

1 Answers1

0

Without any information on the error you getting or why it's not working, I can't be certain what you need, but I have two working solutions that will hopefully help.

If you're sending from the browser:

function sendMgEmail(pFrom, pTo, pSubject, pText, mgApiKey){
    const formData = new FormData();

    formData.append('from', pFrom);
    formData.append('to', pTo);
    formData.append('subject', pSubject);
    formData.append('text', pText);

    const qXhr = new XMLHttpRequest;
    const qMethod = 'POST';
    const qUrl = 'https://api.mailgun.net/v3/{{YOUR_DOMAIN}}/messages';
    qXhr.open(qMethod, qUrl);
    qXhr.setRequestHeader('Authorization', 'Basic ' + window.btoa('api:' + mgApiKey));
    qXhr.send(formData);
    qXhr.onload = function() {
        if(qXhr.status == '200' || qXhr.status == '201') {
            console.log('email queued', qXhr.status, qXhr.responseText);
        } else {
            console.log('ERROR ', qXhr.status, qXhr.responseText);
        }
    }
}

If from a Nodejs application, the XMLHttpRequest approach does not seem to work:

First, refer to https://www.npmjs.com/package/mailgun.js?utm_source=recordnotfound.com#messages

Then, install form-data and mailgun.js

npm i form-data
npm i mailgun.js

Lastly, the code...

const FormData = require('form-data');
const Mailgun = require('mailgun.js');

exports.sendMgEmail(pFrom, pTo, pSubject, pText, mgApiKey) {
    const mailgun = new Mailgun(FormData);
    const mg = mailgun.client({username: 'api', key: mgApiKey})

    mg.messages.create('{{YOUR_DOMAIN}}', {
      from: pFrom,
      to: pTo,
      subject: pSubject,
      text: pText
    })
    .then(msg => console.log(msg))
    .catch(err => console.error(err));

}
LWSChad
  • 331
  • 3
  • 14