# Email Configuration Guide

This guide explains how to configure email sending in the Backend application.

## Overview

The backend uses **Nodemailer** to send emails for:
- Contact form submissions
- Proposal requests
- Book expert form submissions
- Partner/affiliate form submissions
- Zoom meeting confirmations
- Google Meet meeting confirmations

## Required Environment Variables

Add these variables to your `Backend/.env` file:

```env
# SMTP Email Configuration
SMTP_HOST=smtp.gmail.com
SMTP_PORT=465
SMTP_SECURE=true
SMTP_USER=your-email@gmail.com
SMTP_PASS=your-app-password

# Company Email (optional, used as reply-to)
COMPANY_EMAIL=contact@aibitsol.com
```

## Gmail Setup (Recommended)

### Step 1: Enable 2-Factor Authentication
1. Go to your Google Account settings
2. Navigate to Security
3. Enable 2-Step Verification

### Step 2: Generate App Password
1. Go to Google Account → Security
2. Under "2-Step Verification", click "App passwords"
3. Select "Mail" and "Other (Custom name)"
4. Enter "Backend Email Service"
5. Click "Generate"
6. Copy the 16-character password (use this as `SMTP_PASS`)

### Step 3: Configure .env File
```env
SMTP_HOST=smtp.gmail.com
SMTP_PORT=465
SMTP_SECURE=true
SMTP_USER=your-email@gmail.com
SMTP_PASS=xxxx xxxx xxxx xxxx  # The 16-character app password
```

## Other Email Providers

### Outlook/Hotmail
```env
SMTP_HOST=smtp-mail.outlook.com
SMTP_PORT=587
SMTP_SECURE=false
SMTP_USER=your-email@outlook.com
SMTP_PASS=your-password
```

### Yahoo Mail
```env
SMTP_HOST=smtp.mail.yahoo.com
SMTP_PORT=465
SMTP_SECURE=true
SMTP_USER=your-email@yahoo.com
SMTP_PASS=your-app-password
```

### Custom SMTP Server
```env
SMTP_HOST=mail.yourdomain.com
SMTP_PORT=465
SMTP_SECURE=true
SMTP_USER=noreply@yourdomain.com
SMTP_PASS=your-password
```

## Testing Email Configuration

### Option 1: Use the Test Script
```bash
cd Backend
npm run check-smtp
```

### Option 2: Manual Test
Create a test file `test-email.js`:

```javascript
import nodemailer from 'nodemailer';
import dotenv from 'dotenv';

dotenv.config();

const transporter = nodemailer.createTransport({
  host: process.env.SMTP_HOST,
  port: parseInt(process.env.SMTP_PORT || "465"),
  secure: process.env.SMTP_PORT === "465" || process.env.SMTP_SECURE === "true",
  auth: {
    user: process.env.SMTP_USER,
    pass: process.env.SMTP_PASS,
  },
});

transporter.verify((error, success) => {
  if (error) {
    console.error('Email configuration error:', error);
  } else {
    console.log('✅ Email server is ready to send messages');
  }
});
```

Run: `node test-email.js`

## Email Templates

The backend automatically sends two types of emails for each form submission:

1. **Admin Notification Email** - Sent to admin email (configured in services)
2. **User Confirmation Email** - Sent to the user who submitted the form

### Email Format

All emails are sent as HTML with:
- Professional styling
- Responsive design
- Company branding
- Clear call-to-action buttons

## Troubleshooting

### "Email transporter not configured"
- Check that all SMTP variables are set in `.env`
- Restart the backend server after adding variables

### "Authentication failed"
- For Gmail: Make sure you're using an App Password, not your regular password
- Check that 2FA is enabled on your Google account
- Verify SMTP_USER and SMTP_PASS are correct

### "Connection timeout"
- Check your firewall settings
- Verify SMTP_HOST and SMTP_PORT are correct
- Try using port 587 with `SMTP_SECURE=false` if 465 doesn't work

### "Emails not being received"
- Check spam/junk folder
- Verify recipient email addresses
- Check backend logs for email sending errors
- Ensure SMTP credentials have permission to send emails

## Security Best Practices

1. **Never commit `.env` file to version control**
2. **Use App Passwords instead of regular passwords**
3. **Restrict SMTP access to specific IPs if possible**
4. **Use environment-specific email accounts for production**

## Production Configuration

For production, consider:

1. **Dedicated Email Service**: Use services like SendGrid, Mailgun, or AWS SES
2. **Email Queue**: Implement a queue system (Bull, RabbitMQ) for better reliability
3. **Rate Limiting**: Prevent email abuse
4. **Email Templates**: Use a template engine for better customization
5. **Email Logging**: Log all sent emails for audit purposes

## Support

If you encounter issues:
1. Check backend logs: `Backend/logs/` (if logging is configured)
2. Test SMTP connection using the test script
3. Verify environment variables are loaded correctly
4. Check email provider's documentation for specific requirements

