<?php

require '../../cron/phpMailer/Exception.php';
require '../../cron/phpMailer/PHPMailer.php';
require '../../cron/phpMailer/SMTP.php';
require '../../cron/phpMailer/POP3.php';

class MailHelper {

    private $mail;
    private $conn;
    private $top; 
    private $bottom ;

    public function __construct($conn){
        $this->conn = $conn;
        $query_company = $this->conn->query("SELECT * FROM company");
        $row_company = $query_company->fetch_assoc();

        $this->top = $row_company['header'];
        $this->bottom = $row_company['footer'];

    }

    public function connect(){
        $this->mail = new PHPMailer\PHPMailer\PHPMailer(true);
        $this->mail->isSMTP();
        $this->mail->Host = 'smtp.gmail.com';
        $this->mail->Port = 587;
        $this->mail->SMTPKeepAlive = true;   
        $this->mail->Mailer = '“smtp”';
        $this->mail->SMTPSecure = 'tls';
        $this->mail->SMTPAuth   = true;
        $this->mail->Username = 'info@3boltcourt.com';
        $this->mail->Password = '3Boltcourt';
        $this->mail->SetFrom('info@3boltcourt.com', '3Boltcourt');
        $this->mail->AddReplyTo('clerks@3boltcourt.com', '3Boltcourt');
        $this->mail->IsHTML(true);
    }

    public function transmitEmail($emails, $subject, $message){

        foreach($emails as $email){
            $this->mail->addAddress($email); 
        }

        $this->mail->Subject = $subject;

        //generate full body of the email
        $body = $this->top.''.$message.''.$this->bottom;

        $this->mail->Body = $body;

        if($this->mail->send()){
            $this->mail->ClearAddresses();
            return true;
        }else{
            $this->mail->ClearAddresses();
            return false;
        }
        

    }
    
    public function getMail(){
        return $this->mail;
    }



}




?>