# Email Configuration Fix - Summary

## Issues Fixed

### 1. ✅ Hardcoded Email Address
**Problem**: Finance team email was hardcoded as `NOC@afinet.africa` in PaymentController
**Solution**: Changed to use `config('mail.company_mail')` which reads from `.env`

### 2. ✅ Missing Config Entry
**Problem**: `company_mail` was not defined in `config/mail.php`
**Solution**: Added `company_mail` configuration that reads from `COMPANY_MAIL` env variable

### 3. ✅ Config Cache
**Problem**: Config changes weren't being picked up
**Solution**: Cleared and recached configuration

---

## Current Email Configuration

### From .env File:
```env
# Sender Configuration
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=tnrwatida@gmail.com
MAIL_PASSWORD="tqan cfdc veyi seeg"
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=tnrwatida@gmail.com
MAIL_FROM_NAME="AFINET Customer Portal"

# Company/Finance Team Email (receives notifications)
COMPANY_MAIL=tawona@quatrohaus.com
```

### Email Flow:
- **All outgoing emails** are sent FROM: `tnrwatida@gmail.com`
- **Company notifications** (proof of payment, quotations, etc.) go TO: `tawona@quatrohaus.com`
- **Customer emails** go TO: customer's registered email address

---

## Files Modified

1. **config/mail.php**
   - Added `company_mail` configuration

2. **app/Http/Controllers/API/PaymentController.php**
   - Changed hardcoded `NOC@afinet.africa` to `config('mail.company_mail')`

3. **app/Console/Commands/TestEmail.php** (NEW)
   - Created test command to verify email sending

---

## Email Sending Points in System

### 1. Authentication & Registration
- **Welcome Email** → Customer (on registration)
- **Password Reset** → Customer (on forgot password)

### 2. Feasibility Checks
- **Feasibility Result** → Customer (after coverage check)

### 3. Quotations
- **Quotation Requested** → Company (when customer requests quote)
- **Quotation Accepted** → Company (when customer accepts quote)
- **Sales Order Created** → Customer (after quotation acceptance)
- **Invoice Created** → Customer (if invoice generated from quotation)

### 4. Custom Package Enquiries
- **Custom Package Enquiry** → Company (when customer requests custom package)

### 5. Payments - Pesepay
- **Payment Confirmation** → Customer (after successful payment)
- **Payment Received** → Company (after successful payment)

### 6. Payments - Bank Transfer (Proof of Payment)
- **Proof of Payment Received** → Company (when customer uploads proof)
  - Includes: Customer info, payment details, invoice/order info
  - **File attached**: Proof of payment document
- **Proof of Payment Confirmation** → Customer (upload confirmation)
- **Payment Approved** → Customer (when finance approves payment)
- **Payment Confirmation** → Customer (after approval)
- **Payment Received** → Company (after approval)

---

## Testing Email Sending

### Test Command
```bash
# Test with company email (default)
php artisan email:test

# Test with specific email
php artisan email:test your.email@example.com
```

### Expected Output:
```
Sending test email to: tawona@quatrohaus.com
MAIL_MAILER: smtp
MAIL_HOST: smtp.gmail.com
MAIL_FROM: tnrwatida@gmail.com
COMPANY_MAIL: tawona@quatrohaus.com
✅ Test email sent successfully to tawona@quatrohaus.com
Check your inbox (and spam folder)
```

### Test Results:
- ✅ Email to `tawona@quatrohaus.com` - **SENT SUCCESSFULLY**
- ✅ Email to `tnrwatida@gmail.com` - **SENT SUCCESSFULLY**

---

## Troubleshooting

### If Emails Are Not Received:

1. **Check Spam/Junk Folder**
   - Gmail may mark automated emails as spam initially

2. **Check Laravel Logs**
   ```bash
   tail -f storage/logs/laravel.log
   ```

3. **Verify Gmail App Password**
   - Make sure the app password is correct: `tqan cfdc veyi seeg`
   - App passwords are different from your regular Gmail password

4. **Check Gmail Account Settings**
   - Ensure "Less secure app access" is enabled (if using regular password)
   - Or use App Password (recommended - already configured)

5. **Test Email Sending**
   ```bash
   php artisan email:test tnrwatida@gmail.com
   ```

6. **Check Queue**
   ```bash
   # Check if emails are queued
   php artisan queue:failed
   
   # Process queue (if using queue)
   php artisan queue:work
   ```

---

## For Production Deployment

When deploying to production, update `.env`:

```env
# Production Mail Configuration
MAIL_MAILER=smtp
MAIL_HOST=mail.afinet.africa
MAIL_PORT=587
MAIL_USERNAME=notifications@afinet.africa
MAIL_PASSWORD=your_production_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=notifications@afinet.africa
MAIL_FROM_NAME="AFINET Customer Portal"

# Production Company Email
COMPANY_MAIL=NOC@afinet.africa
```

Then run:
```bash
php artisan config:clear
php artisan config:cache
php artisan email:test NOC@afinet.africa
```

---

## Email Templates Location

All email templates are located in:
```
afinet-portal-backend/resources/views/emails/
```

Email classes (Mailable) are located in:
```
afinet-portal-backend/app/Mail/
```

---

## Current Status

✅ Email configuration is working correctly
✅ Test emails sent successfully
✅ All email sending points use config values (no hardcoded emails)
✅ Company notifications go to: `tawona@quatrohaus.com`
✅ Customer emails go to their registered email addresses
✅ All emails sent from: `tnrwatida@gmail.com`

---

## Next Steps

1. **Check your email inbox** (both tawona@quatrohaus.com and tnrwatida@gmail.com)
2. **Check spam folder** if emails not in inbox
3. **Test proof of payment upload** to verify the complete flow
4. **Monitor Laravel logs** during testing: `tail -f storage/logs/laravel.log`

---

## Quick Test Checklist

- [ ] Received test email at tawona@quatrohaus.com
- [ ] Received test email at tnrwatida@gmail.com
- [ ] Test proof of payment upload
- [ ] Verify finance team receives email with attachment
- [ ] Verify customer receives confirmation email
- [ ] Check Laravel logs for any errors
