Customize email sender
Get started
Integrate Mailgun with Kinde to send welcome emails and other communications to your users. This guide covers two approaches: configuring Mailgun SMTP in Kinde for automatic email delivery, or using webhooks with Mailgun’s API for custom email templates and workflows.
You can send emails to your users using Mailgun in two ways:
You can configure Kinde to send emails using your Mailgun account. This means your users will receive communications from your business’s email address, and not Kinde’s.
Other benefits include better control of email deliverability, analytics and tracking, scalability, email compliance, etc.
Please review the documentation at Customize email sender for details about how custom SMTP works with Kinde and third-party providers.
Before configuring SMTP in Kinde, you need to set up and verify your domain in Mailgun:
smtp.mailgun.org587 for TLS or 465 for SSLpostmaster@your-domain.mailgun.org or your custom SMTP usernameSee Mailgun’s SMTP documentation for more details.
If you are setting this up and your production environment is already live, we recommend testing this in a staging or test environment first.
In Kinde, go to Settings > Email.
Enter a Sender name. This is usually your business name, but might be a specific person.
Enable the Use custom sender switch. The SMTP details section opens.
Enter the Sender email. This is the address your users will receive emails from and should be one of your verified domains from your Mailgun account (e.g., noreply@yourdomain.com).
Enter the SMTP details of your email provider:
smtp.mailgun.org587 (for TLS) or 465 (for SSL)postmaster@your-domain.mailgun.org)Select Send test email. This sends a test email to the email address you are logged into Kinde with.
If the test is successful, select Save.
If the test is not successful, check the SMTP details (step 5) and try sending a test email again. If this does not work, see the troubleshooting section below.
For more control over welcome email content and design, you can use Kinde webhooks to trigger Mailgun’s API when new users are created.
Sign in to your Kinde dashboard and go to Settings
Select Webhooks from the settings area and select the Add webhook button to create a new webhook for your app
Add the name and optional description of your webhook
Enter your application’s public webhook endpoint URL (e.g., https://yourwebsite.com/api/mailgun)
From the Events section, select the Add Events link. This will open a dialog box to select the event types you will subscribe to
Select user.created from the events list and select Save to continue
Select Save to save the webhook
When a user is created, Kinde sends a webhook payload (as a JWT token) that contains:
{ "type": "user.created", "data": { "user": { "email": "new.user@example.com", "user_id": "usr_abc", "first_name": "John", "last_name": "Doe" } }, "source": "admin"}Use the @kinde-oss/webhook package to verify Kinde webhook tokens.
npm install @kinde-oss/webhookJavaScript backend example:
import { decodeWebhook } from "@kinde-oss/webhook"
try { // Get the token from the request const token = await req.text()
// Validate and decode the webhook token const event = await decodeWebhook(token, { issuerUrl: process.env.KINDE_ISSUER_URL, })
if(event.type === "user.created") { // send welcome email }
return { status: 200 }} catch (err) { console.error("Webhook validation error:", err) return { status: 400, error: err.message }}Use the mailgun.js and form-data packages to send the email via Mailgun API from your JavaScript backend.
npm install mailgun.js form-dataHere’s how to send a custom email from your endpoint:
import Mailgun from "mailgun.js"import formData from "form-data"
const mailgun = new Mailgun(formData)const mg = mailgun.client({ username: "api", key: process.env.MAILGUN_API_KEY || "key-yourkeyhere",})
async function sendWelcomeEmail(email) { await mg.messages.create(process.env.MAILGUN_DOMAIN || "mg.yourdomain.com", { from: "Kinde <kinde@yourdomain.com>", to: [email], subject: "Your Sign In Information", text: "To sign in to your account, click here: https://yourwebsite.com/api/auth/login", html: `<h1>Your Sign In Information</h1> <p>To sign in to your account, <a href="https://yourwebsite.com/api/auth/login">click here</a> and enter your email.</p> <p>Kinde.</p>`, })}The source field in the webhook payload indicates whether the user was created manually (admin), via API (api), or by the user themselves (user). You can filter by source to trigger different email automation flows.
Adding a user will trigger the webhook to send a POST request to your application. Once your application verifies the request, it will send a new email with the sign-in information to the newly created user.
The user will receive an email in their inbox from your application, with the email template from your code. By adding the sign-in link to the email, you can direct the user to go to your app’s sign-in page to complete the process.
Similarly, if you add users using the Kinde API, you can filter the event source “api” to trigger another email automation flow.
@kinde-oss/webhook package before processing to ensure the request is authenticsource field indicates whether the user was created manually (admin), via API (api), or by the user themselves (user). You may want to treat these differentlyIf sending a test email fails:
smtp.mailgun.orgpostmaster@your-domain.mailgun.org)For more details, see Mailgun’s SMTP troubleshooting guide.
If webhooks aren’t triggering emails:
To improve email deliverability: