mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Merge branch 'fix/email_service' into 'main'
Añadido archivos EmailServies.php See merge request jjimenez/safekat!617
This commit is contained in:
58
ci4/app/Services/EmailService.php
Normal file
58
ci4/app/Services/EmailService.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Models\Sistema\SettingsModel;
|
||||||
|
use Config\Services;
|
||||||
|
|
||||||
|
class EmailService
|
||||||
|
{
|
||||||
|
public function send(string $subject, string $body, $recipient): bool
|
||||||
|
{
|
||||||
|
|
||||||
|
$skEnv = env('sk_environment', 'production'); // fallback a producción si no está definido
|
||||||
|
|
||||||
|
// Si no estamos en producción, forzar el destinatario a uno fijo
|
||||||
|
if ($skEnv !== 'production') {
|
||||||
|
$recipient = 'imnavajas@coit.es'; // <-- Cambia por el correo de pruebas
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$settings_model = new SettingsModel();
|
||||||
|
$config = $settings_model->first()->toArray();
|
||||||
|
$gateway = $config['email_gateway'];
|
||||||
|
$body = html_entity_decode($body);
|
||||||
|
|
||||||
|
if ($gateway === 'smtp') {
|
||||||
|
try {
|
||||||
|
$email = Services::email();
|
||||||
|
|
||||||
|
$emailConfig = [
|
||||||
|
'protocol' => $config['email_gateway'],
|
||||||
|
'SMTPHost' => $config['email_smtp'],
|
||||||
|
'SMTPUser' => $config['email_address'],
|
||||||
|
'SMTPPass' => $config['email_pass'],
|
||||||
|
'SMTPPort' => (int) $config['email_port'],
|
||||||
|
'SMTPCrypto' => $config['email_cert'] === 'none' ? '' : $config['email_cert'],
|
||||||
|
'SMTPTimeout' => 15,
|
||||||
|
'mailType' => 'html',
|
||||||
|
'wordWrap' => true,
|
||||||
|
];
|
||||||
|
|
||||||
|
$email->initialize($emailConfig);
|
||||||
|
|
||||||
|
$email->setFrom($config['email_address'], $config['email_name']);
|
||||||
|
$email->setTo($recipient);
|
||||||
|
$email->setSubject($subject);
|
||||||
|
$email->setMessage($body);
|
||||||
|
|
||||||
|
return $email->send();
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
log_message('error', 'EmailService failed: ' . $e->getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user