---
title: "Sending emails | WebhookRelay"
meta:
  "og:description": "Webhook Relay provides a Mailgun package to easily send emails on various events."
  "og:title": "Sending emails"
  description: "Webhook Relay provides a Mailgun package to easily send emails on various events."
---

![Stripes](https://webhookrelay.com/docs/webhooks/functions/send-emails/images/stripes.svg)

Documentation

**Fundamentals**

# **Sending emails**

Webhook Relay provides a Mailgun package to easily send emails on various events.

Webhook Relay provides a Mailgun module to easily send emails on various events.

## [Prerequisites](#prerequisites)

- [Mailgun](https://www.mailgun.com/) account
- [Webhook Relay](https://my.webhookrelay.com/) account

## [Create a Function](#create-a-function)

First, head to the [Functions page](https://my.webhookrelay.com/functions) and create a new function called `mailgun`.

You can use the **mailgun** module to send emails. To start sending emails, create a new Function. This function will need an API key and domain:

```
const domain = cfg.get("domain")
const apiKey = cfg.get("api_key")

// mailgun.initialize('domain', 'api-key', 'region (us/eu)')
const initResult = mailgun.initialize(domain, apiKey, "us")
if (initResult && initResult.error) {
    console.error("Mailgun init failed:", initResult.error)
}

// mailgun.send('sender@foo.com', 'subject', 'body-here', 'recipient@foo.com')
const sendResult = mailgun.send("your-email@example.com", "test subject", "test body", "john@example.com")
if (sendResult && sendResult.error) {
    console.error("Email send failed:", sendResult.error)
}
```

```
-- Import Mailgun helper package
local mailgun = require('mailgun')

local domain = cfg:GetValue('domain')
local api_key = cfg:GetValue('api_key')

-- mailgun.initialize('domain', 'api-key', 'region (us/eu)')
err = mailgun.initialize(domain, api_key, 'us')
if err then error(err) end

-- mailgun.send('sender@foo.com', 'subject', 'body-here', 'recipient@foo.com')
err = mailgun.send('your-email@example.com', 'test subject', 'test body', 'john@example.com')
if err then error(err) end
```

Then, you will need to enter your API key from the [API keys page](https://app.mailgun.com/app/account/security/api_keys) and set the as config variables for your function. You can find more details on how to find your API keys in [Mailgun](https://help.mailgun.com/hc/en-us/articles/203380100-Where-Can-I-Find-My-API-Key-and-SMTP-Credentials-) here.

## [Create a Bucket](#create-a-bucket)

Then, in the [https://my.webhookrelay.com/buckets](https://my.webhookrelay.com/buckets) click on "create empty bucket" and type `webhook-to-mail`. Once created, go to the input details and click on "transform" section to select your Function:

![selecting image](https://webhookrelay.com/docs/webhooks/functions/send-emails/images/docs/webhooks/functions/function-select.png)

## [Receiving webhooks](#receiving-webhooks)

In your input or bucket details page you should see your public endpoint, use it to receive webhooks.

Did this page help you?