Receive emails on new Stripe subscribers

By Karolis Rusenas · Jun 25, 2022

Stripe to Email

It’s great to receive Stripe 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.

Prerequisites

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.

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

Configure Stripe

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

stripe webhooks

It should listen for customer.subscription.created events.

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:
  1. Create the function body and set your sender and recipient emails (replace from-address@example.com and 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('[email protected]', 'Hooray! New Paying Customer!', text, '[email protected]')
if err then error(err) end

r:SetRequestBody('sent')

Attaching function to the Bucket’s Input

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

Attach function to input

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

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!