1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Mailer; |
4
|
|
|
|
5
|
|
|
use Win\Mvc\Block; |
6
|
|
|
use Win\Mvc\Application; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Envios de E-mails |
10
|
|
|
* |
11
|
|
|
* Responsável por enviar emails, simplificando a forma de envio |
12
|
|
|
*/ |
13
|
|
|
class Email { |
14
|
|
|
|
15
|
|
|
/** @var Block */ |
16
|
|
|
private $layout; |
17
|
|
|
|
18
|
|
|
/** @var Block|string */ |
19
|
|
|
private $content; |
20
|
|
|
|
21
|
|
|
/** @var object Classe responsável pelo envio real */ |
22
|
|
|
private $mailer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Cria uma mensagem de E-mail |
26
|
|
|
*/ |
27
|
|
|
public function __construct() { |
28
|
|
|
$this->setLayout('main'); |
29
|
|
|
|
30
|
|
|
spl_autoload_register('\Win\Mailer\Email::autoload'); |
31
|
|
|
|
32
|
|
|
$this->mailer = new \PHPMailer(); |
33
|
|
|
$this->mailer->CharSet = 'utf-8'; |
34
|
|
|
$this->mailer->SetLanguage('br'); |
35
|
|
|
$this->mailer->IsMail(); |
36
|
|
|
$this->mailer->IsHTML(true); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Inclui bibliotecas necessarias |
41
|
|
|
* @param string $className |
42
|
|
|
*/ |
43
|
|
|
public static function autoload($className) { |
44
|
|
|
$file = BASE_PATH . '/lib/vendor/phpmailer/class.' . strtolower($className) . '.php'; |
45
|
|
|
if (file_exists($file)): |
46
|
|
|
return require $file; |
47
|
|
|
endif; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Adiciona um Destinatário |
52
|
|
|
* @param string $address E-mail destinatário |
53
|
|
|
* @param string $name Nome destinatário |
54
|
|
|
*/ |
55
|
|
|
public function addAddress($address, $name = '') { |
56
|
|
|
$this->mailer->AddAddress($address, $name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Define o Remetente |
61
|
|
|
* @param string $address E-mail remetente |
62
|
|
|
* @param string $name Nome remetente |
63
|
|
|
*/ |
64
|
|
|
public function setFrom($address, $name = '') { |
65
|
|
|
$this->mailer->SetFrom($address, $name); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Define qual será o arquivo de layout |
70
|
|
|
* |
71
|
|
|
* @param string $layout Nome do arquivo de layout |
72
|
|
|
*/ |
73
|
|
|
public function setLayout($layout) { |
74
|
|
|
$file = 'email/' . $layout; |
75
|
|
|
$this->layout = new Block($file, ['email' => $this]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Define o conteudo do E-mail |
80
|
|
|
* que pode ser uma string ou um bloco |
81
|
|
|
* @param string|Block $content |
82
|
|
|
*/ |
83
|
|
|
public function setContent($content) { |
84
|
|
|
$this->content = $content; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Define o Assunto |
89
|
|
|
* @param string $subject |
90
|
|
|
*/ |
91
|
|
|
public function setSubject($subject) { |
92
|
|
|
$this->mailer->Subject = $subject; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Define o idioma |
97
|
|
|
* @param string $lang |
98
|
|
|
*/ |
99
|
|
|
public function setLanguage($lang) { |
100
|
|
|
$this->mailer->SetLanguage($lang); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Retorna o E-mail do Destinatário |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getFrom() { |
108
|
|
|
return $this->mailer->From; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Retorna o Nome do destinatário |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getFromName() { |
116
|
|
|
return $this->mailer->FromName; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Retorna o conteudo do E-mail |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getContent() { |
124
|
|
|
return $this->content; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Retorna o Assunto |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getSubject() { |
132
|
|
|
return $this->mailer->Subject; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Envia o email |
137
|
|
|
* |
138
|
|
|
* No localhost será mostrado o conteudo do E-mail |
139
|
|
|
* @return null|string Retorna null ou string de erro |
140
|
|
|
*/ |
141
|
|
|
public function send() { |
142
|
|
|
if (!Application::app()->isLocalHost()) { |
143
|
|
|
$this->mailer->Body = $this->layout->toString(); |
144
|
|
|
$send = $this->mailer->Send(); |
145
|
|
|
$this->mailer->ClearAllRecipients(); |
146
|
|
|
$this->mailer->ClearAttachments(); |
147
|
|
|
if (!$send) { |
148
|
|
|
return 'Houve um erro ao enviar o e-mail.<br /><span style="display:none">' . $this->mailer->ErrorInfo . '</span>'; |
149
|
|
|
} |
150
|
|
|
return null; |
151
|
|
|
} |
152
|
|
|
$this->layout->toHtml(); |
153
|
|
|
return null; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|