1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Mailer; |
4
|
|
|
|
5
|
|
|
use PHPMailer; |
6
|
|
|
use Win\File\File; |
7
|
|
|
use Win\Mvc\Block; |
8
|
|
|
use Win\Request\Server; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Envios de E-mails |
12
|
|
|
* |
13
|
|
|
* Responsável por enviar emails, simplificando a forma de envio |
14
|
|
|
*/ |
15
|
|
|
class Email { |
16
|
|
|
|
17
|
|
|
/** @var Block */ |
18
|
|
|
private $layout; |
19
|
|
|
|
20
|
|
|
/** @var Block|string */ |
21
|
|
|
private $content; |
22
|
|
|
|
23
|
|
|
/** @var \PHPMailer Classe responsável pelo envio real */ |
24
|
|
|
private $mailer; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Cria uma mensagem de E-mail |
28
|
|
|
*/ |
29
|
|
|
public function __construct() { |
30
|
|
|
$this->setLayout('main'); |
31
|
|
|
|
32
|
|
|
spl_autoload_register('\Win\Mailer\Email::autoload'); |
33
|
|
|
|
34
|
|
|
$this->mailer = new PHPMailer(); |
35
|
|
|
$this->mailer->CharSet = 'utf-8'; |
36
|
|
|
$this->mailer->SetLanguage('br'); |
37
|
|
|
$this->mailer->IsMail(); |
38
|
|
|
$this->mailer->IsHTML(true); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Inclui bibliotecas necessarias |
43
|
|
|
* @param string $className |
44
|
|
|
*/ |
45
|
|
|
public static function autoload($className) { |
46
|
|
|
$file = BASE_PATH . '/lib/vendor/phpmailer/class.' . strtolower($className) . '.php'; |
47
|
|
|
if (file_exists($file)): |
48
|
|
|
return require $file; |
49
|
|
|
endif; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Adiciona um Destinatário |
54
|
|
|
* @param string $address E-mail destinatário |
55
|
|
|
* @param string $name Nome destinatário |
56
|
|
|
*/ |
57
|
|
|
public function addAddress($address, $name = '') { |
58
|
|
|
$this->mailer->AddAddress($address, $name); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Define pra quem será respondido |
63
|
|
|
* @param string $address |
64
|
|
|
* @param string $name |
65
|
|
|
*/ |
66
|
|
|
public function addReplyTo($address, $name = '') { |
67
|
|
|
$this->mailer->AddReplyTo($address, $name); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Define o Remetente |
72
|
|
|
* @param string $address E-mail remetente |
73
|
|
|
* @param string $name Nome remetente |
74
|
|
|
*/ |
75
|
|
|
public function setFrom($address, $name = '') { |
76
|
|
|
$this->mailer->SetFrom($address, $name); |
77
|
|
|
$this->mailer->ClearReplyTos(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Define qual será o arquivo de layout |
82
|
|
|
* |
83
|
|
|
* @param string $layout Nome do arquivo de layout |
84
|
|
|
*/ |
85
|
|
|
public function setLayout($layout) { |
86
|
|
|
$file = 'email/' . $layout; |
87
|
|
|
$this->layout = new Block($file, ['email' => $this]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Define o conteudo do E-mail |
92
|
|
|
* que pode ser uma string ou um bloco |
93
|
|
|
* @param string|Block $content |
94
|
|
|
*/ |
95
|
|
|
public function setContent($content) { |
96
|
|
|
$this->content = $content; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Define o Assunto |
101
|
|
|
* @param string $subject |
102
|
|
|
*/ |
103
|
|
|
public function setSubject($subject) { |
104
|
|
|
$this->mailer->Subject = $subject; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Define o idioma |
109
|
|
|
* @param string $lang |
110
|
|
|
*/ |
111
|
|
|
public function setLanguage($lang) { |
112
|
|
|
$this->mailer->SetLanguage($lang); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Retorna o E-mail do Destinatário |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function getFrom() { |
120
|
|
|
return $this->mailer->From; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Retorna o Nome do destinatário |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getFromName() { |
128
|
|
|
return $this->mailer->FromName; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Retorna o conteudo do E-mail |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getContent() { |
136
|
|
|
return $this->content; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Retorna o Assunto |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getSubject() { |
144
|
|
|
return $this->mailer->Subject; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Envia o email |
149
|
|
|
* |
150
|
|
|
* No localhost será mostrado o conteudo do E-mail |
151
|
|
|
* @return null|string Retorna null ou string de erro |
152
|
|
|
*/ |
153
|
|
|
public function send() { |
154
|
|
|
if (!Server::isLocalHost()) { |
155
|
|
|
$this->mailer->Body = $this->layout->toString(); |
156
|
|
|
$send = $this->mailer->Send(); |
157
|
|
|
$this->mailer->ClearAllRecipients(); |
158
|
|
|
$this->mailer->ClearAttachments(); |
159
|
|
|
if (!$send) { |
160
|
|
|
return 'Houve um erro ao enviar o e-mail.<br /><span style="display:none">' . $this->mailer->ErrorInfo . '</span>'; |
161
|
|
|
} |
162
|
|
|
return null; |
163
|
|
|
} else { |
164
|
|
|
$this->saveOnDisk(); |
165
|
|
|
} |
166
|
|
|
return null; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Salva o email em um arquivo |
171
|
|
|
*/ |
172
|
|
|
private function saveOnDisk() { |
173
|
|
|
$file = new File(); |
174
|
|
|
$file->setDirectory('data/email'); |
175
|
|
|
|
176
|
|
|
$fileName = date('Y.m.d-H.i.s-') . strtolower(md5(uniqid(time()))) . '.html'; |
177
|
|
|
$file->setName($fileName); |
178
|
|
|
$file->write($this->layout->toString()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Autentica o email via SMTP |
183
|
|
|
* @param string $host |
184
|
|
|
* @param string $user |
185
|
|
|
* @param string $pass |
186
|
|
|
* @param int $port |
187
|
|
|
*/ |
188
|
|
|
public function autenticate($host, $user, $pass, $port = 587) { |
189
|
|
|
//$this->mailer->IsSMTP(); |
|
|
|
|
190
|
|
|
$this->mailer->SMTPAuth = true; |
191
|
|
|
$this->mailer->SMTPSecure = "tls"; |
192
|
|
|
$this->mailer->Host = $host; |
193
|
|
|
$this->mailer->Port = $port; |
194
|
|
|
$this->mailer->Username = $user; |
195
|
|
|
$this->mailer->Password = $pass; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.