Mailing

website·Indexed 32 days ago

https://www.mailing.run/docs

An open source tool for building, testing, and sending compatible, consistent emails using React components. It features a REST API for email templating, support for Remix and Turborepo, and a platform for managing mailing lists and subscription preferences.

Tokens
5.6K
Snippets
32
Records
46
Agent score
64%

What's inside Mailing

  1. Overview of Mailing for React apps

    Mailing is an open-source tool that allows developers to build, preview, and send consistent emails from any Node app using React. It integrates MJML-React support to ensure compatibility across email clients without requiring manual table layouts. The system includes a preview server with hot reloading and is transport-agnostic, utilizing Nodemailer for sending emails.
  2. Overview of Mailing Platform

    Mailing Platform is the advanced, self-hosted version of Mailing designed for teams that code. It provides a comprehensive system for sending both marketing and transactional emails, featuring mailing lists and easy unsubscribe links. Unlike the free version, Mailing Platform requires a deployment, an associated database, and an API key.
  3. Develop and send emails with Mailing

    Mailing works by adding an emails directory to a Node application where templates are stored under source control. These templates can import shared constants, images, and components. To send emails, the sendMail function renders React MJML templates to HTML and sends them via a transport provider. Because it is built with Nodemailer, any options supported by Nodemailer can be passed into the sendMail function.
  4. Initialize a Mailing production account

    After deploying and connecting a database, you must secure the instance by creating an account. Navigate to /signup on your production instance. This process secures the deployment, creates a Default list, and generates an API key for the /api/sendMail endpoint. Note that currently, only one user can sign up per instance.
  5. Export and render email templates without Mailing

    Mailing provides zero lock-in for its development environment and sendMail proxy. If you need to switch to a different sending system, you can either export the preview HTML or use the render function to process the React templates.
  6. Configure email transport and send emails

    Configure your email transport (defaulting to nodemailer's SMTP) and defaultFrom in emails/index.ts. To send an email, import the sendMail function and provide the recipient details and the React component to be rendered as the email body.
    // emails/index.ts - Example SendGrid transport
    import nodemailer from 'nodemailer';
    
    const transport = nodemailer.createTransport({
      host: "smtp.sendgrid.net",
      port: 587,
      auth: {
        user: "apikey",
        pass: process.env.SEND_GRID_KEY,
      },
    });
    
    // Sending an email
    import sendMail from "./path/to/emails";
    import AccountCreated from "emails/AccountCreated";
    
    await sendMail({
      subject: "My First Email",
      to: "tester@example.com",
      cc: "tester+cc@example.com",
      bcc: ["tester+bcc@example.com", "tester+bcc2@example.com"],
      component: <AccountCreated firstName="Amelita" />,
    });
  7. Set up a shared email package across multiple apps in Turborepo

    Use this setup when multiple apps in your monorepo need to send the same set of emails. This involves creating a dedicated internal package to house the templates.

    1. Create an internal package at packages/emails and install mailing, mailing-core, next, react, and react-dom. If using TypeScript, also install @types/react and @types/react-dom.
    2. Create packages/emails/index.ts to export your templates and emails/index.
    3. Configure the package.json for packages/emails with the correct entrypoint and add a dev script to start the preview server alongside your apps.
    4. Import the packages/emails package into your target apps. For Next.js apps, add the package to transpilePackages in next.config.js to allow imports in server-side API routes.
    // next.config.js
    module.exports = {
      transpilePackages: ['@repo/emails'],
    };
  8. Send emails using the sendMail function

    The sendMail function allows you to send emails by passing a React component, which is automatically rendered to HTML. Because it uses secrets, sendMail must be called from the back-end (e.g., a Next.js API route). It is an asynchronous function and should be used with await.
    import sendMail from "path/to/emails";
    
    await sendMail({
      to: "ok@ok.com",
      subject: "Hello",
      component: <MyTemplate data={myData} />
    });
  9. Deploy Mailing server to Vercel

    Mailing is a Next.js application and can be deployed to Vercel by connecting a git repository and configuring the build settings.
    # Build Command
    npx mailing server build
    
    # Output Directory
    .mailing/.next
  10. Enable and set up Mailing Platform

    To enable Mailing Platform, you must deploy the Mailing software and connect it to a database to generate an API key. After deployment, you can utilize Lists to manage user subscription preferences.
  11. Install and initialize Mailing

    Mailing allows you to build email templates using React components and MJML. To get started, install the core dependencies and run the initialization command to scaffold the project structure.
    # Install dependencies
    yarn add mailing-core next react react-dom
    yarn add --dev mailing
    
    # Start preview server and scaffold emails directory
    npx mailing
    
    # Force TypeScript mode during scaffolding
    npx mailing --typescript
  12. Implement an unsubscribe link in email templates

    To allow users to manage their email preferences, use the EMAIL_PREFERENCES_URL constant from the mailing-core package in your template. When sendMail is called, Mailing automatically substitutes this constant with a unique link to the recipient's email settings page, where they can subscribe or unsubscribe from lists they have previously received mail from.
    import { EMAIL_PREFERENCES_URL } from "mailing-core";
    
    // Inside your component/template
    <a href={EMAIL_PREFERENCES_URL}>Unsubscribe</a>