<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

Mailer::initialize();

class Mailer extends Model
{
    public static $email;

    public static function initialize(){
		//var_dump(ENV("PHPMAILER_HOST") . '-----' .ENV("PHPMAILER_PORT") );die();
		self::$email = new PHPMailer(true);
		self::$email->isSMTP();
		self::$email->CharSet = 'UTF-8';
		self::$email->Encoding = 'base64';
		self::$email->Host = env("MAIL_HOST");
		self::$email->SMTPAuth = false;//true;    // Enable SMTP authentication
		self::$email->Username = env("MAIL_USERNAME");
        self::$email->Password = env("MAIL_PASSWORD");
		self::$email->SMTPSecure = '';  // Enable TLS encryption, `ssl` also accepted
		// self::$email->SMTPDebug = 3;  // DEBUG
		self::$email->Port = env("MAIL_PORT");   
		self::$email->isHTML(true);
		self::$email->SMTPOptions = array(
			'ssl' => array(
				'verify_peer' => false,
				'verify_peer_name' => false,
				'allow_self_signed' => true
			)
		);
	}

    public static function from($email, $name="") {
        self::$email->setFrom($email, $name);
    }

    public static function to($email,$name=""){
		if(is_array($email)){
 			foreach($email as $mail){
 				self::$email->addAddress($mail,$mail);
 			}
 		}else{ 
 			self::$email->addAddress($email, $name);
		}
	}

	public static function cc($email) {
		if(null != $email) {
			self::$email->addCC($email);				
		}
	}

	public static function bcc($email) {
		if(null != $email) {
			if(is_array($email)){
				foreach($email as $mail){
					self::$email->addBCC($mail);
				}
			} else {
				self::$email->addBCC($email);
			}			
		}
	}

	public static function replyto($email) {
		if(null != $email) {
			self::$email->addReplyTo($email);
		}
	}

	public static function text($body,$subject="",$AltBody=""){
		self::$email->Subject = $subject;
  		self::$email->Body    = $body;
  		self::$email->AltBody = $AltBody;
	}

	public static function send(){
		$resp = self::$email->send();
		self::$email->ClearAllRecipients();
		return $resp;
	}

	public static function attachment($path='', $name='') {
		if($path != '') {
			return self::$email->addAttachment($path, $name);
		}
	}
}
