Backbench Docs
  • Introduction
  • Getting Started
    • BBLANG
    • Node
    • Python
    • Go
  • Backbench Features and UI workflow
  • API Reference
    • BBLANG
    • Node
    • Python
    • Go
  • 3rd Party Integrations
    • Login using Facebook
    • Login using Google
    • Login with Twitter
    • Login using LinkedIn
    • Connection with MongoDB
    • Connection with MySQL
    • Geo Location Integration
    • Integration with AWS S3
    • Integration with Google Cloud Storage
    • Integration with Razorpay
    • Integration with Stripe
    • Integration with Mailchimp
    • Integration with Segment
    • Sending Emails with SendGrid
    • Sending Emails with Mailgun
    • Sending SMS with Twilio
    • Sending Push Notification with Pushwoosh
  • Troubleshooting and support
    • CNAME Support
    • FAQS
    • Error Codes
  • Testing
Powered by GitBook
On this page
  • What You'll Need
  • Prerequisites for the app
  • Frontend
  • Dependencies
  • Backend
  • Asking permissions
  1. 3rd Party Integrations

Sending Emails with Mailgun

PreviousSending Emails with SendGridNextSending SMS with Twilio

Last updated 6 years ago

What You'll Need

Backbench account - , 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);
 });
};
sendMail(message) => {
MAILGUN_AUTH = {
    user: "api",
    pass: "API_Key"
}

bb:http:post({
    url: "https://api.mailgun.net/v3/mailgun.backbench.io/messages",
    formData: message,
    auth: MAILGUN_AUTH
});
}
sendMail:handler(req) => {
message = "{{emailbody}}" % req.body

response = sendMail({
        from: req.body.from,
        to: req.body.to,
        subject: req.body.subject,
        html: message
})
bb:log(response, req.body.from, req.body.to, req.body.subject, message);
return response.statusMessage;
}

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).

Sign Up