Sending Emails with Mailgun

What You'll Need

Backbench account - Sign Up, the personal account will always remain free to use.

Mailgun account - Its free to Sign Up and you get 10000 free emails every month.

Prerequisites for the app

  1. Sign In to Backbench account.

  2. Select +, in the upper right corner to create a Bench. For example, say "bench_one" and select CREATE or hit Enter.

Frontend

  1. Select +, in the upper right corner of file manager to create a html file. For example, say "index.html" and select CREATE or hit Enter.

  2. Copy and paste the code module from below.

  3. Select save.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>mail service</title>
</head>
<body>
<div id="contact">
    <h1>Send an email</h1>
    <form action="/m1" method="post">
        <fieldset>
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" placeholder="Enter your full name" />

            <label for="email">Email:</label>
            <input type="email" id="email" name="email" placeholder="Enter your email address" />

            <label for="subject">subject:</label>
            <input type="subject" id="subject" name="subject" placeholder="subject" />

            <label for="message">Message:</label>
            <textarea id="message" placeholder="Message" name="message"></textarea>

            <input type="submit" value="Send message" />
        </fieldset>
    </form>
</div>
</body> 
</html>

Dependencies

For nodejs only.

Create a module name package.json and paste the following code.

package.json
{
   "dependencies":{
      "mailgun-js":"^0.10.0"
   }
}

Backend

  1. Select +, in the upper right corner to create a Module. For example, say "Send_email.js" and select CREATE or hit Enter.

  2. Copy and paste the code module from below.

  3. Replace API_KEY, DOMAIN from Mailgun dashboard.

  4. Select save.

Send_email.js
var mailgun = require("mailgun-js");
var api_key = "YOUR_API_KEY_FROM_DASHBOARD";
var DOMAIN = "YOUR_DOMAIN_NAME_FROM_DOMAINS-SECTION";
var mailgun = require("mailgun-js")({ apiKey: api_key, domain: DOMAIN });

module.exports.endpoint = function(req, cb) {
 var data = {
  from: "admin@backbench.io",
  to: req.body.email,
  subject: req.body.subject,
  text: req.body.message
 };
 mailgun.messages().send(data, function(error, body) {
  console.log(body);
  cb(undefined, body);
 });
};

Asking permissions

Add Authorized Recipients in Domain section (Email-id to which email must be sent, it can me more than one, the recipients email will get a mail from Mailgun to verify if they are interested to receive the email).

Last updated