|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Ocrend Framewok 3 package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Ocrend Software <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ocrend\Kernel\Helpers; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Helper con funciones útiles para trabajar con evío de correos mediante mailer. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Brayan Narváez <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
class Emails { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Ruta en la qeu están guardados los templates. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
const TEMPLATES_ROUTE = ___ROOT___ . 'assets/mail/'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Lista de plantillas. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
const TEMPLATES = [ |
|
35
|
|
|
'tpl-btn.html', |
|
36
|
|
|
'tpl-no-btn.html' |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Carga una plantilla y sistituye su contenido. |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $content: Contenido de cada elemento |
|
43
|
|
|
* @param int $template: Plantilla seleccionada |
|
44
|
|
|
* |
|
45
|
|
|
* @return string plantilla llena |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function loadTemplate(array $content, int $template) : string { |
|
48
|
|
|
# Verificar que existe la plantilla |
|
49
|
|
|
if(!array_key_exists($template,self::TEMPLATES)) { |
|
50
|
|
|
throw new \RuntimeException('La plantilla seleccionada no se encuentra.'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
# Cargar contenido |
|
54
|
|
|
$tpl = Files::read_file(self::TEMPLATES_ROUTE . self::TEMPLATES[$template]); |
|
55
|
|
|
|
|
56
|
|
|
# Reempalzar contenido |
|
57
|
|
|
foreach($content as $index => $html) { |
|
58
|
|
|
$tpl = str_replace($index,$html,$tpl); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $tpl; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Envía un correo electrónico utilizando mailer |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $dest: Arreglo con la forma array( |
|
68
|
|
|
* 'email destinatario 1' => 'nombre destinatario 1', |
|
69
|
|
|
* 'email destinatario 2' => 'nombre destinatario 2' |
|
70
|
|
|
* ) |
|
71
|
|
|
* @param array $content: Arreglo con el contenido por secciones con la forma |
|
72
|
|
|
* array( |
|
73
|
|
|
* '{{title}}' => 'Título', |
|
74
|
|
|
* '{{content}}' => '<p>Contenido</p><p>Etc...</p>', |
|
75
|
|
|
* ) |
|
76
|
|
|
* @param int $template: Template elegido, por defecto es el primero (0) |
|
77
|
|
|
* @param array $adj: Arreglo con direccion local de los adjuntos a enviar, con la forma array( |
|
78
|
|
|
* 'ruta archivo 1', |
|
79
|
|
|
* 'ruta archivo 2' |
|
80
|
|
|
* ) |
|
81
|
|
|
* |
|
82
|
|
|
* @throws \RuntimeException en caso de algún problema |
|
83
|
|
|
* @return bool true si fue enviado correctamente, false si no |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function send(array $dest, array $content, int $template = 0, array $adj = []) : bool { |
|
86
|
|
|
global $config; |
|
87
|
|
|
|
|
88
|
|
|
# Hack para cuando algun servidor tenga un SSL no válido, por ejemplo localhost |
|
89
|
|
|
$https['ssl']['verify_peer'] = false; |
|
|
|
|
|
|
90
|
|
|
$https['ssl']['verify_peer_name'] = false; |
|
91
|
|
|
|
|
92
|
|
|
# Verificar si están llenos los campos |
|
93
|
|
|
if(!$config['build']['production'] && Functions::emp($config['mailer']['host'])) { |
|
94
|
|
|
throw new \RuntimeException('Los datos de mailer, en Ocrend.ini.yml están vacíos.'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
# Transporte |
|
98
|
|
|
$transport = (new \Swift_SmtpTransport($config['mailer']['host'], $config['mailer']['port'], 'tls')) |
|
99
|
|
|
->setUsername($config['mailer']['user']) |
|
100
|
|
|
->setPassword($config['mailer']['pass']) |
|
101
|
|
|
->setStreamOptions($https); |
|
102
|
|
|
|
|
103
|
|
|
# Mailer |
|
104
|
|
|
$mailer = new \Swift_Mailer($transport); |
|
105
|
|
|
|
|
106
|
|
|
# El mensaje |
|
107
|
|
|
$message = new \Swift_Message(); |
|
108
|
|
|
$message->setSubject(array_key_exists('{{title}}',$content) ? $content['{{title}}'] : $config['build']['name']); |
|
109
|
|
|
$message->setBody(self::loadTemplate($content,$template), 'text/html'); |
|
110
|
|
|
$message->setFrom([ $config['mailer']['user'] => $config['build']['name']]); |
|
111
|
|
|
$message->setTo($dest); |
|
112
|
|
|
|
|
113
|
|
|
# Adjuntos |
|
114
|
|
|
if (sizeof($adj)) { |
|
115
|
|
|
foreach ($adj as $ruta) { |
|
116
|
|
|
$message->attach(\Swift_Attachment::fromPath($ruta)); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
# Verificar respuesta |
|
121
|
|
|
return (bool) $mailer->send($message); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|