Skip to main content

Send messages through your own provider

Use your own email and SMS provider and your own templates for the messages of LumoAuth.

RecipeDraftView source
This page is a draft. The code exists, and not all of it has been run for this page yet. The page says what was checked.
Works with
Plain HTTP

What it shows​

  1. In the portal, you connect an email provider. Your organization's mail then comes from your own address and domain.
  2. In the portal, you connect an SMS provider, so that people can receive one-time codes by text message.
  3. A script replaces the text of the sign-in link email (the magic link). It previews the new text, and then saves it.
  4. The script asks for a sign-in link for a test address. The email that arrives has your text.

Before you start​

  • The permissions settings.manage and users.delete in the portal. Both are needed for the email provider and for email templates. The SMS provider needs settings.manage.
  • An account at an email provider: Amazon SES, SendGrid, Mailgun, Resend, Postmark, or an SMTP server on a public host. The From address must be a verified sender there.
  • For text messages, an account at Twilio, Vonage, AWS SNS or MessageBird.
  • An API key with the scopes admin:settings:read and admin:settings:write, for the script. Create it in the portal under Applications & developer tools, API keys. The setup script also needs admin:users:write and admin:clients:read.
  • Magic link login turned on under Sign-in settings, Login flow. Without it, the request for a sign-in link sends nothing.
  • Check that your plan includes custom email templates in the plans matrix.
VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_API_KEYAn API key with the scopes admin:settings:read and admin:settings:write. The setup script also needs admin:users:write and admin:clients:read.
TEST_EMAILOptional. The address that receives a sign-in link. The setup script creates a user with this address, by default email-template-user@example.com. Use an address of your own mailbox to read the message.
CLIENT_IDThe client ID of an application of your organization, which the request for the sign-in link names. The setup script prints the one of the Starter App.

The setup script creates what the recipe needs, through the Admin API. You can run it again at any time.

node operations/email-and-sms/setup.mjs
setup.mjs
// Creates a test user who receives the sign-in link, and finds the client ID
// of the Starter App, which the link request names. Run it again at any time.
//
// node operations/email-and-sms/setup.mjs
//
// The API key needs the scopes admin:users:write and admin:clients:read.
// Put an address of your own mailbox in TEST_EMAIL to read the message there.
import { randomBytes } from 'node:crypto';
import { ensure, list, printEnv } from '../../_lib/admin.mjs';

const email = process.env.TEST_EMAIL ?? 'email-template-user@example.com';
const user = await ensure('/admin/users', (u) => u.email === email, {
email,
name: 'Maya Chen',
// The link signs the user in, so nobody needs the password.
password: `${randomBytes(18).toString('base64url')}#Aa1`,
emailVerified: true,
});

const starter = (await list('/admin/clients')).find((c) => c.name === 'Starter App');
if (!starter) {
console.error('No application named "Starter App". Set CLIENT_ID to the client ID of another application.');
process.exit(1);
}

console.log(`User ${user.email} is ready.`);
printEnv({ TEST_EMAIL: user.email, CLIENT_ID: starter.clientId });

Connect an email provider​

  1. Open Integrations in the portal, and go to the section SMS & email delivery.
  2. Click Configure on the card of your provider. The page Email provider opens with that provider chosen under Provider.
  3. Enter the credentials that the provider asks for, for example the API key for SendGrid or Resend.
  4. Under Delivery, enter the From address, and if you like a From name and a Reply-To address.
  5. Click Save configuration, then Send test email. The test uses the saved settings, also while your provider is not used yet.
  6. Turn on Route this organization's email through my provider and save again. From now on, every email of your organization goes through your provider.

Connect an SMS provider​

  1. In the same section, click Configure on the card of your SMS provider. The page SMS provider opens.
  2. Enter the credentials, for example the Account SID, the Auth token and a From number (E.164) for Twilio.
  3. Turn on Enable SMS delivery, and click Save configuration.
  4. Click Send test SMS to send a message to your own number.
  5. To offer the codes at sign-in, open Sign-in settings, MFA policy, and tick Text message (SMS) under Allowed factors.

These steps were written from the docs. Nobody has followed them for this page yet. The script ran against an organization, and the sign-in link arrived with the new text. The provider steps were not followed: they need an account at an email or SMS provider.

Code​

The script changes the sign-in link email. Change http/magic-link.html to your own text first. A placeholder for the link, such as {{magic_url}}, may stand as text or as the whole href of a link, but never inside another address.

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization. The script ran against an organization, and the sign-in link arrived with the new text. The provider steps were not followed: they need an account at an email or SMS provider.

