1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Mailer; |
4
|
|
|
|
5
|
|
|
use PHPMailer\PHPMailer\PHPMailer; |
6
|
|
|
use Win\File\File; |
7
|
|
|
use Win\Mvc\Block; |
8
|
|
|
use Win\Request\Server; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Envio de E-mails |
12
|
|
|
* |
13
|
|
|
* Responsável por enviar E-mails, 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 object Classe responsável pelo envio real */ |
24
|
|
|
private $mailer; |
25
|
|
|
public static $sendOnLocalHost = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Cria uma mensagem de E-mail |
29
|
|
|
*/ |
30
|
|
|
public function __construct() { |
31
|
|
|
$this->setLayout('main'); |
32
|
|
|
|
33
|
|
|
$this->mailer = new PHPMailer(); |
34
|
|
|
$this->mailer->CharSet = 'utf-8'; |
35
|
|
|
$this->mailer->SetLanguage('br'); |
36
|
|
|
$this->mailer->IsMail(); |
37
|
|
|
$this->mailer->IsHTML(true); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Adiciona um Destinatário |
42
|
|
|
* @param string $address E-mail destinatário |
43
|
|
|
* @param string $name Nome destinatário |
44
|
|
|
*/ |
45
|
|
|
public function addAddress($address, $name = '') { |
46
|
|
|
$this->mailer->AddAddress($address, $name); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Define pra quem será respondido |
51
|
|
|
* @param string $address |
52
|
|
|
* @param string $name |
53
|
|
|
*/ |
54
|
|
|
public function addReplyTo($address, $name = '') { |
55
|
|
|
$this->mailer->AddReplyTo($address, $name); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Define o Remetente |
60
|
|
|
* @param string $address E-mail remetente |
61
|
|
|
* @param string $name Nome remetente |
62
|
|
|
*/ |
63
|
|
|
public function setFrom($address, $name = '') { |
64
|
|
|
$this->mailer->SetFrom($address, $name); |
65
|
|
|
$this->mailer->ClearReplyTos(); |
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
|
|
|
/** @return string */ |
79
|
|
|
public function __toString() { |
80
|
|
|
return $this->layout->toString(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Define o conteúdo do E-mail |
85
|
|
|
* que pode ser uma string ou um bloco |
86
|
|
|
* @param string|Block $content |
87
|
|
|
*/ |
88
|
|
|
public function setContent($content) { |
89
|
|
|
$this->content = $content; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Define o Assunto |
94
|
|
|
* @param string $subject |
95
|
|
|
*/ |
96
|
|
|
public function setSubject($subject) { |
97
|
|
|
$this->mailer->Subject = $subject; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Define o idioma |
102
|
|
|
* @param string $lang |
103
|
|
|
*/ |
104
|
|
|
public function setLanguage($lang) { |
105
|
|
|
$this->mailer->SetLanguage($lang); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Retorna o E-mail do Destinatário |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getFrom() { |
113
|
|
|
return $this->mailer->From; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Retorna o Nome do destinatário |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getFromName() { |
121
|
|
|
return $this->mailer->FromName; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Retorna o conteúdo do E-mail |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getContent() { |
129
|
|
|
return $this->content; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Retorna o Assunto |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getSubject() { |
137
|
|
|
return $this->mailer->Subject; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Envia o E-mail |
142
|
|
|
* |
143
|
|
|
* @return null|string Retorna null ou string de erro |
144
|
|
|
*/ |
145
|
|
|
public function send() { |
146
|
|
|
if (!Server::isLocalHost() || static::$sendOnLocalHost) { |
147
|
|
|
$this->mailer->Body = $this->layout->toString(); |
148
|
|
|
$send = $this->mailer->Send(); |
149
|
|
|
$this->mailer->ClearAllRecipients(); |
150
|
|
|
$this->mailer->ClearAttachments(); |
151
|
|
|
if (!$send) { |
152
|
|
|
return 'Houve um erro ao enviar o e-mail.<br /><span style="display:none">' . $this->mailer->ErrorInfo . '</span>'; |
153
|
|
|
} |
154
|
|
|
return null; |
155
|
|
|
} else { |
156
|
|
|
$this->saveOnDisk(); |
157
|
|
|
} |
158
|
|
|
return null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Salva o E-mail em um arquivo |
163
|
|
|
*/ |
164
|
|
|
private function saveOnDisk() { |
165
|
|
|
$file = new File(); |
166
|
|
|
$file->setDirectory('data/email'); |
167
|
|
|
|
168
|
|
|
$fileName = date('Y.m.d-H.i.s-') . strtolower(md5(uniqid(time()))) . '.html'; |
169
|
|
|
$file->setName($fileName); |
170
|
|
|
$file->write($this->layout->toString()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|