> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://help.swellsystem.com/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# Email Integration

# Email Integration

Connect your email accounts to automatically convert emails into support tickets.

## Setting Up Email Integration

### 1. Configure Inbox Email Settings
1. Go to Settings → Support → Inboxes
2. Select an inbox to configure
3. Enter email account details:
   - **Email Address**: The email address to monitor
   - **Protocol**: IMAP or POP3
   - **Host**: Mail server hostname
   - **Port**: Server port (usually 993 for IMAP SSL, 143 for IMAP, 995 for POP3)
   - **Encryption**: SSL/TLS or None
   - **Username**: Email account username
   - **Password**: Email account password or app password

### 2. Test Connection
1. Click "Test Connection" button
2. Verify connection is successful
3. Save settings

### 3. Enable Auto-Sync
Email sync runs automatically every 5 minutes via scheduled command. You can also manually sync:
```bash
php artisan tickets:sync-inboxes
```

Or sync a specific inbox:
```bash
php artisan tickets:sync-inboxes --inbox=1
```

## How Email-to-Ticket Works

### Automatic Conversion
1. Email is received in the configured inbox
2. System checks if email is from an existing customer
3. If customer exists, email is added to existing ticket thread
4. If no matching thread, new ticket is created
5. Customer is automatically created if email doesn't match existing contact

### Email Threading
The system uses email headers to maintain conversation threads:
- **Message-ID**: Unique identifier for each email
- **In-Reply-To**: Links reply to original message
- **References**: Chain of message IDs in conversation

### Priority Detection
The system attempts to detect priority from:
- Email subject (e.g., "URGENT", "HIGH PRIORITY")
- Email content keywords
- Default priority from inbox settings

### Auto-Assignment
Tickets can be automatically assigned based on:
- Inbox default assignee
- Inbox default team
- Round-robin assignment
- Workflow automation rules

## Email Formatting

### Supported Content
- **Plain Text**: Fully supported
- **HTML**: Converted to plain text for display
- **Attachments**: Currently logged (full attachment support coming soon)

### Email Headers Preserved
- From name and email
- Subject line
- Received timestamp
- Reply-to address

## Troubleshooting

### Emails Not Creating Tickets
1. **Check Connection**: Verify email account credentials
2. **Check Logs**: Review application logs for errors
3. **Verify Sync**: Ensure scheduled command is running
4. **Check Spam**: Verify emails aren't being filtered

### Duplicate Tickets
- System uses email threading to prevent duplicates
- If duplicates occur, check email headers are correct
- Verify Message-ID headers are present

### Missing Emails
- Check email server logs
- Verify account has proper permissions
- Ensure emails aren't being deleted before sync

## Best Practices

1. **Use Dedicated Email Accounts**: Create separate accounts for support
2. **Enable IMAP**: IMAP is preferred over POP3 for better threading
3. **Use App Passwords**: For Gmail and other providers requiring 2FA
4. **Monitor Sync Status**: Check logs regularly
5. **Test Before Production**: Verify email integration in staging

## Manual Email Sync

You can manually trigger email sync:
```bash
# Sync all inboxes
php artisan tickets:sync-inboxes

# Sync specific inbox
php artisan tickets:sync-inboxes --inbox=1

# Force re-sync (ignore last sync time)
php artisan tickets:sync-inboxes --force
```

## Future Enhancements

- Email attachment handling
- Reply-to-email functionality
- Email templates with variables
- CC/BCC support
- Email forwarding