# Email Setup Guide for Afghan Market OTP System

This guide will help you configure real email sending for the OTP authentication system in production.

## Quick Setup

### 1. Configure Your .env File

Copy the email configuration from `.env.example` to your `.env` file and update it with your email provider credentials:

```bash
# Basic SMTP Configuration
MAIL_MAILER=smtp
MAIL_HOST=your-smtp-host.com
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_USERNAME=your-email@example.com
MAIL_PASSWORD=your-password-or-app-password
MAIL_FROM_ADDRESS=noreply@afghan-market.com
MAIL_FROM_NAME="Afghan Market"
```

### 2. Clear Configuration Cache

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

### 3. Test Email Sending

You can test the OTP system by:
1. Going to the customer registration page
2. Filling out the form
3. The OTP will be sent to the configured email address

## Email Provider Setup

### Gmail / Google Workspace

1. Enable 2-Factor Authentication on your Google account
2. Generate an App Password:
   - Go to Google Account settings
   - Security → 2-Step Verification → App passwords
   - Generate a new app password for "Mail"
3. Use these settings:

```env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=your-16-character-app-password
```

### Outlook / Office 365

```env
MAIL_MAILER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_USERNAME=your-email@outlook.com
MAIL_PASSWORD=your-password
```

### SendGrid

1. Create a SendGrid account
2. Generate an API key
3. Use these settings:

```env
MAIL_MAILER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_USERNAME=apikey
MAIL_PASSWORD=YOUR_SENDGRID_API_KEY
```

### Mailgun

1. Create a Mailgun account
2. Verify your domain
3. Use these settings:

```env
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_USERNAME=postmaster@your-domain.com
MAIL_PASSWORD=YOUR_MAILGUN_PASSWORD
```

### Amazon SES

1. Create an AWS account
2. Set up SES in your preferred region
3. Verify your domain or email address
4. Use these settings:

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

## Production Considerations

### Security

- **Never commit email credentials to version control**
- Use environment variables or a secure secret management system
- For Gmail, always use App Passwords, never your main password
- Consider using dedicated email services like SendGrid or Mailgun for production

### Deliverability

1. **SPF Records**: Add your server's IP to your domain's SPF record
2. **DKIM**: Configure DKIM signing if your email provider supports it
3. **Domain Verification**: Verify your sending domain with your email provider
4. **Warm-up**: Gradually increase sending volume to build reputation

### Monitoring

Check your Laravel logs for email delivery issues:

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

Look for entries like:
- "OTP for email@example.com: 123456"
- "Failed to send OTP email: ..."

### Testing

In local environment, the OTP will be:
- Logged to Laravel logs
- Returned in the API response as `otp_for_testing`
- Displayed as a toast notification in the frontend

In production, the OTP will only be sent via email.

## Troubleshooting

### Common Issues

1. **Authentication Failed**
   - Check username/password
   - For Gmail, ensure you're using an App Password
   - Verify 2FA is enabled if required

2. **Connection Timed Out**
   - Check firewall settings
   - Verify port (587 for TLS, 465 for SSL, 25 for none)
   - Check if your hosting provider blocks outbound SMTP

3. **Email Not Received**
   - Check spam/junk folder
   - Verify FROM address is properly configured
   - Check email provider's sending limits

### Debug Mode

For debugging, you can temporarily switch to log driver:

```env
MAIL_MAILER=log
```

This will log all emails to `storage/logs/laravel.log` instead of sending them.

## Production Deployment Checklist

- [ ] Configure email provider credentials in `.env`
- [ ] Set `MAIL_MAILER=smtp` (or your preferred driver)
- [ ] Update `MAIL_FROM_ADDRESS` to your domain
- [ ] Test email sending in staging environment
- [ ] Verify SPF/DKIM records if using custom domain
- [ ] Set up monitoring for email delivery failures
- [ ] Remove any test OTP display code from production
- [ ] Clear configuration cache: `php artisan config:clear`

## Support

If you encounter issues:

1. Check Laravel logs: `storage/logs/laravel.log`
2. Verify your email provider account is in good standing
3. Ensure your server can reach the SMTP host
4. Test with a simple PHP mail script first

The OTP system is designed to work even if email delivery fails temporarily - the OTP will still be generated and stored, allowing users to proceed if they have access to the testing OTP (in development) or if email delivery is restored.
