<?php

namespace App\Jobs\Mails;
use App\Jobs\Job;
use App\Models\Mail;
use App\Models\Setting;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class ValidateMailAddressJob extends Job
{
    private $mailObject = null;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Mail $mailObject)
    {
        $this->mailObject = $mailObject;
    }

    public $tries = 2;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(Setting::getSettingValue("smtp_active") == false) {
            return;
        }
        $mail = new PHPMailer(true);
        $mail->isSMTP();
        $mail->Host = Setting::getSettingValue("smtp_host");
        $mail->SMTPAuth = Setting::getSettingValue("smtp_smtpAuth");
        $mail->Username = Setting::getSettingValue("smtp_username");
        $mail->Password = Setting::getSettingValue("smtp_password");
        $mail->SMTPSecure = Setting::getSettingValue("smtp_secure");
        $mail->Port       = Setting::getSettingValue("smtp_port");
        $mail->setFrom(Setting::getSettingValue("smtp_from_mail"), Setting::getSettingValue("smtp_from_name"));
        $mail->addAddress($this->mailObject->mail);
        $bcc = Setting::getSettingValue("smtp_bcc");
        if(!empty($bcc)) {
            $mail->addBCC($bcc);
        }
        $mail->isHTML(true);
        $mail->Subject = Setting::getSettingValue("name_big").Setting::getSettingValue("name_small").' E-Mail validation';
        $mail->Body    = 'Hello,<br>to validate your E-Mail address click on the following link: <a href="'.Setting::getSettingValue('url').'/gui/mailValidation/'.$this->mailObject->id.'/'.$this->mailObject->validation_code.'">Activate Account</a>';
        $mail->AltBody    = 'Hello,\r\nto validate your E-Mail address click on the following link: '.Setting::getSettingValue('url').'/gui/mailValidation/'.$this->mailObject->id.'/'.$this->mailObject->validation_code;
        $mail->send();
    }
}