# Automated Follow-Up Email System

## Overview
The follow-up system includes strategic email templates that cycle through different approaches. **IMPORTANT: Automatic email sending is DISABLED by default.** The system schedules follow-ups and creates calendar reminders, but does NOT automatically send emails. Users must send follow-up emails manually using the appropriate template.

## Follow-Up Email Strategies

The system cycles through 8 different follow-up email templates:

1. **Follow-up #1: BUILD RAPPORT**
   - Focus: Quick check-in, friendly approach
   - Goal: Establish connection and availability

2. **Follow-up #2: Mention their goals**
   - Focus: Reference goals mentioned in conversation
   - Goal: Show alignment with their objectives

3. **Follow-up #3: Mention Pain**
   - Focus: Address challenges/problems they mentioned
   - Goal: Position as solution provider

4. **Follow-up #4: Educate them**
   - Focus: Share valuable insights or tips
   - Goal: Provide value and build trust

5. **Follow-up #5: Humor**
   - Focus: Light-hearted, friendly approach
   - Goal: Stand out and maintain engagement

6. **Follow-up #6: Ask for the sale**
   - Focus: Direct ask about moving forward
   - Goal: Close or get clear answer

7. **Follow-up #7: Value Proposition**
   - Focus: Remind of key benefits
   - Goal: Reinforce value and urgency

8. **Follow-up #8: Final Check-in**
   - Focus: Last attempt, respectful exit
   - Goal: Final opportunity or graceful closure

After 8 follow-ups, the cycle repeats from #1.

## How It Works

### Follow-Up Reminders (NO Automatic Emails)
1. When a follow-up is due (based on `next_followup_at`), the cron job processes it
2. System determines which template should be used based on follow-up count
3. **Reminder activity is created** (NOT an email - just a reminder)
4. User is notified that a follow-up is due and which template to use
5. User must manually send the email using the suggested template

### Template Selection
- Follow-up #1 → BUILD RAPPORT template
- Follow-up #2 → GOALS template
- Follow-up #3 → PAIN template
- Follow-up #4 → EDUCATE template
- Follow-up #5 → HUMOR template
- Follow-up #6 → ASK_FOR_SALE template
- Follow-up #7 → VALUE_PROP template
- Follow-up #8 → FINAL_CHECK template
- Follow-up #9+ → Cycles back to #1

### Email Template Features
- **Merge Fields:** All templates support merge fields:
  - `{first_name}`, `{last_name}`, `{full_name}`
  - `{email}`, `{phone}`, `{company}`
  - `{sender_name}`, `{sender_email}`, `{sender_phone}`
  - `{today_date}`
- **Personalization:** Each email is personalized with lead information
- **Professional Tone:** Templates are written to be professional yet approachable

## Database Schema

### New Column in `leads` Table
- `last_followup_type` (VARCHAR(50), NULL) - Tracks which follow-up type was used last
  - Values: `BUILD_RAPPORT`, `GOALS`, `PAIN`, `EDUCATE`, `HUMOR`, `ASK_FOR_SALE`, `VALUE_PROP`, `FINAL_CHECK`

### Email Templates
All follow-up email templates are stored in `email_templates` table with:
- `name`: Template identifier (e.g., "FollowUp Email - BUILD RAPPORT")
- `subject_template`: Email subject with merge fields
- `body_template`: Email body with merge fields
- `is_shared`: TRUE (available to all tenants)

## Cron Job

The follow-up processor runs daily at 9 AM:
```bash
0 9 * * * /usr/bin/php /var/www/html/scripts/followup-processor.php >> /var/log/followup-processor.log 2>&1
```

This cron job:
1. Finds all leads with overdue follow-ups
2. Creates reminder activities (NOT emails) suggesting which template to use
3. Logs activities for tracking
4. **Does NOT send emails automatically**

## Manual Follow-Up Sending

You can also manually trigger a follow-up email:
```php
$followUpService = new FollowUpService($db, $tenantId, $userId);
$result = $followUpService->sendFollowUpEmail($leadId);
```

## Tracking

Each follow-up email:
- Is logged in `email_log` table
- Creates an activity entry
- Updates `last_followup_type` in leads table
- Increments `followup_count`
- Schedules next follow-up date

## Customization

### Editing Templates
Templates can be edited in the admin panel or directly in the database:
```sql
UPDATE email_templates 
SET subject_template = '...', body_template = '...'
WHERE name = 'FollowUp Email - BUILD RAPPORT';
```

### Adding New Templates
To add new follow-up strategies:
1. Insert new template into `email_templates` table
2. Update `getFollowUpTemplate()` method in `FollowUpService.php`
3. Add mapping in `getFollowUpTypeFromTemplate()` method

## Status
✅ **COMPLETE** - System automatically sends follow-up emails using strategic templates

