EVOLUTION-MANAGER
Edit File: MailerSendController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\View; use App\User; use Storage; use Log; class MailerSendController extends Controller { private $to = []; private $from_email; private $to_email; private $attachments = []; public function prueba(){ $data = []; MailerSendController::send('correos.mensaje_contacto', $data, function ($m) { $m->to("prueba.sucre@sucre.gob.ec") ->subject('Pruebas de correo electronico'); }); } public static function send($p_vista, $p_vista_data, $callback) { try { $mail = new self(); // Creamos una instancia de la clase Mail $callback($mail); // Llamamos a la función de callback, pasándole la instancia de Mail $vista = $p_vista; $vista_data = $p_vista_data; // Renderizar la vista a un string $htmlContent = View::make($vista, $vista_data)->render(); // Obtenemos los datos de la cuenta de correo que envia $from = env('MAIL_USERNAME'); // Datos de la solicitud a MailerSend $data = [ 'from' => [ 'email' => $from, ], 'to' => $mail->to, 'subject' => $mail->subject, 'text' => 'Gad Sucre.', 'html' => $htmlContent, ]; //preparamos archivos adjuntos si existen if(!empty($mail->attachments)){ $data['attachments'] = $mail->attachments; } // dd($data); // Convertir los datos a JSON $jsonData = json_encode($data); // Token de Autorización //$authToken = 'XXXXXXXXXXXX'; $authToken = env('MAILER_SEND_TOKEND'); // Iniciar una sesión cURL $ch = curl_init('https://api.mailersend.com/v1/email'); // Configurar las opciones de cURL curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'X-Requested-With: XMLHttpRequest', 'Authorization: Bearer ' . $authToken, ]); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); // Ejecutar la solicitud y obtener la respuesta $response = curl_exec($ch); // Verificar si hubo errores en la solicitud if ($response === false) { return response()->json([ 'status' => 'error', 'message' => 'Error en cURL: ' . curl_error($ch), ], 500); } // Cerrar la sesión cURL curl_close($ch); // Decodificar la respuesta JSON de MailerSend $responseBody = json_decode($response, true); // Manejar la respuesta (puedes personalizar esto según tus necesidades) return response()->json([ 'status' => 'success', 'data' => $responseBody, ]); } catch (\Throwable $th) { Log::error(__CLASS__." => ".__FUNCTION__." => Mensaje => ".$th->getMessage()." => en la linea: ".$th->getLine()); return response()->json([ 'status' => 'error', 'message' => 'Error interno de servidor', ], 500); } } //lista de correo destinatarios public function to($list_email) { if(is_array($list_email)){ foreach ($list_email as $key => $email) { array_push($this->to, ['email' => "$email"]); } }else{ $email = $list_email; array_push($this->to, ['email' => "$email"]); } return $this; } public function subject($subject) { $this->subject = $subject; return $this; } public function attach($ruta_local_archivo){ // Lee el contenido del archivo $contenidoArchivo = file_get_contents($ruta_local_archivo); // Codifica el contenido del archivo en Base64 $fileBase64 = base64_encode($contenidoArchivo); // Obtén el nombre nativo del archivo $fileName = basename($ruta_local_archivo); // Obtén el tipo MIME del archivo $tipoMime = mime_content_type($ruta_local_archivo); $aux_attachments = [ 'content' => $fileBase64, 'filename' => $fileName, 'disposition' => 'attachment', 'mimetype' => $tipoMime, ]; array_push($this->attachments, $aux_attachments); return $this; } public function attachData($p_archivo, $p_arch_nombre, $p_arch_tipo){ $fileName = basename($p_arch_nombre); $fileBase64 = base64_encode($p_archivo); $aux_attachments = [ 'content' => $fileBase64, 'filename' => $fileName, 'disposition' => 'attachment', // 'mimetype' => 'application/pdf', ]; array_push($aux_attachments, $p_arch_tipo); array_push($this->attachments, $aux_attachments); return $this; } }