mailtm-proxy
Version:
TypeScript library for interacting with temporary email services like mail.tm and mail.gw with proxy support
228 lines (155 loc) • 5.59 kB
Markdown
A TypeScript library for interacting with temporary email services like mail.tm and mail.gw, with proxy support to bypass rate limits.
```bash
npm install mailtm-proxy
yarn add mailtm-proxy
pnpm add mailtm-proxy
```
- Create random email accounts
- Login to email accounts
- View message list
- View message details
- Delete messages
- Delete email accounts
- Proxy support for requests
- Support for multiple temporary email providers (mail.tm, mail.gw)
- Support for both ESM (ES Modules) and CJS (CommonJS)
## Usage
### Initialization
#### ES Modules (ESM)
```typescript
// Using import with ESM projects
import { CustomMailjs } from 'mailtm-proxy';
// or default import
import CustomMailjs from 'mailtm-proxy';
// Initialize with default configuration (mail.tm, no proxy)
const mailjs = new CustomMailjs();
```
```javascript
// Using require with CommonJS projects
const { CustomMailjs } = require('mailtm-proxy');
// or default import
const CustomMailjs = require('mailtm-proxy');
// Initialize with default configuration (mail.tm, no proxy)
const mailjs = new CustomMailjs();
```
```typescript
// Configuration applies to both ESM and CJS
const mailjs = new CustomMailjs({
provider: 'mail.gw', // 'mail.tm' or 'mail.gw'
proxy: 'http://user:pass@ip:port' // Optional
});
```
The library includes type definitions so you can use it without installing additional @types.
```typescript
import { CustomMailjs } from 'mailtm-proxy';
import type { CreateRandomEmailResponse } from 'mailtm-proxy';
async function createEmail(): Promise<CreateRandomEmailResponse> {
const mailjs = new CustomMailjs();
return await mailjs.createRandomEmail();
}
```
```typescript
// Create email with default configuration
const account = await mailjs.createRandomEmail();
// { id: '...', email: 'random123@domain.com', password: 'abc123' }
// Or create with custom configuration
const account = await mailjs.createRandomEmail({
emailLength: 10, // Email length (default: 15)
passwordLength: 10, // Password length (default: 7)
emailPrefix: 'myemail' // Prefix for email address (optional)
});
```
```typescript
const { token } = await mailjs.login({
email: 'your-email@domain.com',
password: 'your-password'
});
```
```typescript
const messages = await mailjs.getMessages();
```
```typescript
const messageDetails = await mailjs.getMessage('message-id');
```
```typescript
await mailjs.deleteMessage('message-id');
```
```typescript
await mailjs.deleteAccount('account-id');
```
```typescript
const accountInfo = await mailjs.me();
```
This library is bundled to support both modern and older environments:
- **ES Modules**: For modern projects, Node.js >= 14
- **CommonJS**: Backward compatibility with older Node.js versions
The package automatically detects and selects the appropriate format based on how you import/require it.
Initialize a new instance of CustomMailjs.
| Parameter | Type | Description |
|---------|------|-------|
| options | Object | Configuration options |
| options.provider | string | Mail service provider ('mail.tm' or 'mail.gw'), default: 'mail.tm' |
| options.proxy | string | Proxy URL to use for requests (optional) |
#### getDomains()
Get a list of available domains.
**Returns**: `Promise<string[]>` - List of domains.
#### createRandomEmail(params)
Create a random email account.
| Parameter | Type | Description |
|---------|------|-------|
| params | Object | Options |
| params.emailLength | number | Email length, default: 15 |
| params.passwordLength | number | Password length, default: 7 |
| params.emailPrefix | string | Prefix for email address (optional) |
**Returns**: `Promise<CreateRandomEmailResponse>` - Created account information.
#### login(params)
Authenticate and get token.
| Parameter | Type | Description |
|---------|------|-------|
| params | Object | Login parameters |
| params.email | string | Email address to login |
| params.password | string | Password to login |
**Returns**: `Promise<LoginResponse>` - Authentication token.
#### getMessages()
Get all messages from authenticated account.
**Returns**: `Promise<GetMessagesResponse[]>` - List of messages.
#### getMessage(id)
Get a specific message by ID.
| Parameter | Type | Description |
|---------|------|-------|
| id | string | ID of message to retrieve |
**Returns**: `Promise<GetMessageResponse>` - Message details.
#### deleteMessage(id)
Delete a specific message by ID.
| Parameter | Type | Description |
|---------|------|-------|
| id | string | ID of message to delete |
**Returns**: `Promise<void>` - No data returned on success.
#### deleteAccount(id)
Delete authenticated account by ID.
| Parameter | Type | Description |
|---------|------|-------|
| id | string | ID of account to delete |
**Returns**: `Promise<void>` - No data returned on success.
#### me()
Get authenticated user account detailed information.
**Returns**: `Promise<MeResponse>` - Account details.