From 46f61db54ac7d081b0a783ba5c6f13cdf514cba0 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sat, 24 Jun 2017 15:36:13 -0400 Subject: [PATCH] mailgun --- package.json | 2 +- server.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8dba00c..a159090 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,6 @@ "homepage": "https://github.com/mahuntington/emailer#readme", "dependencies": { "express": "^4.15.3", - "request": "^2.81.0" + "mailgun-js": "^0.11.2" } } diff --git a/server.js b/server.js index 2efd32c..8c89ecd 100644 --- a/server.js +++ b/server.js @@ -1,8 +1,35 @@ const express = require('express'); const app = express(); +var Mailgun = require('mailgun-js'); app.get('/', function(req, res){ - res.send('works'); + var mailgun = new Mailgun({apiKey: process.env.MAILGUNAPIKEY, domain: process.env.MAILGUNDOMAIN}); + + var data = { + //Specify email data + from: 'matt.huntington@gmail.com', + //The email to contact + to: 'matt.huntington@gmail.com', + //Subject and text data + subject: 'Hello from Mailgun', + html: 'Hello, This is not a plain-text email, I wanted to test some spicy Mailgun sauce in NodeJS! Click here to add your email address to a mailing list' + } + + //Invokes the method to send emails given the above data with the helper library + mailgun.messages().send(data, function (err, body) { + //If there is an error, render the error page + if (err) { + res.render('error', { error : err}); + console.log("got an error: ", err); + } + //Else we can greet and leave + else { + //Here "submitted.jade" is the view file for this landing page + //We pass the variable "email" from the url parameter in an object rendered by Jade + res.send('submitted'); + console.log(body); + } + }); }); app.listen(process.env.PORT, function(){