<?php

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

require __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../config/renderer.php';

function sendMail($fromEmail, $fromName, $mobile, $fservice)
{
    $mail = new PHPMailer(true);

    try {
        $mail->isSMTP();
        $mail->Host       = $_ENV['MAIL_HOST'];
        $mail->SMTPAuth   = true;
        $mail->Username   = $_ENV['MAIL_USERNAME'];
        $mail->Password   = $_ENV['MAIL_PASSWORD'];
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $mail->Port       = (int) $_ENV['MAIL_PORT'];

        $mail->setFrom($_ENV['MAIL_USERNAME'], 'Website');
        $mail->addAddress($_ENV['MAIL_USERNAME']);

        if (filter_var($fromEmail, FILTER_VALIDATE_EMAIL)) {
            $mail->addReplyTo($fromEmail, $fromName);
        }

        $mail->Subject = 'New Form Submission';

        $mail->isHTML(true);
        $body = view('contact.html', [
            'name' => $fromName,
            'mobile' => $mobile,
            'email' => $fromEmail,
            'service' => $fservice,
            '_site_title' => $_ENV['site.title'],
            '_site_url' => $_ENV['site.url'],
        ]);
        $mail->Body = $body;
        // return $body;

        return $mail->send();

    } catch (Exception $e) {
        return $mail->ErrorInfo;
    }
}