Skip to content

Instantly share code, notes, and snippets.

@Med116
Created July 22, 2014 20:30
Show Gist options
  • Save Med116/6d36f94e0960b3ae9b15 to your computer and use it in GitHub Desktop.
Save Med116/6d36f94e0960b3ae9b15 to your computer and use it in GitHub Desktop.
sends email via mailgun api
var MailGun = function(configObj){
var mailgunKey = process.env.MAILGUN_KEY || configObj.apiKey;
this.auth = "api:" + mailgunKey;
this.hostname = "api.mailgun.net";
this.toArray = [];
this.text = "default text";
this.html = "";
this.subject = configObj.subject || "No Subject";
this.from = configObj.from || "";
this.domain = configObj.domain;
}
MailGun.prototype.addRecip = function(recipEmail){
this.toArray.push(recipEmail);
}
MailGun.prototype.addRecipArray = function(recipArr){
this.toArray.concat(recipArr);
}
MailGun.prototype.setSubject = function(subj){
this.subject = subj;
}
MailGun.prototype.setText = function(text){
this.text = text;
}
MailGun.prototype.setHtml = function(html){
this.html = html;
}
MailGun.prototype.setFrom = function(from){
this.from = from;
}
MailGun.prototype.send = function(){
var https = require("https");
var querystring = require("querystring");
var body = {
"from": this.from,
"to" : this.formatToArray(this.toArray),
"subject": this.subject,
"Content-Type":"application/x-www-form-urlencoded",
"html":this.html
}
console.log("BODY");
console.log(body);
var bodyStr = querystring.stringify(body);
if(this.subject){
body.subject = this.subject;
}
if(this.text){
body.text = this.text;
body.html = "";
}
if(this.html){
body.html = this.html;
body.text = "";
}
var options = {
"auth": this.auth,
"hostname" : this.hostname,
"port": 443,
"path": "/v2/" + this.domain + " .com/messages",
"method":"POST",
"headers":{
"Content-Type":"application/x-www-form-urlencoded"
}
};
console.log("OPTIONS");
console.log(options);
var req = https.request(options,function(res){
req.on("error",function(e){
console.log(e.message);
});
res.on("data",function(chunk){
console.log(chunk.toString());
});
});
req.write(bodyStr);
req.end();
}
MailGun.prototype.formatToArray = function(toArray){
var toStr = "";
for (var i=0;i<toArray.length;i++){
toStr+= "name <" + toArray[i] + ">,";
}
// get rid of trailing comma
return toStr.replace(/,$/, "");
}
module.exports = MailGun;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment