# Email Verification - Step-by-Step Installation

## 🎯 Goal
Get email verification working for username "963" at https://www.jpu.edu.jo/lms2_2401/

## 📋 Pre-Installation Checklist

- [ ] You have admin access to the Moodle server
- [ ] You can upload files to `/local/emailverify/` directory
- [ ] You have database admin privileges
- [ ] External database authentication is working

## 🚀 Installation Steps

### Step 1: Upload Files

Upload these files to your Moodle server:

```
/local/emailverify/simple_login_handler.php
/local/emailverify/simple_verify.php
/local/emailverify/create_table.php
```

**Using FTP/SFTP:**
```bash
# Navigate to your Moodle directory
cd /path/to/moodle/local/emailverify/

# Upload the files
# (use your preferred method: FTP, SFTP, SCP, etc.)
```

**Using command line:**
```bash
# If you have shell access
cp simple_login_handler.php /path/to/moodle/local/emailverify/
cp simple_verify.php /path/to/moodle/local/emailverify/
cp create_table.php /path/to/moodle/local/emailverify/
```

### Step 2: Set File Permissions

```bash
# Set proper permissions
chmod 644 /path/to/moodle/local/emailverify/simple_*.php
chmod 644 /path/to/moodle/local/emailverify/create_table.php

# Set proper ownership (adjust user/group as needed)
chown www-data:www-data /path/to/moodle/local/emailverify/simple_*.php
chown www-data:www-data /path/to/moodle/local/emailverify/create_table.php
```

### Step 3: Create Database Table

1. **Log in as Moodle administrator**
2. **Go to**: `https://www.jpu.edu.jo/lms2_2401/local/emailverify/create_table.php`
3. **Run the script** - it will:
   - Check if the table exists
   - Create the table if needed
   - Test database operations
   - Show "Setup Complete!" when done

**Expected output:**
```
Email Verification Table Setup
✓ Table 'local_emailverify_tokens' created successfully!
Current token count: 0
✓ Test record inserted with ID: 1
✓ Test record retrieved successfully
✓ Test record deleted successfully
Setup Complete!
```

### Step 4: Test the Login Handler

1. **Go to**: `https://www.jpu.edu.jo/lms2_2401/local/emailverify/simple_login_handler.php`
2. **Verify page loads** - should show login form
3. **Test with non-verification user first** (e.g., username "admin"):
   - Enter admin credentials
   - Should login normally without verification

### Step 5: Test Email Verification

1. **Use username "963"**:
   - Enter username: 963
   - Enter correct password
   - Click Login

2. **Expected behavior**:
   - Should show "Verification email sent to [email]"
   - Should NOT redirect to normal login
   - User should receive verification email

3. **Check email**:
   - Look for email from info@julms.com
   - Subject: "Email Verification Required - JPU LMS"
   - Should contain verification link

4. **Click verification link**:
   - Should show "Email Verification Successful!"
   - Should automatically login and redirect to dashboard

## 🔍 Troubleshooting

### Issue: create_table.php shows error

**Possible causes:**
- Database permissions
- Table prefix issues
- MySQL/MariaDB compatibility

**Solution:**
```sql
-- Run manually in database
CREATE TABLE mdl_local_emailverify_tokens (
    id BIGINT(10) NOT NULL AUTO_INCREMENT,
    userid BIGINT(10) NOT NULL,
    token VARCHAR(64) NOT NULL,
    username VARCHAR(100) NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    ip_address VARCHAR(45) DEFAULT NULL,
    user_agent TEXT DEFAULT NULL,
    created BIGINT(10) NOT NULL,
    expires BIGINT(10) NOT NULL,
    verified TINYINT(1) NOT NULL DEFAULT 0,
    verified_at BIGINT(10) DEFAULT NULL,
    attempts INT(3) NOT NULL DEFAULT 0,
    email_sent TINYINT(1) NOT NULL DEFAULT 0,
    email_sent_at BIGINT(10) DEFAULT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY token (token),
    KEY userid (userid),
    KEY username (username),
    KEY created (created),
    KEY expires (expires)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```

### Issue: Login handler shows blank page

**Check:**
1. PHP error logs
2. File permissions
3. Moodle config.php path

**Debug:**
```bash
# Check PHP syntax
php -l /path/to/simple_login_handler.php

# Check error logs
tail -f /var/log/apache2/error.log
tail -f /var/log/nginx/error.log
```

### Issue: Email not sending

**Check server logs for SMTP errors:**
```bash
# Check mail logs
tail -f /var/log/mail.log

# Test SMTP connection
telnet mail.privateemail.com 465
```

### Issue: Authentication failing

**Check external database connection:**
1. Verify external DB auth is working for other users
2. Check user exists in external database
3. Verify credentials are correct

## 📧 Email Configuration

The system uses these hardcoded settings:
- **Host**: mail.privateemail.com
- **Username**: info@julms.com
- **Password**: You$of2030
- **Security**: SSL
- **Port**: 465

## ✅ Success Verification

After installation, verify these work:

1. **Page loads**: `https://www.jpu.edu.jo/lms2_2401/local/emailverify/simple_login_handler.php`
2. **Normal login**: Username "admin" logs in without verification
3. **Verification trigger**: Username "963" triggers email verification
4. **Email delivery**: Verification email is received
5. **Verification completion**: Clicking link completes login

## 🆘 Getting Help

If you encounter issues:

1. **Check server error logs** first
2. **Review the debug output** on the login page
3. **Verify database table** was created properly
4. **Test SMTP connection** independently
5. **Confirm external database auth** is working

## 📞 Support Information

The simplified version includes extensive logging and error handling to help identify any issues. All operations are logged with the `[EmailVerify]` prefix in your server logs.

