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.
- 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.
Install via npm:
npm install fastmail
Install via yarn:
yarn add fastmail
The Test template is used to send a test email to users. It includes the user's name and the date they signed up.
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.
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.
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.
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.
The Newsletter template is used to send a newsletter email to subscribers. It includes the newsletter content and a call-to-action button.
- Initialize the mail transport: You can use any SMTP provider (e.g., Gmail, SendGrid).
- 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: "your-email@example.com",
pass: "your-email-password",
},
});
// Send the email
await sendMail(mailTransport, {
from: "your-email@example.com",
to: "recipient@example.com",
subject: "Welcome to Our Service!",
template:Test()
});
You can use the pre-built templates like Welcome, OTP, and Password Reset. The renderTemplate
function allows you to pass data for dynamic rendering.
import { renderTemplate, sendMail } from "fastmail";
import {Otp} from "fastmail/templates";
await sendMail(mailTransport, {
from: "your-email@example.com",
to: "john@example.com",
subject: "Your OTP Code",
template:Otp({otp: "123456", expiry: "5 minutes"}),
});
You can easily create your own custom templates and render them in the same way.
- 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>
`;
};
- 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: "your-email@example.com",
to: "jane@example.com",
subject: "A Special Message",
html,
});
- Description: Creates a Nodemailer transport for sending emails.
- Options: Accepts any Nodemailer transport options.
- Description: Sends an email using the provided transport.
- Parameters:
transport
: The mail transport object created usingcreateMailTransport
.options
: The email options (from
,to
,subject
,html
, etc.).
- 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.
We welcome contributions to the FastMail project! If you have an idea for a new template or feature, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature-branch
). - Create a new Pull Request.
Please ensure your changes are well-documented and include tests if applicable.
This project is licensed under the MIT License - see the LICENSE file for details.
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:
- Clone the repository.
- Navigate to the example folder.
- Install the dependencies:
npm install
- Add your SMTP credentials to the
.env
file. - 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.