# Email Notifications for New Chats

## Overview

The A1AI plugin now includes email notification functionality that sends alerts when new chats start on your website.

## Features

- **Per-Chatbot Configuration**: Each chatbot can have its own notification email address
- **Automatic Notifications**: Emails are sent automatically when a new conversation begins
- **Rich Email Content**: Includes chatbot name, website URL, admin panel link, session ID, and first message
- **Email Validation**: Both client-side and server-side validation ensure valid email addresses
- **Debug Logging**: Failed email attempts are logged when WordPress debug mode is enabled

## Setup

### 1. Configure Email Notifications

1. Go to **A1AI > Chatbots** in your WordPress admin
2. Click **Edit** on any chatbot
3. In the **General** tab, find the **New Chat Alert Email** field
4. Enter a valid email address (e.g., `admin@yourwebsite.com`)
5. Click **Update Chatbot**

### 2. Email Content

When a new chat starts, the following information is included in the email:

- **Site Name**: Your WordPress site name
- **Chatbot Name**: The name of the chatbot that received the chat
- **Website URL**: Link to your website
- **Admin Panel**: Direct link to the A1AI conversations page
- **Session ID**: Unique identifier for the chat session
- **First Message**: The user's first message (if any)

### 3. Email Format

```
Subject: [Your Site Name] New Chat Started

A new chat has started on your website.

Site: Your Site Name
Chatbot: Customer Support Bot
Website: https://yourwebsite.com
Admin Panel: https://yourwebsite.com/wp-admin/admin.php?page=a1ai-conversations
Session ID: user_123_abc123def456
First Message: Hello, I need help with my order

This is an automated notification from your A1AI chatbot system.
```

## Technical Details

### Database Storage

Email addresses are stored in the chatbot settings as JSON in the `wp_a1ai_chatbots` table:

```sql
settings JSON field contains:
{
  "notification_email": "admin@example.com",
  "other_settings": "..."
}
```

### Email Trigger

Emails are sent when:
1. A new conversation is created (not for every message)
2. The chatbot has a valid notification email configured
3. The user sends their first message

### Email Function

The email functionality is implemented in `A1AI_Data_Manager::send_new_chat_notification()`:

```php
public function send_new_chat_notification($chatbot_id, $session_id, $user_message = '')
```

### Validation

- **Client-side**: HTML5 email validation with pattern matching
- **Server-side**: WordPress `is_email()` function validation
- **Sanitization**: WordPress `sanitize_email()` function

## Troubleshooting

### Email Not Sending

1. **Check Email Configuration**: Ensure the email address is valid and properly saved
2. **WordPress Email**: Verify WordPress can send emails (check spam folder)
3. **Debug Logs**: Enable WordPress debug mode to see email attempt logs
4. **Plugin Conflicts**: Check for email-related plugin conflicts

### Debug Logging

When `WP_DEBUG` and `WP_DEBUG_LOG` are enabled, email attempts are logged:

```
A1AI Email Notification: Attempted to send new chat notification to admin@example.com for chatbot 1. Result: Success
```

## Security

- Email addresses are validated and sanitized
- Nonces are used for form submissions
- User capabilities are checked for admin access
- Email content is properly escaped

## Customization

### Modify Email Content

To customize the email content, edit the `send_new_chat_notification()` method in `includes/class-a1ai-data-manager.php`.

### Add Additional Fields

To add more information to the email, modify the settings array and email template in the same method.

## Support

For issues with email notifications:

1. Check the WordPress error logs
2. Verify email server configuration
3. Test with a simple email address
4. Contact plugin support with debug information 