# Email Troubleshooting Guide

## Common Issues and Solutions

### 1. Emails Not Arriving

**Problem**: Emails appear to send successfully but never arrive in the inbox.

**Solutions**:
- Check spam/junk folders
- Verify recipient email address is correct
- Check server error logs for mail delivery errors
- Ensure proper SMTP configuration

### 2. Gmail Authentication Issues

**Problem**: Gmail rejects authentication attempts.

**Solutions**:
- Enable 2-Factor Authentication on Gmail account
- Generate and use an App Password instead of regular password
- Ensure "Less secure app access" is enabled (if not using App Passwords)

### 3. Local Development Environment Issues

**Problem**: Emails work on production but not on local XAMPP/WAMP.

**Solutions**:
- Configure sendmail in php.ini
- Install PHPMailer for better SMTP support
- Use a service like Mailtrap for testing

### 4. Error: "Could not instantiate mail function"

**Problem**: PHP mail() function is not available or configured.

**Solutions**:
- Check if sendmail is installed and configured
- Install PHPMailer as an alternative
- Verify php.ini mail settings

## Testing Email Functionality

1. Visit `http://localhost/storeweb/test_email.php`
2. Check PHP error logs:
   - XAMPP: `C:\xampp\php\logs\php_error_log`
   - WAMP: Check Apache error logs

## Debugging Steps

1. **Check error logs**:
   ```php
   error_log("Debug message here");
   ```

2. **Verify configuration**:
   - Check `includes/email_config.php` for correct SMTP settings
   - Ensure `includes/email_functions.php` is properly configured

3. **Test with a simple script**:
   ```php
   <?php
   $to = "test@example.com";
   $subject = "Test";
   $message = "Test message";
   $headers = "From: webmaster@example.com";
   
   if (mail($to, $subject, $message, $headers)) {
       echo "Email sent successfully";
   } else {
       echo "Email failed to send";
   }
   ?>
   ```

## PHPMailer Installation

If you want to use PHPMailer for better email delivery:

1. Download from https://github.com/PHPMailer/PHPMailer
2. Extract to `includes/PHPMailer/`
3. The system will automatically detect and use it

## Gmail Configuration

For Gmail SMTP:

1. Enable 2-Factor Authentication
2. Generate App Password:
   - Go to Google Account settings
   - Security → 2-Step Verification → App passwords
   - Generate new app password for "Mail"
3. Use the 16-character app password in your configuration

## XAMPP Sendmail Configuration

1. Edit `C:\xampp\php\php.ini`:
   ```
   [mail function]
   SMTP = smtp.gmail.com
   smtp_port = 587
   sendmail_from = your-email@gmail.com
   sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
   ```

2. Edit `C:\xampp\sendmail\sendmail.ini`:
   ```
   [sendmail]
   smtp_server=smtp.gmail.com
   smtp_port=587
   error_logfile=error.log
   debug_logfile=debug.log
   auth_username=your-email@gmail.com
   auth_password=your-app-password
   force_sender=your-email@gmail.com
   ```

3. Restart Apache service

## Alternative Solutions

### Using Mailtrap (for development)
1. Sign up at https://mailtrap.io
2. Get SMTP credentials
3. Update `includes/email_config.php` with Mailtrap settings

### Using SendGrid
1. Sign up at https://sendgrid.com
2. Get API key
3. Configure SMTP settings in `includes/email_config.php`