<?php
@ini_set('max_execution_time', '-1');

// Load Composer's autoloader
require 'vendor/autoload.php';

// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->SMTPDebug = 2;                                       // Enable verbose debug output
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';                     // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                                   // Enable SMTP authentication
    $mail->Username = 'no-reply@jsc.ma';               // SMTP username
    $mail->Password = 'welcome@jsc@ma';                  // SMTP password
    $mail->SMTPSecure = 'ssl';         // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    // Recipients
    $mail->setFrom('no-reply@jsc.ma', 'jsc.ma');
    $mail->addAddress('themehdimarghach@gmail.com', 'Mehdi MARGHACH');                   // Name is optional
    $mail->addReplyTo('ano-reply@jsc.ma', 'jsc.ma');

    // Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');           // Add attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');      // Optional name

    // Content
    $mail->isHTML(true);                                        // Set email format to HTML
    $mail->Subject = 'Message from jsc.ma';
    $mail->Body = 'This is a message from jsc.ma smtp service.';

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