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
Sign In to Backbench account.
Select +, in the upper right corner to create a Bench. For example, say "bench_one" and select CREATE or hit Enter.
Frontend
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.
Copy and paste the code module from below.
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.
{
"dependencies":{
"mailgun-js":"^0.10.0"
}
}
Backend
Select +, in the upper right corner to create a Module. For example, say "Send_email.js" and select CREATE or hit Enter.
Copy and paste the code module from below.
Replace API_KEY, DOMAIN from Mailgun dashboard.
Select save.
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