<?php
require( __DIR__.'/../plugins/PHPMailer/src/Exception.php' ) ;
require( __DIR__.'/../plugins/PHPMailer/src/PHPMailer.php' ) ;
require( __DIR__.'/../plugins/PHPMailer/src/SMTP.php' ) ;

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

class Mailer {

	private $smtp     = MAILSMTP ;
	private $host     = MAILHOST ;
	private $username = MAILUSERNAME ;
	private $password = MAILPASSWORD ;
	private $port     = MAILPORT ;

	public  $from     = '' ;
	public  $fromname = '' ;
	public  $to       = [] ;
	public  $cc       = [] ;
	public  $bcc      = [] ;
	public  $subject  = '' ;
	public  $body     = '' ;

	public function sendAttachment($path, $filename){
		$this->attachment[0] = $path;
		$this->attachment[1] = $filename;
	}

	public function from( $from ){
		$this->from = $from ;
	}

	public function to( $to ){
		$this->to = $to ;
	}

	public function cc( $cc ){
		$this->cc = $cc ;
	}

	public function bcc( $bcc ){
		$this->bcc = $bcc ;
	}

	public function subject( $subject ){
		$this->subject = $subject ;
	}

	public function body( $body ){
		$this->body = $body ;
	}

	public function send(){
		$mail = new PHPMailer ;

		// Server settings
		if ( $this->smtp == 'yes' ){
		    // $mail->isSMTP() ;
		    // $mail->SMTPDebug  = 1 ;
		    $mail->Host       = $this->host ;
		    $mail->SMTPAuth   = true ;
		    $mail->Username   = $this->username ;
		    $mail->Password   = $this->password ;
		    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS ;
		    $mail->SMTPAuth   = true ;
		    $mail->Port       = $this->port ;
		}

		// from
		$mail->setFrom( $this->from, $this->fromname ) ;

		// send to / cc / bcc
		foreach ( $this->to as $k => $v ){ $mail->addAddress( $v ) ; }
		foreach ( $this->cc as $k => $v ){ $mail->addCC( $v ) ; }
		foreach ( $this->bcc as $k => $v ){ $mail->addBCC( $v ) ; }

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

		if(count($this->attachment) > 0){
			$mail->AddAttachment($this->attachment[0], $this->attachment[1]);
		}
		
		$mail->isHTML(true) ;
		$mail->msgHTML( $this->body ) ;

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

}

?>