<?php
session_start();
require 'conexao.php'; // Sua conexão com o banco

$tenant_id = $_SESSION['tenant_id']; // Ou via cookie/GET
$email = $_POST['email'];
$senha = $_POST['senha'];
$imap_host = $_POST['imap_host'];
$imap_port = $_POST['imap_port'];

// Criptografa a senha (opcional, mas recomendado)
$senha_cripto = password_hash($senha, PASSWORD_BCRYPT);

// Salva no banco
$query = "REPLACE INTO configuracoes_email 
          (tenant_id, email, senha, imap_host, imap_port) 
          VALUES (?, ?, ?, ?, ?)";
$stmt = $pdo->prepare($query);
$stmt->execute([$tenant_id, $email, $senha_cripto, $imap_host, $imap_port]);

header("Location: email_app.php?tenant=" . $tenant_id);