# Feedback SMTP - Final Status & Solutions

## ✅ Fixed Issues

### 1. Table Lookup Corrected
**Problem:** Code was looking for system SMTP in `app_settings` table  
**Fixed:** Now correctly looks in `smtp_configurations` table (same as welcome emails)

```typescript
// BEFORE (Wrong table)
const systemSmtp = await prisma.appSetting.findFirst({
  where: { key: "smtp_config" }
});

// AFTER (Correct table - same as welcome emails)
const systemSmtp = await prisma.smtpConfiguration.findFirst({
  where: { status: "1" },
  orderBy: { id: "desc" }
});
```

**File Changed:** `mis/backend/src/services/smtp.ts` line ~499

---

## 🟡 Expected Behavior (Not a Bug)

### 422 Error When Modal Shows

**What you see:**
```
POST /api/projects/1/feedback/72/replies 422 (Unprocessable Entity)
Error: NO_TP_SMTP_CONFIGURED
```

**This is CORRECT behavior!** Here's why:

1. User clicks "Send reply" button → `send(false)` is called
2. Backend tries to send with TP SMTP → TP SMTP not configured
3. Backend returns 422 error with `NO_TP_SMTP_CONFIGURED`
4. Frontend catches this error → **Shows the modal** ✅
5. Modal gives user choice: Configure TP SMTP or Use System SMTP

**The 422 error is the trigger for showing the modal.** Without it, the modal wouldn't know when to appear.

---

## ❌ Remaining Issue

### SMTP Authentication Error

**Error Message:**
```
Invalid login: 535 Incorrect authentication data
```

**What This Means:**
The SMTP server is rejecting the login credentials (username/password).

### Why This is Confusing

You mentioned: "system smtp is already working for other purposes like when tp registration it sends welcome email"

**This should mean:**
- System SMTP config in `smtp_configurations` table is correct
- Credentials work for welcome emails
- **Should work for feedback replies too** (they use the same code path now!)

### Possible Causes

1. **Multiple SMTP Records**
   - There might be multiple rows in `smtp_configurations` table
   - Only the one with `status = "1"` and highest `id` is used
   - Check if the correct one is active

2. **Credentials Recently Changed**
   - Email provider password was changed
   - Or app-specific password was revoked (Gmail/Office365)
   - Update needed in database

3. **Email Provider Restrictions**
   - Some providers limit "from" addresses
   - Feedback emails might use different "from" address than welcome emails
   - Provider might reject it

4. **Backend Not Restarted**
   - Code change requires backend restart to take effect

---

## 🔍 Diagnostic Steps

### Step 1: Check Active SMTP Config

Run this SQL:

```sql
SELECT id, sender_name, sender_email, smtp_host, smtp_port, smtp_user, smtp_type, status
FROM smtp_configurations
WHERE status = '1'
ORDER BY id DESC
LIMIT 1;
```

**Expected:** Should return 1 row with your SMTP credentials

### Step 2: Verify This is the Same Config Used for Welcome Emails

Compare the result above with what you know works for welcome emails. They should be identical.

### Step 3: Test SMTP Credentials

Try sending a test email using the same credentials outside the app (e.g., using an email client or online SMTP tester) to verify they work.

### Step 4: Check Backend Logs

When you click "Use System SMTP", check the backend console for detailed error messages. It might show more specific info about why auth failed.

### Step 5: Restart Backend

After the code changes:

```bash
cd mis/backend
npm run dev
```

---

## 📝 Summary

| Item | Status | Notes |
|------|--------|-------|
| Table lookup | ✅ Fixed | Now uses `smtp_configurations` (same as welcome emails) |
| 422 error on modal show | ✅ Expected | This is how the modal gets triggered - not a bug |
| SMTP auth error (535) | ❌ Needs investigation | Check active SMTP record in database |
| Backend restart | ⚠️ Required | Must restart backend after code changes |

---

## 🎯 Next Actions

1. **Restart backend server** (required for code changes to take effect)
2. **Run SQL diagnostic** (Step 1 above) to see active SMTP config
3. **Test feedback reply** with System SMTP option
4. **Check backend logs** for detailed error message
5. If still failing, **compare SMTP config** between what's in database vs what you expect

---

## 🆘 If Still Not Working

Share the output of:

```sql
SELECT id, sender_name, sender_email, smtp_host, smtp_port, smtp_user, smtp_type, status, created_at
FROM smtp_configurations
ORDER BY id DESC
LIMIT 5;
```

And any error messages from the backend console log.

