<?php
require_once 'vendor/autoload.php';
include 'email_config.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$account = intval($_POST['account']);
$acc = $email_accounts[$account];

$mail = new PHPMailer(true);
try {
    $mail->isSMTP();
    $mail->Host = $acc['smtp_host'];
    $mail->SMTPAuth = true;
    $mail->Username = $acc['username'];
    $mail->Password = $acc['password'];
    $mail->SMTPSecure = 'tls';
    $mail->Port = $acc['smtp_port'];
    $mail->Timeout = 10;

    $mail->setFrom($acc['email']);
    $mail->addAddress($_POST['to']);
    $mail->Subject = $_POST['subject'];
    $mail->isHTML(true);
    $mail->Body = $_POST['message'];

    $mail->send();
    echo "Message has been sent.";
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
