# Gmail Email Troubleshooting Guide

If emails are not being sent, follow these steps. **All emails are sent using your environment configuration.**

## 🔧 **Step 1: Check Your .env Configuration**

Make sure your `.env` file has the correct email 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
MAIL_FROM_ADDRESS=your-email@gmail.com
MAIL_FROM_NAME="Your Company Name"
```

**IMPORTANT**:

- `MAIL_FROM_ADDRESS` is where all emails are sent FROM
- `MAIL_FROM_NAME` is your company name that appears in emails
- You must use an **App Password**, not your regular Gmail password

## 📧 **How Email Sending Works**

1. **From Address**: Uses `MAIL_FROM_ADDRESS` from your `.env`
2. **Company Name**: Uses `MAIL_FROM_NAME` from your `.env`
3. **Test Emails**: Automatically sent to your `MAIL_FROM_ADDRESS`
4. **OTP Emails**: Sent to user's email but FROM your `MAIL_FROM_ADDRESS`

## 🔐 **Step 2: Generate Gmail App Password**

1. **Enable 2-Factor Authentication**:
    - Go to your Google Account: https://myaccount.google.com/
    - Security → 2-Step Verification
    - Turn it on if not already enabled

2. **Generate App Password**:
    - Go to: https://myaccount.google.com/apppasswords
    - Select "Mail" from the app dropdown
    - Select "Other (Custom name)" and enter "Adamart"
    - Click "Generate"
    - Copy the 16-character password (it will look like: `abcd efgh ijkl mnop`)

3. **Update Your .env**:
    - Use this 16-character password as your `MAIL_PASSWORD`
    - Remove any spaces if needed

## 🧪 **Step 3: Test Your Configuration**

1. **Clear Laravel Cache**:

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

2. **Test Mail Configuration**:
   Visit: `http://afghan-admin.test/test-mail-config`

    This will show your current mail settings including your `MAIL_FROM_ADDRESS`.

3. **Send Test Email**:
   Visit: `http://afghan-admin.test/test-email`

    This will send a test email to your `MAIL_FROM_ADDRESS` (the email configured in your `.env`).

## 🚨 **Common Issues & Solutions**

### Issue 1: "Authentication failed"

**Cause**: Using regular password instead of App Password
**Solution**: Generate and use App Password (see Step 2)

### Issue 2: "Connection timed out"

**Cause**: Firewall blocking port 587
**Solutions**:

- Check if your firewall allows outbound connections on port 587
- Try port 465 with SSL encryption:
    ```env
    MAIL_PORT=465
    MAIL_ENCRYPTION=ssl
    ```

### Issue 3: "SMTP connect() failed"

**Cause**: Network connectivity or SSL/TLS issues
**Solutions**:

- Ensure you have internet connection
- Try disabling SSL verification (for testing only):
    ```env
    MAIL_ENCRYPTION=null
    ```

### Issue 4: "Less secure app access" error

**Cause**: Google blocking less secure apps
**Solution**: Use App Password (Step 2) - this is the official way

### Issue 5: Email goes to spam

**Solutions**:

- Check your Gmail spam folder
- Mark the email as "Not spam"
- Add the sender to your contacts

## 🔍 **Debug Steps**

1. **Check Laravel Logs**:

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

2. **Test with Different Email**:
   Try sending to a different email address (not Gmail)

3. **Verify Environment**:
   Make sure you're not in `local` environment with `MAIL_MAILER=log`

## 📱 **Alternative: Use Google Workspace**

If you have Google Workspace:

```env
MAIL_HOST=smtp.gmail.com
MAIL_USERNAME=your-email@your-domain.com
MAIL_PASSWORD=your-app-password
```

## 🔄 **Alternative Email Providers**

If Gmail still doesn't work, try:

### SendGrid (Recommended)

```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

```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
```

## ✅ **Checklist Before Testing**

- [ ] 2-Factor Authentication enabled on Google account
- [ ] App Password generated (16 characters)
- [ ] .env file updated with App Password
- [ ] Config cache cleared: `php artisan config:clear`
- [ ] Test mail config: `/test-mail-config`
- [ ] Send test email: `/test-email`
- [ ] Check spam folder
- [ ] Check Laravel logs for errors

## 🆘 **Still Not Working?**

1. Check the exact error message in Laravel logs
2. Try a different email provider
3. Contact your hosting provider about SMTP restrictions
4. Test with a simple PHP mail script first

## 📞 **Quick Test Command**

You can also test via artisan:

```bash
php artisan tinker
>>> Mail::raw('Test email', function($message) { $message->to('your-email@gmail.com')->subject('Test'); });
```

This will show you the exact error if email sending fails.
