Simple Mailgun Test using Node & Jake
Sometimes I need a quick and easy way to send emails using Mailgun. It takes a few minutes to configure this setup, but it's a one-time thing, and the code is portable.
Step 1 - Install Jake
JakeJS is a task management tool similar to Grunt or Gulp but inspired by Ruby Rake.
npm i -g jake
Step 2 - Create a New Project
Create a new directory.
mkdir ~/Desktop/my_new_project && ~/Desktop/my_new_project
Create a file.
touch email.jake
/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
Filename: Email Tester via Mailgun
* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
var util = require('util');
var moment = require('moment');
namespace('email', function () {
desc('Test E-mail. Ex: jake email:test');
task('test', { async: true }, function() {
var timestamp = moment().format("dddd, MMMM Do YYYY, h:mm:ss a");
var apiKey = "<ENTER YOUR MAILGUN API KEY>";
var domain = "mydomain.com";
var fromName = "Excited User ";
var from = fromName + "noreply@mydomain.com";
var to = "tester@maildrop.cc";
var subject = "E-mail Test";
var message = "Testing some Mailgun awesomness! \n" + timestamp;
var cmds = [
util.format("curl -s --user 'api:%s' https://api.mailgun.net/v3/%s/messages -F from='%s' -F to=%s -F subject='%s' -F text='%s'",
apiKey, domain, from, to, subject, message)
];
jake.exec(cmds, { printStdout: true });
});
});
/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
Filename: Email Tester via NodeJS
* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
const mailgun = require("mailgun-js");
const DOMAIN = "sandboxabXXXXXXXXXXXXXXXXXXXXXXXXX123.mailgun.org";
const mg = mailgun({apiKey: "a33..................XXXa-XXXXXXX-XXXX", domain: DOMAIN});
const data = {
from: "Mailgun Sandbox <postmaster@sandboxabXXXXXXXXXXXXXXXXXXXXX123.mailgun.org>",
to: "signup@skygroundmedia.com",
subject: "Hello",
text: "Testing some Mailgun awesomness!"
};
mg.messages().send(data, function (error, body) {
console.log(body);
});