232 lines
5.1 KiB
Markdown
232 lines
5.1 KiB
Markdown
# Email Rules Sync System
|
|
|
|
This script synchronizes email rules from AWS DynamoDB to the mail server, enabling:
|
|
- **Out-of-Office Auto-Replies** (Sieve scripts)
|
|
- **Email Forwarding** (Postfix virtual aliases)
|
|
|
|
## How It Works
|
|
|
|
```
|
|
┌─────────────┐
|
|
│ DynamoDB │ ← Rules stored here (via Web UI)
|
|
│ email-rules │
|
|
└──────┬──────┘
|
|
│
|
|
│ Sync Script (every 5 minutes)
|
|
↓
|
|
┌─────────────────────────────────────┐
|
|
│ Mail Server (Docker) │
|
|
│ ┌────────────┐ ┌───────────────┐ │
|
|
│ │ Sieve │ │ Postfix │ │
|
|
│ │ Scripts │ │Virtual Aliases│ │
|
|
│ │ (OOO) │ │ (Forwarding) │ │
|
|
│ └────────────┘ └───────────────┘ │
|
|
└─────────────────────────────────────┘
|
|
```
|
|
|
|
## Setup
|
|
|
|
### 1. Install Dependencies
|
|
|
|
```bash
|
|
cd /home/timo/config-email/sync
|
|
npm install
|
|
```
|
|
|
|
### 2. Configure Environment
|
|
|
|
The `.env` file is already configured with:
|
|
- AWS credentials
|
|
- DynamoDB table name
|
|
- Mail server paths
|
|
|
|
### 3. Test Manual Sync
|
|
|
|
```bash
|
|
npm start
|
|
```
|
|
|
|
You should see output like:
|
|
```
|
|
🚀 Starting email rules sync...
|
|
📊 DynamoDB Table: email-rules
|
|
🌍 Region: us-east-2
|
|
|
|
📥 Fetching rules from DynamoDB...
|
|
✅ Found 2 email rules
|
|
|
|
📝 Processing Sieve scripts (Out-of-Office)...
|
|
✅ Created Sieve script for support@qrmaster.net
|
|
✅ Processed 1 Sieve scripts
|
|
|
|
📮 Updating virtual aliases (Forwarding)...
|
|
✅ Found 0 forwarding rules
|
|
✅ Updated virtual aliases
|
|
|
|
🔄 Applying changes to mail server...
|
|
✅ Postfix reloaded
|
|
✅ Dovecot reloaded
|
|
|
|
✨ Sync completed successfully!
|
|
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
Total Rules: 2
|
|
OOO Active: 1
|
|
Forwarding Active: 0
|
|
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
```
|
|
|
|
### 4. Install as Cron Job (Automatic Sync)
|
|
|
|
```bash
|
|
./install-cron.sh
|
|
```
|
|
|
|
This will:
|
|
- Run sync every 5 minutes
|
|
- Log to `/tmp/email-rules-sync.log`
|
|
|
|
## Usage
|
|
|
|
### View Sync Logs
|
|
|
|
```bash
|
|
tail -f /tmp/email-rules-sync.log
|
|
```
|
|
|
|
### Manual Sync
|
|
|
|
```bash
|
|
cd /home/timo/config-email/sync
|
|
npm start
|
|
```
|
|
|
|
### Check Current Cron Jobs
|
|
|
|
```bash
|
|
crontab -l
|
|
```
|
|
|
|
### Remove Cron Job
|
|
|
|
```bash
|
|
crontab -l | grep -v "email-rules-sync" | crontab -
|
|
```
|
|
|
|
## What Gets Generated
|
|
|
|
### 1. Sieve Scripts (Out-of-Office)
|
|
|
|
Location: `/home/timo/docker-mailserver/docker-data/dms/mail-data/{domain}/{user}/home/.dovecot.sieve`
|
|
|
|
Example for `support@qrmaster.net`:
|
|
```sieve
|
|
require ["vacation", "variables"];
|
|
|
|
# Auto-Reply / Out-of-Office
|
|
# Generated by Email Rules Sync System
|
|
# Last updated: 2025-12-27T12:00:00.000Z
|
|
|
|
if true {
|
|
vacation
|
|
:days 1
|
|
:subject "Out of Office"
|
|
"I am currently out of office and will respond when I return.";
|
|
}
|
|
```
|
|
|
|
### 2. Virtual Aliases (Forwarding)
|
|
|
|
Location: `/home/timo/docker-mailserver/docker-data/dms/config/postfix-virtual.cf`
|
|
|
|
Example:
|
|
```
|
|
# Virtual Aliases - Email Forwarding
|
|
# Generated by Email Rules Sync System
|
|
# Last updated: 2025-12-27T12:00:00.000Z
|
|
|
|
# Forwarding for support@qrmaster.net
|
|
support@qrmaster.net forward1@example.com,forward2@example.com
|
|
```
|
|
|
|
## DynamoDB Schema
|
|
|
|
The sync script expects this schema:
|
|
|
|
```javascript
|
|
{
|
|
email_address: "support@qrmaster.net", // Primary Key
|
|
ooo_active: true, // Enable/disable auto-reply
|
|
ooo_message: "I am out of office...", // Auto-reply message
|
|
ooo_content_type: "text", // "text" or "html"
|
|
forwards: ["user@example.com"], // Array of forward addresses
|
|
last_updated: "2025-12-27T12:00:00.000Z" // Timestamp
|
|
}
|
|
```
|
|
|
|
## Troubleshooting
|
|
|
|
### Script fails to connect to DynamoDB
|
|
|
|
Check AWS credentials in `.env`:
|
|
```bash
|
|
cat .env | grep AWS
|
|
```
|
|
|
|
### Sieve scripts not working
|
|
|
|
1. Check script was created:
|
|
```bash
|
|
ls -la /home/timo/docker-mailserver/docker-data/dms/mail-data/qrmaster.net/support/home/
|
|
```
|
|
|
|
2. Check Dovecot logs:
|
|
```bash
|
|
docker logs mailserver-new 2>&1 | grep -i sieve
|
|
```
|
|
|
|
### Forwarding not working
|
|
|
|
1. Check virtual aliases file:
|
|
```bash
|
|
cat /home/timo/docker-mailserver/docker-data/dms/config/postfix-virtual.cf
|
|
```
|
|
|
|
2. Check Postfix logs:
|
|
```bash
|
|
docker logs mailserver-new 2>&1 | grep -i virtual
|
|
```
|
|
|
|
3. Reload Postfix manually:
|
|
```bash
|
|
docker exec mailserver-new postfix reload
|
|
```
|
|
|
|
## Architecture
|
|
|
|
```
|
|
Web UI (React)
|
|
↓
|
|
Backend API (Express)
|
|
↓
|
|
DynamoDB (email-rules)
|
|
↓
|
|
Sync Script (Node.js) ← You are here
|
|
↓
|
|
Mail Server (Dovecot + Postfix)
|
|
```
|
|
|
|
## Files
|
|
|
|
- `sync.js` - Main sync script
|
|
- `package.json` - Dependencies
|
|
- `.env` - Configuration
|
|
- `install-cron.sh` - Cron job installer
|
|
- `README.md` - This file
|
|
|
|
## Support
|
|
|
|
For issues, check:
|
|
1. Sync logs: `/tmp/email-rules-sync.log`
|
|
2. Mail server logs: `docker logs mailserver-new`
|
|
3. DynamoDB table: AWS Console → DynamoDB → email-rules
|