0.0.3 • Published 9 months ago

@swiftpost/mail v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@swiftpost/mail

Overview

Allows you to compose and send swift html emails, with a special focus on authentication emails. It provides a flexible template system and integrates with Nodemailer for email delivery.

Installation

npm install @swiftpost/mail

Usage

Configuration

Configure SwiftPost with your SMTP settings.

By default, it will use the environment variables SMTP_HOST, SMTP_PORT, SMTP_SECURE, SMTP_USER, and SMTP_PASS to configure the SMTP connection. If SMTP_URL is set in the environment, it will be used instead. If SMTP_FROM is set, it will be used as the default sender email address.

import { SwiftPost } from "@swiftpost/mail";

const swiftPost = new SwiftPost({
  host: "smtp.swiftpost.io",
  port: 587,
  secure: false,
  auth: {
    user: "user@swiftpost.io",
    pass: "password",
  },
  from: "no-reply@swiftpost.io",
});

Sending Emails

Use the provided methods to send different types of emails.

await swiftPost.sendWelcomeEmail(
  { to: "hi@swiftpost.io" },
  {
    name: "Jon Manga",
    companyName: "SwiftPost",
    link: "https://swiftpost.io/welcome",
  }
);

SwiftPost methods

MethodDescription
sendWelcomeEmailGenerates and sends a welcome email.
getWelcomeEmailHtmlRenders and returns the html for a welcome email.
sendConfirmationEmailGenerates and sends a confirmation email.
getConfirmationEmailHtmlRenders and returns the html for a confirmation email.
sendPasswordResetEmailGenerates and sends a password reset email.
getPasswordResetEmailHtmlRenders and returns the html for a password reset email.
sendPasswordlessLoginEmailGenerates and sends a passwordless login email.
getPasswordlessLoginEmailHtmlRenders and returns the html for a passwordless login email.
sendAcceptInvitationEmailGenerates and sends an accept invitation email.
getAcceptInvitationEmailHtmlRenders and returns the html for an accept invitation email.
sendPasswordInvitationEmailGenerates and sends a password invitation email.
getPasswordInvitationEmailHtmlRenders and returns the html for a password invitation email.
sendWelcomeWithCredentialsEmailGenerates and sends a welcome with credentials email.
getWelcomeWithCredentialsEmailHtmlRenders and returns the html for a welcome with credentials email.
sendMailSends an email using nodemailer's sendMail method.

SwiftPost Options

SwiftPost options extend the Nodemailer SMTP options.

OptionTypeDescription
hoststringThe hostname or IP address to connect to (e.g., "smtp.swiftpost.io").
portnumberThe port to connect to (e.g., 587).
securebooleanIf true, the connection will use TLS when connecting to the server.
authobjectAuthentication object containing user and pass properties.
fromstringThe default email address to use as the sender.
urlstringSMTP URL to connect to.
transportobjectCustom transport object or string.
templatesDirstringDirectory path for email templates. You can specify your own path is you want to override the templates
debugbooleanIf true, enables debug mode for logging.

You also have access to the @swiftpost/templ package to use and render your own templates.

Example of a custom template:

const templatePath = path.join(__dirname, "templates", "my-template.html");
const html = await swiftPost.templ.render(templatePath, {
  name: "Jon Manga",
});
const template = `<h1>Hello {{ name }}</h1>`;
const html = await swiftPost.templ.render(template, {
  name: "Jon Manga",
});
await swiftPost.sendMail({
  to: "hi@swiftpost.io",
  subject: "My Template",
  html,
});

You can also have access to the Templ class directly to use your own templates.

const templ = new Templ({
  baseDir: path.join(__dirname, "templates"),
});

NestJS

See the @swiftpost/nest package for more information on how to use SwiftPost with NestJS.

License

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

0.0.3

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago