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

@ahmedayob/email-toolkit

v0.2.2

Published

A powerful and flexible toolkit for building, validating, and processing emails with TypeScript. This library offers utilities for handling email headers, attachments, and encoding, making it easier to compose and manage emails programmatically.

Downloads

23

Readme

@ahmedayob/email-toolkit

A powerful and flexible toolkit for building, validating, and processing emails with TypeScript. This library offers utilities for handling email headers, attachments, and encoding, making it easier to compose and manage emails programmatically.

Features

  • Email Header Management: Easily create and manage email headers.
  • Attachment Handling: Add, validate, and format email attachments.
  • Encoding Utilities: Encode content in Base64 for attachments.
  • Validation: Ensure email content, headers, and attachments are valid with built-in validation schemas.
  • Custom Error Handling: Handle email-related errors with a custom EmailError class.

Installation

To install the toolkit, use npm or yarn:

npm install @ahmedayob/email-toolkit
# or
yarn add @ahmedayob/email-toolkit

Usage

Here's a quick guide on how to use the library:

Email Header

Create and configure email headers:

import { EmailBuilderHeader } from "@ahmedayob/email-toolkit";

const header = new EmailBuilderHeader();
header
  .setFrom("ahmed <[email protected]>")
  .setTo("ahmed <[email protected]>")
  .setCc("ahmed <[email protected]>")
  .setBcc("ahmed <[email protected]>")
  .setSubject("This is a test email subject")
  .setInReplyTo("[email protected]")
  .setMIMEVersion("1.0")
  .setContentTransferEncoding("quoted-printable")
  .setContentType("text/html")
  .setCharset("utf-8");

Email Attachment

Add attachments to your email:

import { EmailBuilderAttachment, Base64 } from "@ahmedayob/email-toolkit";

const attachment = new EmailBuilderAttachment();
attachment.addAttachment({
  headers: {
    "Content-Type": 'text/plain; charset="utf-8"',
    "Content-Transfer-Encoding": "base64",
    "Content-Disposition": 'attachment; filename="test.txt"',
  },
  size: 1234,
  filename: "test.txt",
  mimeType: "text/plain",
  attachmentId: "1234",
  attachmentContent: Base64.encodeToBase64(
    "This is the content of the attachment."
  ),
});

Building the Email

Combine headers and attachments to create the final email:

import { EmailBuilder } from "@ahmedayob/email-toolkit";

const email = new EmailBuilder();
email.messagebody = "<p>This is the message body</p>";

const finalEmail = email.getRawMessage(header.headers, attachment.attachments);
console.log(finalEmail);

Encoding and Signature

Generate base64-encoded messages and email signatures:

import { EmailBuilder } from "@ahmedayob/email-toolkit";

const email = new EmailBuilder();
email.messagebody = "<p>This is the message body</p>";

const encodedMessage = email.getEncodedMessage(
  header.headers,
  attachment.attachments
);
console.log(encodedMessage);

email.setSignature({
  url: "https://github.com/wildduck2",
  name: "Ahmed Ayob",
});

const signature = email.getSignature({
  from: "[email protected]",
  url: "https://github.com/wildduck2",
  name: "Ahmed Ayob",
});
console.log(signature.join("\n"));

API

EmailBuilderHeader

  • setFrom(address: string): Sets the "From" header.
  • setTo(address: string): Sets the "To" header.
  • setCc(address: string): Sets the "Cc" header.
  • setBcc(address: string): Sets the "Bcc" header.
  • setSubject(subject: string): Sets the email subject.
  • setInReplyTo(messageId: string): Sets the "In-Reply-To" header.
  • setMIMEVersion(version: string): Sets the "MIME-Version" header.
  • setContentTransferEncoding(encoding: string): Sets the "Content-Transfer-Encoding" header.
  • setContentType(type: string): Sets the "Content-Type" header.
  • setCharset(charset: string): Sets the charset.

EmailBuilderAttachment

  • addAttachment(attachment: AttachmentType): Adds an attachment.
  • getAttachment(): Retrieves the formatted attachments.

EmailBuilder

  • messagebody: Sets the body of the email.
  • getRawMessage(headers: HeadersType, attachments?: AttachmentType[]): Gets the raw email message.
  • getEncodedMessage(headers: HeadersType, attachments?: AttachmentType[]): Gets the base64-encoded email message.
  • getSignature(signatureDetails: GetSignatureType): Generates a formatted signature block.
  • setSignature(signatureDetails: NonNullableType<Omit<GetSignatureType, "from">>): Sets the email signature details.

Base64

  • encodeToBase64(data: string): Encodes data to Base64.

EmailError

  • name: The name of the error.
  • description: A description of the error.
  • constructor({ message, description }: { message: string; description: string }): Constructs a new EmailError.

Validation

Validation schemas are available to ensure data correctness:

  • HeadersTypeSchema: Validates email headers.
  • AttachmentHeaderSchema: Validates attachment headers.
  • StringSchema: Validates strings.
  • ContentTransferEncodingSchema: Validates content transfer encodings.
  • ContentTypeSchema: Validates content types.
  • CharsetTypeSchema: Validates charset types.

Contributing

Contributions are welcome! Please open issues and pull requests on the GitHub repository.

License

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