mulungood

Using variables conditionally in Supabase’s transactional e-mails

Learn how to provide default values and use if statements in your email templates. Explore the power of Go's `html/template`.

fernanda's photofernanda
Henrique Doro's photoHenrique Doro

If you have user metadata values you want to use in your email templates, but don’t want to risk having users receive “Welcome to our app, NULL” in the case where metadata is missing, you can use variables conditionally.

<!-- In an ideal world... --> <h1>Welcome to our app, {{ .Data.name || "" }}!</h1>

Supabase uses gomail.v2 to send e-mails, which is powered by Go’s html/template. Providing default values for variables can be achieved with the {{ or }} function:

<h1>Welcome to our app, {{ or .Data.name "" }}!</h1>

You can also use if statements for more advanced templates:

{{ if .Data.favoriteColor }} <p> We're are also fans of {{ .Data.favoriteColor }} - let's paint the world with it, {{ or .Data.name "together" }}! </p> {{ end }}

You can use other features of Go’s html/template, of course - it’s so powerful it’s even used by the Hugo static site generator (SSG) to create entire websites!

⚠️ Be careful: when defining templates’ subject lines in Supabase local development through supabase/config.toml, you need to escape quotes in the template - Welcome {{ or .Data.name \"\" }} - however, when adding the subject line in the Supabase dashboard, you can’t include these quotes.