# AWS SES Troubleshooting Guide

Your website now has enhanced error handling and logging. Here's how to troubleshoot email sending issues:

## 1. Check Browser Console

Open your website and:
1. Press F12 to open Developer Tools
2. Go to the "Console" tab
3. Submit a test form
4. Look for error messages

### Common Console Messages:

**Success:**
```
AWS SES configured successfully
Attempting to send email with subject: New Quote Request - PTSAerial - [service]
Email sent successfully: {MessageId: "..."}
```

**Common Errors:**
```
AWS SDK not loaded. Please check the script tag.
Error configuring AWS: [error details]
SES Error: MessageRejected
SES Error: AccessDenied
SES Error: InvalidParameterValue
```

## 2. Common Issues & Solutions

### Issue: "MessageRejected" Error
**Cause:** Email addresses not verified in SES
**Solution:** 
1. Go to AWS SES Console
2. Navigate to "Verified identities"
3. Verify both `oli@ptslondon.co.uk` and `noreply@ptslondon.co.uk`
4. Check your email for verification links

### Issue: "AccessDenied" Error
**Cause:** IAM user lacks SES permissions
**Solution:**
1. Go to AWS IAM Console
2. Check your user has the SES policy attached
3. Ensure policy includes `ses:SendEmail` and `ses:SendRawEmail`

### Issue: "Sandbox" Restrictions
**Cause:** SES is in sandbox mode
**Solution:**
1. In SES Console, request production access
2. Or verify all recipient email addresses
3. Sandbox only allows verified recipients

### Issue: "Region" Mismatch
**Cause:** Wrong AWS region in config
**Solution:**
1. Check which region your SES is set up in
2. Update `region: 'eu-west-1'` in script.js to match

### Issue: "CORS" Errors
**Cause:** Browser security restrictions
**Solution:**
- This shouldn't occur with SES, but if it does, consider using a backend API instead

## 3. Verify SES Setup

### Check in AWS Console:

1. **SES Dashboard** → "Sending statistics"
   - Should show attempted sends
   - Check for bounce/complaint rates

2. **Verified identities** 
   - `oli@ptslondon.co.uk` should show "Verified"
   - `noreply@ptslondon.co.uk` should show "Verified"

3. **Configuration sets** (optional)
   - Can help with tracking delivery

## 4. Test with Minimal Example

Add this to your browser console to test SES directly:

```javascript
// Test AWS SES configuration
AWS.config.update({
    region: 'eu-west-1',
    accessKeyId: 'AKIA3C5Y7TIMPQ7PHLEM',
    secretAccessKey: 'BPCPlS2X77KwgvFweOO3IebeT9U5gBNnZ5/7ZRX2WEtO'
});

const ses = new AWS.SES();
const params = {
    Destination: { ToAddresses: ['oli@ptslondon.co.uk'] },
    Message: {
        Body: { Text: { Data: 'Test email from website' } },
        Subject: { Data: 'Test Email' }
    },
    Source: 'noreply@ptslondon.co.uk'
};

ses.sendEmail(params, function(err, data) {
    if (err) console.error('Error:', err);
    else console.log('Success:', data);
});
```

## 5. Security Note

⚠️ **Important:** Your AWS credentials are currently visible in the browser source code. This is acceptable for testing but consider these alternatives for production:

### Option A: Backend API (Recommended)
Create a simple backend endpoint that handles email sending:
```javascript
// Instead of direct SES, call your API
fetch('/api/send-email', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(formData)
});
```

### Option B: Cognito Identity Pool
Use temporary credentials instead of permanent access keys.

## 6. Alternative: Quick Backend Solution

If SES continues to have issues, here's a simple Node.js backend you could deploy:

```javascript
// server.js
const express = require('express');
const AWS = require('aws-sdk');
const app = express();

AWS.config.update({
    region: 'eu-west-1',
    accessKeyId: process.env.AWS_ACCESS_KEY,
    secretAccessKey: process.env.AWS_SECRET_KEY
});

app.post('/send-email', (req, res) => {
    const ses = new AWS.SES();
    // Email sending logic here
});
```

## 7. Monitoring

Check these regularly:
- AWS SES bounce rate (keep under 5%)
- AWS SES complaint rate (keep under 0.1%)
- Your website's email delivery success rate

## Next Steps

1. Check browser console for specific error messages
2. Verify email addresses in AWS SES
3. Ensure SES is out of sandbox mode or recipients are verified
4. Test with the minimal example above
5. Consider implementing a backend API for production use

Let me know what specific error messages you see in the console!
