· 5 years ago · Aug 25, 2020, 06:12 PM
1var express = require('express');
2 var Mailgun = require('mailgun-js');
3 //init express
4 var app = express();
5
6 //Your api key, from Mailgun’s Control Panel
7 var api_key = 'MAILGUN-API-KEY';
8
9 //Your domain, from the Mailgun Control Panel
10 var domain = 'YOUR-DOMAIN.com';
11
12 //Your sending email address
13 var from_who = 'your@email.com';
14
15 //Tell express to fetch files from the /js directory
16 app.use(express.static(__dirname + '/js'));
17 //We're using the Jade templating language because it's fast and neat
18 app.set('view engine', 'jade')
19 //Do something when you're landing on the first page
20 app.get('/', function(req, res) {
21 //render the index.jade file - input forms for humans
22 res.render('index', function(err, html) {
23 if (err) {
24 // log any error to the console for debug
25 console.log(err);
26 }
27 else {
28 //no error, so send the html to the browser
29 res.send(html)
30 };
31 });
32 });
33 // Send a message to the specified email address when you navigate to /submit/someaddr@email.com
34 // The index redirects here
35 app.get('/submit/:mail', function(req,res) {
36 //We pass the api_key and domain to the wrapper, or it won't be able to identify + send emails
37 var mailgun = new Mailgun({apiKey: api_key, domain: domain});
38 var data = {
39 //Specify email data
40 from: from_who,
41 //The email to contact
42 to: req.params.mail,
43 //Subject and text data
44 subject: 'Hello from Mailgun',
45 html: 'Hello, This is not a plain-text email, I wanted to test some spicy Mailgun sauce in NodeJS! <a href="http://0.0.0.0:3030/validate?' + req.params.mail + '">Click here to add your email address to a mailing list</a>'
46 }
47 //Invokes the method to send emails given the above data with the helper library
48 mailgun.messages().send(data, function (err, body) {
49 //If there is an error, render the error page
50 if (err) {
51 res.render('error', { error : err});
52 console.log("got an error: ", err);
53 }
54 //Else we can greet and leave
55 else {
56 //Here "submitted.jade" is the view file for this landing page
57 //We pass the variable "email" from the url parameter in an object rendered by Jade
58 res.render('submitted', { email : req.params.mail });
59 console.log(body);
60 }
61 });
62});
63 app.get('/validate/:mail', function(req,res) {
64 var mailgun = new Mailgun({apiKey: api_key, domain: domain});
65 var members = [
66 {
67 address: req.params.mail
68 }
69 ];
70 //For the sake of this tutorial you need to create a mailing list on Mailgun.com/cp/lists and put its address below
71 mailgun.lists('NAME@MAILINGLIST.COM').members().add({ members: members, subscribed: true }, function (err, body) {
72 console.log(body);
73 if (err) {
74 res.send("Error - check console");
75 }
76 else {
77 res.send("Added to mailing list");
78 }
79 });
80 })
81 app.get('/invoice/:mail', function(req,res){
82 //Which file to send? I made an empty invoice.txt file in the root directory
83 //We required the path module here..to find the full path to attach the file!
84 var path = require("path");
85 var fp = path.join(__dirname, 'invoice.txt');
86 //Settings
87 var mailgun = new Mailgun({apiKey: api_key, domain: domain});
88 var data = {
89 from: from_who,
90 to: req.params.mail,
91 subject: 'An invoice from your friendly hackers',
92 text: 'A fake invoice should be attached, it is just an empty text file after all',
93 attachment: fp
94 };
95 //Sending the email with attachment
96 mailgun.messages().send(data, function (error, body) {
97 if (error) {
98 res.render('error', {error: error});
99 }
100 else {
101 res.send("Attachment is on its way");
102 console.log("attachment sent", fp);
103 }
104 });
105 })
106 app.listen(3030);