npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastmail

v1.0.7

Published

Send emails with pre-built templates in just plenty lines of code

Downloads

3

Readme

FastMail

FastMail is an easy-to-use Node.js package for sending emails using customizable, pre-built, and user-defined templates. It supports both CommonJS and ES Module environments.

With FastMail, you can send professional emails, such as welcome emails, OTPs, password reset emails, and more, by simply using built-in templates or adding your own.

Features

  • Pre-built email templates (e.g., welcome, OTP, password reset).
  • Dynamic template rendering with customizable data.
  • Support for both CommonJS and ES Module environments.
  • Easily add your own custom templates.
  • Works with any SMTP service via Nodemailer.
  • Type-safe template rendering in TypeScript.

Table of Contents

  1. Installation
  2. File Structure
  3. Pre-built Templates
  4. Usage
  5. API Reference
  6. Contributing
  7. License

Installation

NPM

Install via npm:

npm install fastmail

Yarn

Install via yarn:

yarn add fastmail

Pre-built Templates

Test Email

The Test template is used to send a test email to users. It includes the user's name and the date they signed up.

Welcome Email

The Welcome template is used to send a welcome email to new users. It includes the user's name and the date they signed up. includes the user's name and the date they signed up.

OTP Email

The OTP template is used to send a one-time password (OTP) to users for authentication. It includes the OTP code and the expiry time.

Password Reset Email

The Password Reset template is used to send a password reset link to users who have forgotten their password. It includes the reset link and the expiry time.

Verify Email

The Verify Email template is used to send a verification email to users for email verification. It includes the verification link and the expiry time.

Newsletter Email

The Newsletter template is used to send a newsletter email to subscribers. It includes the newsletter content and a call-to-action button.


Usage

Quick Start

  1. Initialize the mail transport: You can use any SMTP provider (e.g., Gmail, SendGrid).
  2. Send an email: You can use pre-built templates or create custom ones.

Here’s a basic example:

import { createMailTransport, sendMail } from "fastmail";
import {Test} from "fastmail/templates";

// Create the mail transport (using Nodemailer)
const mailTransport = createMailTransport({
  host: "smtp.yourmailservice.com",
  port: 587,
  auth: {
    user: "[email protected]",
    pass: "your-email-password",
  },
});

// Send the email
await sendMail(mailTransport, {
  from: "[email protected]",
  to: "[email protected]",
  subject: "Welcome to Our Service!",
  template:Test()
});

Using Pre-built Templates

You can use the pre-built templates like Welcome, OTP, and Password Reset. The renderTemplate function allows you to pass data for dynamic rendering.

Example: Sending an OTP Email

import { renderTemplate, sendMail } from "fastmail";
import {Otp} from "fastmail/templates";


await sendMail(mailTransport, {
  from: "[email protected]",
  to: "[email protected]",
  subject: "Your OTP Code",
  template:Otp({otp: "123456", expiry: "5 minutes"}),
});

Creating Custom Templates

You can easily create your own custom templates and render them in the same way.

  1. Create a new template file in the templates folder (e.g., myCustomTemplate.ts).
// src/templates/myCustomTemplate.ts

export interface MyCustomTemplateParams {
  userName: string;
  customMessage: string;
}

export const myCustomTemplate = ({
  userName,
  customMessage,
}: MyCustomTemplateParams): string => {
  return `
    <html>
      <body>
        <h1>Hello ${userName}!</h1>
        <p>${customMessage}</p>
      </body>
    </html>
  `;
};
  1. Render and send the custom template:
import {  sendMail } from "fastmail";
import { myCustomTemplate } from "./templates/myCustomTemplate";

// Render the custom template
const html = myCustomTemplate();


await sendMail(mailTransport, {
  from: "[email protected]",
  to: "[email protected]",
  subject: "A Special Message",
  html,
});

API Reference

createMailTransport(options)

  • Description: Creates a Nodemailer transport for sending emails.
  • Options: Accepts any Nodemailer transport options.

sendMail(transport, options)

  • Description: Sends an email using the provided transport.
  • Parameters:
    • transport: The mail transport object created using createMailTransport.
    • options: The email options (from, to, subject, html, etc.).

renderTemplate(templateName, data)

  • Description: Renders an email template with dynamic data.
  • Parameters:
    • templateName: The name of the template (e.g., welcome, otp, passwordReset).
    • data: The dynamic data to render in the template.

Contributing

We welcome contributions to the FastMail project! If you have an idea for a new template or feature, please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Commit your changes (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature-branch).
  5. Create a new Pull Request.

Please ensure your changes are well-documented and include tests if applicable.


License

This project is licensed under the MIT License - see the LICENSE file for details.


Example Project

To see how FastMail can be integrated into a full project, check out the example folder in the repository:

├── example
│   ├── index.ts             # Example usage of FastMail
│   └── .env                 # Example environment variables for SMTP credentials

To run the example:

  1. Clone the repository.
  2. Navigate to the example folder.
  3. Install the dependencies:
    npm install
  4. Add your SMTP credentials to the .env file.
  5. Run the example:
    node index.ts

This README file ensures clarity and provides all the essential information about your package, including examples, installation, and usage instructions.