# 📧 Step-by-Step Email Setup Guide

This guide will help you configure email notifications for the Centralized Inquiry System.

---

## **Step 1: Locate Your .env File**

1. Navigate to the `backend` folder in your project:
   ```
   D:\IYYO\Centalized inquiry system\app\Centralized-Inquiry-System\backend\
   ```

2. Look for a file named `.env` (it might be hidden)
   - If it doesn't exist, create a new file named `.env`
   - Open it with any text editor (Notepad, VS Code, etc.)

---

## **Step 2: Choose Your Email Provider**

### **Option A: Gmail (Recommended - Easiest)**

#### **Step 2A.1: Enable 2-Step Verification**

1. Go to: https://myaccount.google.com/security
2. Sign in with your Gmail account
3. Under "Signing in to Google", find "2-Step Verification"
4. Click on it and follow the prompts to enable it
   - You'll need your phone number
   - Google will send you a verification code

#### **Step 2A.2: Generate App Password**

1. Go to: https://myaccount.google.com/apppasswords
   - Or navigate: Google Account → Security → 2-Step Verification → App passwords
2. You'll see a dropdown menu "Select app" - choose **"Mail"**
3. Select device: Choose **"Other (Custom name)"**
4. Type: `Centralized Inquiry System`
5. Click **"Generate"**
6. **IMPORTANT**: Copy the 16-character password that appears
   - It will look like: `abcd efgh ijkl mnop`
   - Remove spaces when using it: `abcdefghijklmnop`
   - **Save this password - you won't see it again!**

#### **Step 2A.3: Add to .env File**

Add these lines to your `.env` file:

```env
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USER=your-email@gmail.com
EMAIL_PASS=abcdefghijklmnop
EMAIL_FROM=noreply@centralizedinquiry.com
```

**Replace:**
- `your-email@gmail.com` with your actual Gmail address
- `abcdefghijklmnop` with the app password you generated (no spaces)

---

### **Option B: Outlook/Hotmail**

1. Use your Outlook email address
2. Add to `.env`:

```env
EMAIL_HOST=smtp-mail.outlook.com
EMAIL_PORT=587
EMAIL_USER=your-email@outlook.com
EMAIL_PASS=your-outlook-password
EMAIL_FROM=noreply@centralizedinquiry.com
```

**Note:** For Outlook, you may need to:
- Enable "Less secure app access" in your Microsoft account settings
- Or use an app password (similar to Gmail)

---

### **Option C: Yahoo Mail**

1. Use your Yahoo email address
2. Add to `.env`:

```env
EMAIL_HOST=smtp.mail.yahoo.com
EMAIL_PORT=587
EMAIL_USER=your-email@yahoo.com
EMAIL_PASS=your-yahoo-password
EMAIL_FROM=noreply@centralizedinquiry.com
```

**Note:** Yahoo also requires app passwords for security.

---

### **Option D: Custom SMTP Server**

If you have your own email server or use a service like SendGrid, Mailgun, etc.:

```env
EMAIL_HOST=your-smtp-server.com
EMAIL_PORT=587
EMAIL_USER=your-username
EMAIL_PASS=your-password
EMAIL_FROM=noreply@yourdomain.com
```

---

## **Step 3: Complete Your .env File**

Make sure your `.env` file has all required variables. Here's a complete example:

```env
# Server Configuration
PORT=5000
NODE_ENV=development

# Database Configuration
DB_HOST=localhost
DB_PORT=3306
DB_NAME=centralized_inquiry
DB_USER=root
DB_PASSWORD=

# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-change-in-production-12345
JWT_EXPIRES_IN=7d

# Email Configuration
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USER=your-email@gmail.com
EMAIL_PASS=your-app-password-here
EMAIL_FROM=noreply@centralizedinquiry.com

# Frontend URL
FRONTEND_URL=http://localhost:5173

# Other configurations...
```

---

## **Step 4: Save and Restart Server**

1. **Save** the `.env` file
2. **Stop** your backend server if it's running (Press `Ctrl+C` in the terminal)
3. **Restart** the server:
   ```bash
   cd backend
   npm run dev
   ```

---

## **Step 5: Test Email Configuration**

### **Method 1: Check Server Logs**

When you start the server, look for:
- ✅ **Success**: No email warnings in the console
- ⚠️ **Warning**: If you see "Email service not configured", check your `.env` file

### **Method 2: Trigger a Test Email**

1. Create an inquiry that receives 5 bids
2. Check the server console for:
   ```
   Email sent: [messageId]
   ```
3. Check the customer's email inbox for the notification

---

## **Troubleshooting**

### **Problem: "Email service not configured"**

**Solution:**
- Check that all email variables are in your `.env` file
- Make sure there are no extra spaces around the `=` sign
- Restart the server after making changes

### **Problem: "Authentication failed" (Gmail)**

**Solutions:**
1. Make sure you're using an **App Password**, not your regular Gmail password
2. Verify 2-Step Verification is enabled
3. Check that the app password has no spaces

### **Problem: "Connection timeout"**

**Solutions:**
1. Check your internet connection
2. Verify the `EMAIL_HOST` is correct
3. Try port `465` with `secure: true` (requires code change)
4. Check if your firewall is blocking the connection

### **Problem: Emails going to spam**

**Solutions:**
1. Check the spam/junk folder
2. Add `noreply@centralizedinquiry.com` to contacts
3. For production, use a proper domain email

---

## **Security Notes**

⚠️ **Important:**
- Never commit your `.env` file to Git (it should be in `.gitignore`)
- Never share your app passwords
- Use different passwords for development and production
- For production, use a dedicated email service (SendGrid, Mailgun, AWS SES)

---

## **Quick Reference**

### **Gmail Settings:**
```env
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USER=your-email@gmail.com
EMAIL_PASS=your-16-char-app-password
EMAIL_FROM=noreply@centralizedinquiry.com
```

### **Outlook Settings:**
```env
EMAIL_HOST=smtp-mail.outlook.com
EMAIL_PORT=587
EMAIL_USER=your-email@outlook.com
EMAIL_PASS=your-password
EMAIL_FROM=noreply@centralizedinquiry.com
```

---

## **Need Help?**

If you're still having issues:
1. Check the server console for error messages
2. Verify your email provider's SMTP settings
3. Test with a different email provider
4. Check that nodemailer is installed: `npm list nodemailer`

---

**✅ Once configured, emails will be sent automatically when:**
- An inquiry receives 5 bids (customer notification)
- A supplier account is created (password setup email)
- Other system notifications are triggered

