<?php

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

require 'vendor/autoload.php';

$mail = new PHPMailer(true);
if (isset($_POST['message']) && isset($_POST['email'])) {
    try {
        $fname = $_POST['name'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];
        $from_email = $_POST['email'];
        $toemail = 'ajkerweb@gmail.com';
        $subject = 'Mail From Website';
        $messageHtml = "
            <html>
                <head>
                    <title>Mail From Website</title>
                </head>
                <body>
                    $message
                </body>
            </html>
            ";

        $mail->SMTPDebug = 2; // for showing bug and other details even success
        // $mail->SMTPDebug = 0;
        $mail->isSMTP();                          // Set mailer to use SMTP
        $mail->Host = 'mail.ajkerweb.com';
        $mail->SMTPAuth = true;
        $mail->Username = 'info@ajkerweb.com';    // SMTP username
        $mail->Password = '@K!l205545/mollika';         // SMTP password

        // Enable TLS encryption, 'ssl' also accepted
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 465;
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $mail->setFrom($from_email, $fname . ' ' . $phone);
        $mail->addReplyTo($from_email, $fname);
        $mail->addAddress($toemail);         // Add a recipient
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $body = $messageHtml;
        $mail->Body = $body;

        $mail->send();

        echo "Mail has been sent successfully!";
    } catch (Exception $e) {

        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
}else{
    echo 'Please fill up all fields!';
}
