https://github.com/wildduck2/email-toolkit
it's a TypeScript library for building and manipulating MIME (Multipurpose Internet Mail Extensions) messages. This library provides an easy-to-use API for creating, modifying, and encoding MIME messages, making it ideal for email and other internet-based communication systems.
https://github.com/wildduck2/email-toolkit
base64 decoding email encoding mimebundle textdecoder textencoder typescript uri-encoding utility
Last synced: 29 days ago
JSON representation
it's a TypeScript library for building and manipulating MIME (Multipurpose Internet Mail Extensions) messages. This library provides an easy-to-use API for creating, modifying, and encoding MIME messages, making it ideal for email and other internet-based communication systems.
- Host: GitHub
- URL: https://github.com/wildduck2/email-toolkit
- Owner: wildduck2
- License: mit
- Created: 2024-07-30T03:51:11.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-25T10:09:55.000Z (8 months ago)
- Last Synced: 2025-08-08T13:19:08.707Z (6 months ago)
- Topics: base64, decoding, email, encoding, mimebundle, textdecoder, textencoder, typescript, uri-encoding, utility
- Language: TypeScript
- Homepage:
- Size: 201 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
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:
```bash
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:
```typescript
import { EmailBuilderHeader } from "@ahmedayob/email-toolkit";
const header = new EmailBuilderHeader();
header
.setFrom("ahmed ")
.setTo("ahmed ")
.setCc("ahmed ")
.setBcc("ahmed ")
.setSubject("This is a test email subject")
.setInReplyTo("ahmed@gmail.com")
.setMIMEVersion("1.0")
.setContentTransferEncoding("quoted-printable")
.setContentType("text/html")
.setCharset("utf-8");
```
### Email Attachment
Add attachments to your email:
```typescript
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:
```typescript
import { EmailBuilder } from "@ahmedayob/email-toolkit";
const email = new EmailBuilder();
email.messagebody = "
This is the message body
";
const finalEmail = email.getRawMessage(header.headers, attachment.attachments);
console.log(finalEmail);
```
### Encoding and Signature
Generate base64-encoded messages and email signatures:
```typescript
import { EmailBuilder } from "@ahmedayob/email-toolkit";
const email = new EmailBuilder();
email.messagebody = "
This is the message body
";
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: "ahmed@example.com",
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>): 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](https://github.com/ahmedayob/email-toolkit).
## License
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.