# @bobfrankston/mailsend

Queue-based mail sender with SMTP and OAuth2 support. Messages are queued as RFC 822 `.ltr` files and sent via SMTP with automatic retry.

Each account gets its own queue, sent log, and fail directory.

## Install

```bash
npm install @bobfrankston/mailsend
# or globally for CLI:
npm install -g @bobfrankston/mailsend
```

## Directory Structure

```
{baseDir}/
  {accountId}/
    queue/        pending .ltr files
    sent/         sent messages (YYYY/MM/DD/) — optional
    fail/         failed messages for retry
```

## Library Usage

```typescript
import { initMailSend, queueMessage } from "@bobfrankston/mailsend";

// Initialize once at startup
initMailSend({
    baseDir: "./mailqueue",
    defaultAccount: "work",
    logSent: true,
    retryIntervalMs: 300000,  // 5 min
    accounts: {
        work: {
            host: "smtp.example.com",
            port: 587,
            auth: "password",
            user: "bob",
            password: "secret",
        },
        gmail: {
            host: "smtp.gmail.com",
            port: 587,
            auth: "oauth2",
            user: "bob@gmail.com",
            tokenProvider: async () => getOAuthToken(), // your OAuth2 token function
        },
    },
});

// Queue a message — returns immediately, sent in background
queueMessage({
    from: "Bob <bob@example.com>",
    to: ["alice@example.com"],
    subject: "Hello",
    text: "Plain text body",
    // accountId: "work",   // optional, uses defaultAccount
});

// Queue HTML email on a specific account
queueMessage({
    from: "Bob <bob@gmail.com>",
    to: ["alice@example.com"],
    subject: "Hello",
    html: "<p>Rich <b>HTML</b> body</p>",
    accountId: "gmail",
});
```

## CLI Usage

Create a `mailsend.json` config file:

```json
{
    "baseDir": "./mailqueue",
    "defaultAccount": "work",
    "logSent": true,
    "accounts": {
        "work": {
            "host": "smtp.example.com",
            "port": 587,
            "auth": "password",
            "user": "bob",
            "password": "secret"
        }
    }
}
```

Then:

```bash
# Send a message (queued to default account)
mailsend --to alice@example.com --subject "Hello" --body "Message"

# Send via a specific account queue
mailsend -q gmail --to alice@example.com --subject "Hello" --body "Message"

# Check queue status
mailsend --status

# Process queues now (retry failed, send pending)
mailsend --process

# Use a different config file
mailsend --config /path/to/mailsend.json --to ...
```

## Queue Format

Messages are stored as `.ltr` files — standard RFC 822 format with additional `X-MailSend-Account` header for routing.

Filenames: `YYYYMMDD_HHMMSS-NNNN.ltr`

## Retry

Failed messages move to `{accountId}/fail/YYYY/MM/DD/`. The background worker retries them at the configured interval (default 5 minutes) by moving them back to the queue.

## OAuth2 (Gmail)

For Gmail SMTP, set `auth: "oauth2"` and provide a `tokenProvider` function that returns a current access token. This integrates with `@bobfrankston/oauthsupport` or any OAuth2 flow.

## Sent Log

When `logSent: true`, sent messages are preserved in `{accountId}/sent/YYYY/MM/DD/` for debugging. When false, sent messages are deleted.

## License

MIT