operations/email-and-sms/http/template.sh
#!/bin/sh
# Replace the text of the sign-in link email (magic link) with your own,
# preview it, and ask for a sign-in link to see the real message.
#
# sh operations/email-and-sms/http/template.sh
#
# The script needs curl and jq.
set -eu

API="$LUMOAUTH_URL/orgs/$LUMOAUTH_ORG_ID/api/v1"
BODY_FILE="$(dirname "$0")/magic-link.html"

admin() { # <method> <path> [json body]
curl --silent --show-error --fail-with-body --request "$1" "$API/admin$2" \
--header "X-API-Key: $LUMOAUTH_API_KEY" \
--header "Content-Type: application/json" \
${3:+--data "$3"}
}

TEMPLATE=$(jq -n --rawfile body "$BODY_FILE" \
'{subject: "Your sign-in link for {{tenant_name}}", body_html: $body}')

# 1. The placeholders that this email offers.
echo "placeholders: $(admin GET /email-templates/magic_link/variables | jq -r '.variables | keys | join(" ")')"

# 2. Render the new text before you save it. The preview fills in the name
# of the organization.
PREVIEW=$(admin POST /email-templates/magic_link/preview "$TEMPLATE")
echo "preview subject: $(printf '%s\n' "$PREVIEW" | jq -r .subject)"

# 3. Save it. It replaces the default text in every language and for every
# application.
admin PUT /email-templates/magic_link "$TEMPLATE" > /dev/null
echo "saved: magic_link"

# 4. Ask for a real sign-in link. This endpoint takes no API key: it is the
# one that the sign-in form of your application calls. The answer is the
# same whether or not the address has an account.
if [ -n "${TEST_EMAIL:-}" ]; then
ANSWER=$(curl --silent --show-error --fail-with-body "$API/oauth/magic-link" \
--header "Content-Type: application/json" \
--data "$(jq -n --arg email "$TEST_EMAIL" --arg client "$CLIENT_ID" '{email: $email, client_id: $client}')")
echo "sign-in link for $TEST_EMAIL: $(printf '%s\n' "$ANSWER" | jq -r .status)"
fi
operations/email-and-sms/http/magic-link.html
<p>Hi {{user_name}},</p>
<p>Here is your link to sign in to {{tenant_name}}. It works once and expires in {{expires_in}}.</p>
<p><a href="{{magic_url}}">Sign in to {{tenant_name}}</a></p>
<p>Did not ask for this link? Then you can ignore this email. Your account stays as it is.</p>
<p>The Acme support team</p>
operations/email-and-sms/http/revert.sh
#!/bin/sh
# Go back to the default text of the sign-in link email.
#
# sh operations/email-and-sms/http/revert.sh
set -eu

curl --silent --show-error --fail-with-body --request DELETE \
"$LUMOAUTH_URL/orgs/$LUMOAUTH_ORG_ID/api/v1/admin/email-templates/magic_link" \
--header "X-API-Key: $LUMOAUTH_API_KEY" > /dev/null
echo "magic_link: back to the default text"
sh operations/email-and-sms/http/template.sh

Check the result​

Output
placeholders: {{expires_in}} {{magic_url}} {{tenant_name}} {{user_name}}
preview subject: Your sign-in link for Acme Corp
saved: magic_link
sign-in link for email-template-user@example.com: sent
  • Open the mailbox of the test address. The email with the subject "Your sign-in link for Acme Corp" has your text, with the name of the person and the expiry filled in.
  • With an email provider, the email comes from your From address. Without one, it comes from a LumoAuth address.
  • The portal shows the saved text under Login page & emails, Email templates, where Customise edits it and Revert to default removes it.
  • sh operations/email-and-sms/http/revert.sh goes back to the default text.

Limits​

  • The preview fills in only the name of the organization. The link, the name of the person and the expiry stay as placeholders.
  • An SMTP server must be a public host with STARTTLS or TLS, so a mail catcher on your own machine cannot receive the mail.
  • An organization has one email provider and one SMS provider at a time. There is no failover to a second provider.
  • Text messages carry one-time codes only. You change the wording of the SMS code message under Login page & emails, Languages & text, not with an email template.
  • The request for a sign-in link always answers sent, also for an address that has no account. A request over a limit gets the same answer and sends nothing.