File: /var/www/gestor-horarios.bradford/app/helpers/sendgrid_helper.php
<?php
require_once ('sendgrid-php/sendgrid-php.php');
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//https://sendgrid.api-docs.io/v3.0/contacts-api-segments/create-a-segment
use SendGrid\Mail\Mail;
function sendEmail($email_data = '')
{
// ENVIO MAIL SENDGRID
$email_data['remitente'] = "contacto@automata.cl";
$email_data['usuario'] = "Evaluación de desempeño - Grupo automata";
$destinatario = $email_data['destinatario'];
$remitente = $email_data['remitente'];
$usuario = $email_data['usuario'];
$asunto = $email_data['asunto'];
$mensaje = $email_data['mensaje'];
$email = new Mail();
try {
$email->setFrom($remitente, $usuario);
$email->setSubject($asunto);
$email->addTo($destinatario, "");
} catch (Exception $e) {
//return $e;
return 0;
}
if (!empty($email_data['ruta_file'])) {
$adjunto = file_get_contents(UPLOAD_PATH . $email_data['ruta_file'] . $email_data['name_file']);
if ($adjunto) {
$email->addAttachment($adjunto, 'application/pdf', !empty($email_data['name_file_mail']) ? $email_data['name_file_mail'] : $email_data['name_file']);
} else {
}
}
$email->addContent(
"text/html", $mensaje
);
$sendgrid = new \SendGrid(SENDGRID_API_KEY);
$message_id = 0;
try {
$response = $sendgrid->send($email);
$array_headers = $response->headers();
foreach ($array_headers as $header) {
if (strpos($header, 'X-Message-Id') !== false) {
$message_id = str_replace('X-Message-Id: ', '', $header);
}
}
} catch (Exception $e) {
error_log('que pasoMAILex'.print_r(json_encode($e->getMessage()), true));
}
//error_log('que pasoMAIL'.print_r(json_encode($response), true));
return $message_id;
// $sg = new \SendGrid(SENDGRID_API_KEY);
// $from = new SendGrid\Email($usuario, $remitente);
// $subject = $asunto;
// $to = new SendGrid\Email('', $destinatario);
// $content = new SendGrid\Content("text/html", $email_data['mensaje']);
// $mail = new SendGrid\Mail($from, $subject, $to, $content);
// if (!empty($email_data['ruta_file'])) {
// $adjunto = file_get_contents(UPLOAD_PATH . $email_data['ruta_file'] . $email_data['name_file']);
// if ($adjunto) {
// $mail->addAttachment($adjunto, 'application/pdf', !empty($email_data['name_file_mail']) ? $email_data['name_file_mail'] : $email_data['name_file']);
// } else {
// }
// }
// if (count($email_data['multiple_email']) > 0) {
// foreach ($email_data['multiple_email'] as $key => $value) {
// $to_multiple = new SendGrid\Email('', trim($value));
// $mail->personalization[0]->addTo($to_multiple);
// }
// }
// if (count($email_data['multiple_cc']) > 0) {
// foreach ($email_data['multiple_cc'] as $key => $value) {
// $to_cc = new SendGrid\Email('', trim($value));
// $mail->personalization[0]->addCc($to_cc);
// }
// }
// $response = $sg->client->mail()->send()->post($mail);
// //pre($response);
// //echo $response->statusCode();
// //print_r($response->headers());
// //echo $response->body();
// return $response;
}