# 📧 Email Notifications Setup Guide

Your Fire Fighting Bot now supports email notifications for fire alerts and extinguish events!

## 🚀 Quick Setup

### Method 1: Automated Setup (Recommended)
```bash
python setup_email.py
```
This interactive script will guide you through the configuration process.

### Method 2: Manual Setup
1. Copy `.env.template` to `.env`
2. Edit `.env` with your email credentials
3. Restart your Flask application

## 📧 Email Provider Configuration

### Gmail (Recommended)
1. **Enable 2-Factor Authentication** on your Gmail account
2. Go to [Google Account Settings](https://myaccount.google.com) > Security > App passwords
3. Generate a new app password for this application
4. Use the generated app password (NOT your regular password) in the `.env` file

```env
MAIL_SERVER=smtp.gmail.com
MAIL_PORT=587
MAIL_USE_TLS=true
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=your-16-character-app-password
MAIL_DEFAULT_SENDER=your-email@gmail.com
NOTIFICATION_EMAIL=recipient@gmail.com
```

### Outlook/Hotmail
```env
MAIL_SERVER=smtp-mail.outlook.com
MAIL_PORT=587
MAIL_USE_TLS=true
MAIL_USERNAME=your-email@outlook.com
MAIL_PASSWORD=your-password
```

### Yahoo Mail
```env
MAIL_SERVER=smtp.mail.yahoo.com
MAIL_PORT=587
MAIL_USE_TLS=true
MAIL_USERNAME=your-email@yahoo.com
MAIL_PASSWORD=your-app-password
```

## 🔧 Configuration Variables

| Variable | Description | Example |
|----------|-------------|---------|
| `MAIL_SERVER` | SMTP server address | `smtp.gmail.com` |
| `MAIL_PORT` | SMTP port (usually 587) | `587` |
| `MAIL_USE_TLS` | Enable TLS encryption | `true` |
| `MAIL_USERNAME` | Your email address | `your-email@gmail.com` |
| `MAIL_PASSWORD` | Your email password/app password | `your-app-password` |
| `MAIL_DEFAULT_SENDER` | Default sender email | `your-email@gmail.com` |
| `NOTIFICATION_EMAIL` | Email to receive notifications | `recipient@gmail.com` |

## 🧪 Testing Email Configuration

### Test using the setup script:
```bash
python setup_email.py test
```

### Test manually:
1. Start your Flask application
2. Navigate to the dashboard
3. Click "Simulate Fire Alert"
4. Check your email inbox for the notification
5. Click "Simulate Extinguish" 
6. Check for the extinguish notification

## 📨 Email Notification Types

### 🔥 Fire Alert Email
- **Subject**: "🔥 FIRE ALERT - Immediate Action Required"
- **Content**: 
  - Event ID and timestamp
  - Fire detection notification
  - Action items checklist
  - Professional HTML formatting

### ✅ Fire Extinguished Email
- **Subject**: "✅ Fire Extinguished - All Clear"
- **Content**:
  - Event ID and timestamps
  - Response time calculation
  - Post-incident action items
  - Professional HTML formatting

## 🔐 Security Best Practices

1. **Never commit `.env` file** to version control
2. **Use app passwords** instead of regular passwords when available
3. **Enable 2-Factor Authentication** on your email account
4. **Keep credentials secure** and don't share them
5. **Regularly update passwords** and app passwords

## 🐛 Troubleshooting

### Common Issues:

#### "Authentication failed"
- **Gmail**: Use an app password, not your regular password
- **Outlook**: Ensure "Less secure app access" is enabled if not using app password
- **Yahoo**: Generate and use an app password

#### "Connection timeout"
- Check your internet connection
- Verify SMTP server and port settings
- Some networks block SMTP ports

#### "No email received"
- Check spam/junk folder
- Verify recipient email address
- Test with a different email address

#### "Import flask_mail could not be resolved"
- Install Flask-Mail: `pip install Flask-Mail`
- Or install all requirements: `pip install -r requirements.txt`

### Debug Steps:
1. Check Flask application logs for error messages
2. Verify all environment variables are set correctly
3. Test with a simple email first
4. Try different email providers

## 🚀 Production Deployment

### For cPanel/Web Hosting:
1. Upload your `.env` file to the server
2. Ensure environment variables are loaded
3. Test email functionality after deployment

### For Heroku:
```bash
heroku config:set MAIL_SERVER=smtp.gmail.com
heroku config:set MAIL_PORT=587
heroku config:set MAIL_USE_TLS=true
heroku config:set MAIL_USERNAME=your-email@gmail.com
heroku config:set MAIL_PASSWORD=your-app-password
heroku config:set MAIL_DEFAULT_SENDER=your-email@gmail.com
heroku config:set NOTIFICATION_EMAIL=recipient@gmail.com
```

## 📞 Support

If you encounter issues:
1. Check the troubleshooting section above
2. Verify your email provider's SMTP settings
3. Test with the automated setup script
4. Check application logs for detailed error messages

## 🎉 Features

- **Asynchronous email sending** - doesn't block the web application
- **Professional HTML emails** - beautiful, responsive design
- **Automatic response time calculation** - included in extinguish notifications
- **Error handling** - graceful fallback if email fails
- **Security-focused** - uses app passwords and TLS encryption
