Created
March 10, 2018 18:41
-
-
Save rawsh/d6ae286f1daa9774051002aebe5ce7ca to your computer and use it in GitHub Desktop.
Mailgun subscribers for a static website.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "nodelist", | |
"version": "1.0.0", | |
"description": "Mailing lists for DevPy.me", | |
"main": "main.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Robert Washbourne", | |
"license": "ISC", | |
"dependencies": { | |
"body-parser": "^1.15.2", | |
"express": "^4.14.0", | |
"mailgun-js": "^0.7.11", | |
"validator": "^5.4.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NODEJS SERVER | |
var mailgun = require('mailgun-js')({apiKey: "MyAPIkey", domain: "devpy.me"}); | |
var mlist = mailgun.lists('testlist@devpy.me'); | |
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var validator = require('validator'); | |
var app = express(); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.json()); | |
app.use(function(req, res, next) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Credentials", "true"); | |
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT"); | |
res.header("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"); | |
next(); | |
}); | |
app.post('/subscribe', function (req, res) { | |
var email = req.body.email; | |
if(!email || !validator.isEmail(email)) return res.json({success: false, error: "That's not a valid email."}); | |
mlist.members().create({subscribed: true, address: email}, function (err, data) { | |
if(data.message.indexOf("Address already exists") > -1) return res.json({success: false, error: "You're already subscribed. Thanks!"}); | |
res.json({success: true}); | |
}); | |
}); | |
app.listen(process.env.PORT); | |
console.log("listening"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form onsubmit="event.preventDefault(); doSub();"> | |
<fieldset> | |
<legend>Subscribe</legend> | |
<div> | |
<input id="email" placeholder="Your Email Address" type="email" name="Email_Adress" value="" id="Email_Adress" autofocus required> | |
<input class="button" type="submit" value="Subscribe"> | |
</div> | |
<div id="subres"></div> | |
</fieldset> | |
</form> | |
<script> | |
function post(path, params) { | |
var http = new XMLHttpRequest(); | |
var url = path; | |
http.open("POST", url, true); | |
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | |
http.onreadystatechange = function() { | |
if (http.readyState == 4 && http.status == 200) { | |
var res = JSON.parse(http.responseText); | |
var subres = document.getElementById("subres"); | |
if (res["success"] === true) { | |
subres.innerHTML = "You are now subscribed."; | |
ga('send', 'event', 'Subscribe', 'subscribe', 'Page'); | |
} else { | |
subres.innerHTML = res["error"]; | |
} | |
} | |
} | |
http.send(params); | |
} | |
function doSub() { | |
var subres = document.getElementById("subres"); | |
subres.innerHTML = "one moment please..."; | |
var em = document.getElementById("email").value; | |
post('https://changethisurl/subscribe', "email=" + em); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment