---
title: "Receive emails on new Stripe subscribers | WebhookRelay"
meta:
  "og:description": "It's nice to get Stripe notifications on new payments however we can turn any Stripe into an email"
  "og:title": "Receive emails on new Stripe subscribers"
  description: "It's nice to get Stripe notifications on new payments however we can turn any Stripe into an email"
---

![Stripes](https://webhookrelay.com/blog/stripe-webhook-to-email/images/stripes.svg)

# **Receive emails on new Stripe subscribers**

It's nice to get Stripe notifications on new payments however we can turn any Stripe into an email

![Stripe to Email](https://webhookrelay.com/blog/stripe-webhook-to-email/images/blog/stripe-to-email/cover.png)

It's great to receive [Stripe](https://stripe.com/) emails on new payments however it's harder to differentiate new customers from recurring when you are mostly selling subscriptions.

In this short tutorial we will set up a function that receives a webhook from Stripe and transforms it into an email which is then dispatched via [Mailgun](https://www.mailgun.com/).

## [Prerequisites](#prerequisites)

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

## [Create Webhook Relay bucket](#create-webhook-relay-bucket)

In order to start receiving webhooks and doing things with them you will first need to create a bucket here [https://my.webhookrelay.com/buckets](https://my.webhookrelay.com/buckets).

Once created, copy the input URL which looks like `https://xyz.hooks.webhookrelay.com`, we will need it for Stripe.

## [Configure Stripe](#configure-stripe)

When you have logged into your Stripe dashboard, head to webhook admin page [https://dashboard.stripe.com/webhooks](https://dashboard.stripe.com/webhooks) and add a new endpoint with the input URL from the previous step.

![stripe webhooks](https://webhookrelay.com/blog/stripe-webhook-to-email/images/blog/stripe-to-email/create-webhook.png)

It should listen for `customer.subscription.created` events.

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

Our function will do several things, in this example I will filter out all subscriptions that or on a plan named "free", prepare some variables from the webhook and send an email via Mailgun.

1. First, go to function's "config variables" tab and set two variables:
  - **api_key** - your Mailgun API key ([https://app.mailgun.com/app/account/security/api_keys](https://app.mailgun.com/app/account/security/api_keys))
  - **domain** - your Mailgun domain (for example mg.example.com)
2. Create the function body and set your sender and recipient emails (replace _[from-address@example.com](https://webhookrelay.com/blog/stripe-webhook-to-email//mailto:from-address@example.com)_ and _[to-address@example.com](https://webhookrelay.com/blog/stripe-webhook-to-email//mailto:to-address@example.com)_):

```
mailgun = require('mailgun')
json = require('json')

-- First, decoding the stripe payload
local request_payload, err = json.decode(r.RequestBody)
if err then error(err) end

-- You can use product IDs or any other fields that you find useful here
local plan = request_payload["data"]["object"]["plan"]["nickname"]
local amount = request_payload["data"]["object"]["plan"]["amount"]
local subscription_id = request_payload["data"]["object"]["id"]
local customer_id = request_payload["data"]["object"]["customer"]

-- If you have a free plan, you can ignore it
if plan == "Free" then 
    -- request is not important, don't forward it
    r:StopForwarding()
    return
end 

-- preparing the mailgun client client
err = mailgun.initialize(cfg:GetValue('domain'), cfg:GetValue('api_key'), 'us')
if err then error(err) end

-- Preparing the email message!
local text = string.format([[
    Horray!

    Customer subscribed to '%s' ($%s).

    Subscription: https://dashboard.stripe.com/subscriptions/%s
    Customer: https://dashboard.stripe.com/customers/%s
    
    Kind regards,
    Webhook Relay Function
    ]], plan, amount/100, subscription_id, customer_id)

err = mailgun.send('from-address@example.com', 'Hooray! New Paying Customer!', text, 'to-address@example.com')
if err then error(err) end

r:SetRequestBody('sent')
```

## [Attaching function to the Bucket's Input](#attaching-function-to-the-buckets-input)

Now, let's go back to our Bucket, click on the Input and attach this function:

![Attach function to input](https://webhookrelay.com/blog/stripe-webhook-to-email/images/blog/stripe-to-email/attach-func-to-input.png)

Once function is in place, you are ready to start receiving the emails.

## [Wrap up](#wrap-up)

That's it, on the next new subscription you will receive an email with the details! :) Some ideas for the follow up work:

- Function to detect plan downgrades and send you an email
- Detect churned customers
- Email you when payment charge fails

Enjoy!