1

I using Next Js for front end, Nest Js for back end and AWS SES v2 for email service. Currently I am sending emails using EJS email templates that I made in back end. I would like to switch to React Emails. But React emails is generated at front end and use JSX, so how do I implement React Email in my project?

1
  • Are you confused how React Email uses React like syntax so it's only usable on the FrontEnd? Commented Oct 16, 2023 at 12:04

1 Answer 1

3

Installation & Basic Setup

React Email uses React for its syntax. You can use React Email in your backend like so:

  1. Create something like an emails folder in your backend.
  2. Install the required dependencies for it in your top level backend folder using npm install react-email @react-email/button @react-email/html -E
  3. Add an email:dev script to your backend package.json like so:
{
  "scripts": {
    "dev": "email dev"
  }
}
  1. Create a demo email in your previously created emails folder like so:
import { Button } from "@react-email/button";
import { Html } from "@react-email/html";
import * as React from "react";

export default function Email() {
  return (
    <Html>
      <Button
        href="https://example.com"
        style={{ background: "#000", color: "#fff", padding: "12px 20px" }}
      >
        Click me
      </Button>
    </Html>
  );
}
  1. Preview using npm run emails:dev

Sending emails

After all this, you have to actually send the emails. Follow these steps to get started sending them:

  1. Create an API route called something like /send-email
  2. In the API route, use the following code and adjust to your needs.
import { render } from '@react-email/render';
import { SES } from '@aws-sdk/client-ses';
import Email from '../../emails/email';

const ses = new SES({ region: process.env.AWS_SES_REGION })

const emailHtml = render(Email({ url:"https://example.com" }));

const params = {
  Source: '[email protected]',
  Destination: {
    ToAddresses: ['[email protected]'],
  },
  Message: {
    Body: {
      Html: {
        Charset: 'UTF-8',
        Data: emailHtml,
      },
    },
    Subject: {
      Charset: 'UTF-8',
      Data: 'hello world',
    },
  },
};

await ses.sendEmail(params);

Sources:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.