<?php

namespace App\Http\Controllers;

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

class CorreoController{
    public function enviarCorreo($plantilla, $correos, $bindSubject, $bindBody, $msg, $comp){
        require base_path("vendor/autoload.php");
        
        $mail = new PHPMailer(true);
        $subject = $plantilla->COPL_ASUN;
        $body = $plantilla->COPL_CONT;

        foreach($bindSubject as $k=>$v){
            $subject = str_replace($k, $v, $subject);
        }

        foreach($bindBody as $k=>$v){
            $body = str_replace($k, $v, $body);
        }

        try{
            $mail->SMTPDebug = 0;
            $mail->isSMTP();
            $mail->Host = 'smtp.sendgrid.net';
            $mail->Port = 2525;
            $mail->SMTPSecure = 'tls';
            $mail->SMTPAuth = true;
            $mail->Username = "apikey";
            //ittec.vacaciones@gmail.com
            $mail->Password = "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";
            //TXfaaWcgs9GX9uF
            $mail->SetFrom("sirh@ittec.mx", 'Sistema Integral de Recursos Humanos - ' . $comp);
            /*foreach($correos as $correo){
                $mail->AddAddress($correo);
            }*/
            $mail->AddAddress("lidia.perez.perez@timken.com");
            $mail->AddAddress("grace.magana@timken.com");
            $mail->AddAddress("jose.b@ittec.mx");
            $mail->AddAddress("jean@ittec.mx");
            //$mail->AddAddress("jorge@ittec.mx");

            $mail->Subject = $subject;
            $mail->CharSet = 'UTF-8';

            $mail->isHTML(true);
            $mail->Body = $body;

            if($mail->send()){
                return "$msg, correo enviado";
            }else{
                return "$msg, correo no enviado";
            }
        }catch(Exception $e){
            return "$msg, correo no enviado";
        }
    }
}