# IMAP JSON Fetcher

## Setup

Before running the `imap-json-fetcher`, ensure that you have configured the necessary environment variables. This script uses these variables to securely connect to your IMAP server and fetch emails from a specified mailbox.

### Required Environment Variables

Set the following environment variables in your system:

- `IMAP_HOST`: The address of your IMAP server, including the port if necessary (e.g., `imap.example.com:993`).
- `IMAP_USERNAME`: Your email account username.
- `IMAP_PASSWORD`: Your email account password.
- `IMAP_FOLDER`: The specific folder to monitor for new emails. If not specified, the script defaults to monitoring the "INBOX".

You can set these variables in a Unix-like environment using the following commands:

```bash
export IMAP_HOST="your_imap_server_address"
export IMAP_USERNAME="your_username"
export IMAP_PASSWORD="your_password"
export IMAP_FOLDER="your_folder_name"  # Optional, defaults to "INBOX"
```

### Running the Script

Navigate to the `dist` directory where the binary is compiled, and run the script using the following command:

```bash
./imap-json-fetcher
```

### Docker Container Example

For running in a Docker container, you might set it up like this, passing the environment variables into your container. Replace specifics to fit your architecture and environment settings:

```bash
docker run --env IMAP_HOST=$IMAP_HOST \
           --env IMAP_USERNAME=$IMAP_USERNAME \
           --env IMAP_PASSWORD=$IMAP_PASSWORD \
           --env IMAP_FOLDER=$IMAP_FOLDER \
           --name imap-json-fetcher \
           your_docker_image
```

This command assumes that you've built a Docker image named `your_docker_image` that encapsulates your application.

## Output

The `imap-json-fetcher` listens for new emails arriving in the specified IMAP folder. For each new email, it extracts relevant data and saves this information in separate JSON files. The inclusion of the `IMAP_FOLDER` environment variable allows flexibility in choosing which folder to monitor, beyond just the default "INBOX".

### File Naming Convention

Output files are named using a timestamp to ensure uniqueness: `email-yyyyMMdd-HHmmss.json`. This format prevents overwriting files and helps in organizing data chronologically.

## Flexibility in Folder Selection

The `IMAP_FOLDER` environment variable provides users the flexibility to monitor various folders within their email account, such as "Sent", "Archive", or custom labels, depending on their specific requirements.
