> For the complete documentation index, see [llms.txt](https://backbench.gitbook.io/backbench-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://backbench.gitbook.io/backbench-docs/3rd-party-integrations/sending-emails-with-mailgun.md).

# Sending Emails with Mailgun

## What You'll Need <a href="#what-youll-need" id="what-youll-need"></a>

Backbench account - [Sign Up](https://bench.backbench.io/signup), 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 <a href="#prerequisites-for-sample-app" id="prerequisites-for-sample-app"></a>

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 <a href="#frontend" id="frontend"></a>

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.

```markup
<!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 <a href="#backend" id="backend"></a>

For nodejs only.&#x20;

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

{% code title="package.json" %}

```javascript
{
   "dependencies":{
      "mailgun-js":"^0.10.0"
   }
}
```

{% endcode %}

## Backend <a href="#backend" id="backend"></a>

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.

{% tabs %}
{% tab title="NodeJS" %}
{% code title="Send\_email.js" %}

```javascript
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);
 });
};

```

{% endcode %}
{% endtab %}

{% tab title="BBLANG" %}

```javascript
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;
}
```

{% endtab %}
{% endtabs %}

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