# Email Configuration Guide

## Overview

This guide explains how to configure email settings in your Laravel application to ensure emails are actually sent instead of just logged.

## Current Issue

The verification code emails are not working because the default mailer is set to 'log', which means emails are being logged to files instead of actually sent.

## Configuration Options

### Option 1: SMTP Configuration (Recommended for Production)

Add these settings to your `.env` file:

```env
# Mail Configuration
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=your-app-password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your-email@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
```

**For Gmail:**
- Use port 587 with TLS encryption
- Generate an "App Password" in your Google Account settings
- Don't use your regular Gmail password

**For Outlook/Office 365:**
```env
MAIL_MAILER=smtp
MAIL_HOST=smtp-mail.outlook.com
MAIL_PORT=587
MAIL_USERNAME=your-email@outlook.com
MAIL_PASSWORD=your-password
MAIL_ENCRYPTION=tls
```

**For Custom SMTP Server:**
```env
MAIL_MAILER=smtp
MAIL_HOST=your-smtp-server.com
MAIL_PORT=587
MAIL_USERNAME=your-username
MAIL_PASSWORD=your-password
MAIL_ENCRYPTION=tls
```

### Option 2: Mailgun (Cloud Email Service)

```env
MAIL_MAILER=mailgun
MAILGUN_DOMAIN=your-domain.com
MAILGUN_SECRET=your-mailgun-secret
```

### Option 3: Amazon SES

```env
MAIL_MAILER=ses
AWS_ACCESS_KEY_ID=your-access-key
AWS_SECRET_ACCESS_KEY=your-secret-key
AWS_DEFAULT_REGION=us-east-1
```

### Option 4: SendGrid

```env
MAIL_MAILER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=your-sendgrid-api-key
MAIL_ENCRYPTION=tls
```

## Testing Email Configuration

### 1. Test Route (Temporary)

Add this route to `routes/web.php` for testing (remove in production):

```php
Route::get('/test-email', function () {
    try {
        Mail::raw('Test email from Laravel', function($message) {
            $message->to('test@example.com')
                    ->subject('Test Email');
        });
        return 'Email sent successfully!';
    } catch (\Exception $e) {
        return 'Email failed: ' . $e->getMessage();
    }
})->middleware('auth');
```

### 2. Check Mail Logs

If using the 'log' driver, check the mail logs:

```bash
tail -f storage/logs/laravel.log
```

Look for entries like:
```
[2024-01-01 12:00:00] local.INFO: Email sent to test@example.com
```

### 3. Test KYC Verification

After configuring email:

1. Go to a KYC form
2. Enter an authorized email address
3. Click "Send Verification Code"
4. Check if the email is received

## Common Issues and Solutions

### Issue: "Connection refused" or "Connection timeout"
**Solution:** Check your SMTP server settings and firewall rules

### Issue: "Authentication failed"
**Solution:** Verify your username/password or API keys

### Issue: "SSL certificate problem"
**Solution:** Try using port 587 with TLS instead of port 465 with SSL

### Issue: Gmail "Less secure app access"
**Solution:** Use App Passwords instead of regular passwords

## Security Considerations

1. **Never commit `.env` files** to version control
2. **Use App Passwords** for Gmail/Google Workspace
3. **Rotate API keys** regularly
4. **Monitor email logs** for suspicious activity
5. **Use environment-specific** configurations

## Production Checklist

- [ ] Email driver configured (not 'log' or 'array')
- [ ] SMTP credentials verified
- [ ] Email templates tested
- [ ] Error handling implemented
- [ ] Monitoring/logging configured
- [ ] Rate limiting configured (if needed)

## Troubleshooting Commands

```bash
# Clear config cache
php artisan config:clear

# Clear route cache
php artisan route:clear

# Check current mail configuration
php artisan tinker
config('mail.default')
config('mail.mailers.smtp')

# Test mail configuration
php artisan tinker
Mail::raw('Test', function($m) { $m->to('test@example.com')->subject('Test'); });
```

## Support

If you continue to have issues:

1. Check the Laravel logs: `storage/logs/laravel.log`
2. Verify your email provider's settings
3. Test with a simple email first
4. Check your server's outbound email policies
