Emails

Configuration

Learn how to configure your emails in TurboStarter.

The @turbostarter/email package provides a simple and flexible way to send emails using various email providers. It abstracts the complexity of different email services and offers a consistent interface for sending emails with pre-defined templates.

To configure the email service, you need to set a few environment variables.

EMAIL_PROVIDER="resend"
EMAIL_FROM="hello@resend.dev"
EMAIL_THEME="orange"

Let's break down the variables:

  • EMAIL_PROVIDER - The email provider to use. Defaults to Resend.
  • EMAIL_FROM - The email address that emails will be sent from. Please make sure that the mail address and domain are verified in your mail provider.
  • EMAIL_THEME - The theme color to use for the emails. See Themes for more information.

Configuration will be validated against the schema, so you will see the error messages in the console if something is not right.

Providers

TurboStarter supports multiple email providers, each with its own configuration. Below, you'll find detailed information on how to set up and use each supported provider. Choose the one that best fits your needs and follow the instructions in the respective accordion section.

Templates

In the @turbostarter/email package, we provide a set of pre-defined templates for you to use. You can find them in the packages/email/src/templates directory.

When you run your development server, you will be able to preview all available templates in the browser under http://localhost:3005.

Email preview

Next to the templates, you can also find some shared components that you can use in your emails. The file structure looks like this:

index.ts - Main entrypoint for the templates

Feel free to add your own templates and components or modify existing ones to match them with your brand and style.

How to add a new template?

We'll go through the process of adding a new template, as it requires a few steps to make sure everything works correctly.

Define types

Let's assume that we want to add a welcome email, that new users will receive after signing up.

We'll start with defining new template type in packages/email/src/types/templates.ts file:

templates.ts
export const EmailTemplate = {
  ...AuthEmailTemplate,
  WELCOME: "welcome",
} as const;

Also, we would need to add types for variables that we'll pass to the template (if any), in our case it will be just a name of the user:

templates.ts
type WelcomeEmailVariables = {
  welcome: {
    name: string;
  };
};
 
export type EmailVariables = AuthEmailVariables | WelcomeEmailVariables;

By doing this, we ensure that payload passed to the template will have all required properties and we won't end up with an email that tells your user "Hey, undefined!".

Create template

Next up, we need to create a file with the template itself. We'll create an welcome.tsx file in packages/email/src/templates directory.

welcome.tsx
import { Heading, Preview, Text } from "@react-email/components";
 
import { Button } from "../_components/button";
import { Layout } from "../_components/layout/layout";
 
import type { EmailTemplate, EmailVariables } from "../../types";
 
type Props = EmailVariables[typeof EmailTemplate.WELCOME];
 
export const Welcome = ({ name }: Props) => {
  return (
    <Layout>
      <Preview>Welcome to TurboStarter!</Preview>
      <Heading>Hi, {name}!</Heading>
 
      <Text>Start your journey with our app by clicking the button below.</Text>
 
      <Button>Start</Button>
    </Layout>
  );
};
 
Welcome.subject = "Welcome to TurboStarter!";
 
Welcome.PreviewProps = {
  name: "John Doe",
};
 
export default Welcome;

As you can see, by defining appropriate types for the template, we can safely use the variables as a props in the template.

To learn more about supported components, please refer to the React Email documentation.

Register template

We have to register the template in the main entrypoint of the templates in packages/email/src/templates/index.ts file:

index.ts
import { Welcome } from "./welcome";
 
export const templates = {
  ...
  [EmailTemplate.WELCOME]: Welcome,
} as const;

That way, it will be available in the sendEmail function, enabling us to send it from the server-side of your application.

import { sendEmail } from "@turbostarter/email/server";
 
sendEmail({
  to: "user@example.com",
  template: EmailTemplate.WELCOME,
  variables: {
    name: "John Doe",
  },
});

Learn more about sending emails in the dedicated section.

Et voilà! You've just added a new email template to your application 🎉

Last updated on

On this page

Ship your startup everywhere. In minutes.Get TurboStarter