# Cron Jobs Documentation

## Newsletter Queue Processor

**File:** `cron/newsletter_send.php`

**Purpose:** Processes the newsletter email queue in batches, sending pending emails to subscribers.

**Recommended Schedule:** Every 5 minutes

### XAMPP / Local Development

On Windows, use Task Scheduler:

1. Open Task Scheduler → Create Basic Task
2. Name: "AI Web Reporter Newsletter"
3. Trigger: Daily, repeat every 5 minutes
4. Action: Start a program
   - Program: `C:\xampp\php\php.exe`
   - Arguments: `C:\xampp\htdocs\AiWebReporter\cron\newsletter_send.php`

On Linux/macOS XAMPP:
```bash
crontab -e
*/5 * * * * /opt/lampp/bin/php /opt/lampp/htdocs/AiWebReporter/cron/newsletter_send.php >> /tmp/newsletter.log 2>&1
```

### cPanel

cPanel → Cron Jobs → Add New:

- Schedule: Every 5 minutes (`*/5 * * * *`)
- Command:
```
/usr/local/bin/php /home/youruser/public_html/cron/newsletter_send.php >> /home/youruser/logs/newsletter.log 2>&1
```

Find your PHP path with: `which php` via SSH.

### Configuration

The script accepts a `batch` query parameter (URL) or defaults to 20:

| Parameter | Default | Max | Description |
|-----------|---------|-----|-------------|
| batch_size | 20 | 100 | Emails processed per run |

### Behavior

1. Fetches up to `batch_size` pending queue items
2. Sends each email via PHPMailer/SMTP
3. Marks items as `sent` or `failed` (with error message)
4. Retries failed items up to 3 times
5. Updates campaign log counts when all items processed
6. Outputs processing summary to stdout (captured by log redirect)

### Manual Trigger

From the admin panel: **Newsletter Logs → Process Queue** button.

This calls the same processing logic via HTTP, limited to admin-authenticated users.

### Monitoring

- Check campaign status in Admin → Newsletter Logs
- Each campaign shows sent/failed counts
- Click a campaign to see per-recipient delivery status
- Failed items show error messages for debugging
