<?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()
    {
        self::$email = new PHPMailer(true);
        self::$email->isSMTP();
        self::$email->CharSet = 'UTF-8';
        self::$email->Host = env("MAIL_HOST");
        self::$email->SMTPAuth = /*false;*/ false;    // 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->Port = env("MAIL_PORT");
        self::$email->isHTML(true);
        /* self::$email->AddEmbeddedImage('img/header.png', 'header');
        self::$email->AddEmbeddedImage('img/footer.png', 'footer');
        self::$email->AddEmbeddedImage('img/mail_firma.jpg', 'firma'); */
        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 attachFile($file, $name = "")
    {
        if (null != $file and null != $name) {
            self::$email->addAttachment($file, $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) {
            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;
    }
}