Passed
Branch tests1.5 (af713c)
by Wanderson
01:19
created

Email::send()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 0
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Message;
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
	/** @var string|null */
28
	private $error = null;
29
30
	/**
31
	 * Cria uma mensagem de E-mail
32
	 */
33
	public function __construct() {
34
		$this->setLayout('main');
35
36
		$this->mailer = new PHPMailer();
37
		$this->mailer->CharSet = 'utf-8';
38
		$this->mailer->SetLanguage('br');
39
		$this->mailer->IsMail();
40
		$this->mailer->IsHTML(true);
41
	}
42
43
	/**
44
	 * Adiciona um Destinatário
45
	 * @param string $address E-mail destinatário
46
	 * @param string $name Nome destinatário
47
	 */
48
	public function addAddress($address, $name = '') {
49
		$this->mailer->AddAddress($address, $name);
50
	}
51
52
	/**
53
	 * Define pra quem será respondido
54
	 * @param string $address
55
	 * @param string $name
56
	 */
57
	public function addReplyTo($address, $name = '') {
58
		$this->mailer->AddReplyTo($address, $name);
59
	}
60
61
	/**
62
	 * Define o Remetente
63
	 * @param string $address E-mail remetente
64
	 * @param string $name Nome remetente
65
	 */
66
	public function setFrom($address, $name = '') {
67
		$this->mailer->SetFrom($address, $name);
68
		$this->mailer->ClearReplyTos();
69
	}
70
71
	/**
72
	 * Define qual será o arquivo de layout
73
	 *
74
	 * @param string $layout Nome do arquivo de layout
75
	 */
76
	public function setLayout($layout) {
77
		$file = 'email/' . $layout;
78
		$this->layout = new Block($file, ['email' => $this]);
79
	}
80
81
	/** @return string */
82
	public function __toString() {
83
		return $this->layout->toString();
84
	}
85
86
	/**
87
	 * Define o conteúdo do E-mail
88
	 * que pode ser uma string ou um bloco
89
	 * @param string|Block $content
90
	 */
91
	public function setContent($content) {
92
		$this->content = $content;
93
	}
94
95
	/**
96
	 * Define o Assunto
97
	 * @param string $subject
98
	 */
99
	public function setSubject($subject) {
100
		$this->mailer->Subject = $subject;
101
	}
102
103
	/**
104
	 * Define o idioma
105
	 * @param string $lang
106
	 */
107
	public function setLanguage($lang) {
108
		$this->mailer->SetLanguage($lang);
109
	}
110
111
	/**
112
	 * Retorna o E-mail do Destinatário
113
	 * @return string
114
	 */
115
	public function getFrom() {
116
		return $this->mailer->From;
117
	}
118
119
	/**
120
	 * Retorna o Nome do destinatário
121
	 * @return string
122
	 */
123
	public function getFromName() {
124
		return $this->mailer->FromName;
125
	}
126
127
	/** @return string[] */
128
	public function getAdresses() {
129
		return $this->mailer->getAllRecipientAddresses();
130
	}
131
132
	/** @return string[] */
133
	public function getReplyToAddresses() {
134
		return $this->mailer->getReplyToAddresses();
135
	}
136
137
	/**
138
	 * Retorna o conteúdo do E-mail
139
	 * @return string
140
	 */
141
	public function getContent() {
142
		return $this->content;
143
	}
144
145
	/**
146
	 * Retorna o Assunto
147
	 * @return string
148
	 */
149
	public function getSubject() {
150
		return $this->mailer->Subject;
151
	}
152
153
	/**
154
	 * Retorna o erro caso 'send()' tenha retornado FALSE
155
	 * @return string|null
156
	 */
157
	public function getError() {
158
		return $this->error;
159
	}
160
161
	/**
162
	 * Envia o E-mail
163
	 *
164
	 * @return null|string Retorna null ou string de erro
165
	 */
166
	public function send() {
167
		$send = false;
168
		if (!Server::isLocalHost() || static::$sendOnLocalHost) {
169
			$this->mailer->Body = $this->layout->toString();
170
			$send = $this->mailer->Send();
171
			$this->mailer->ClearAllRecipients();
172
			$this->mailer->ClearAttachments();
173
			if (!$send) {
174
				$this->error = 'Houve um erro ao enviar o e-mail.<br /><span style="display:none">' . $this->mailer->ErrorInfo . '</span>';
175
			}
176
		} else {
177
			$send = $this->saveOnDisk();
178
		}
179
		return $send;
180
	}
181
182
	/**
183
	 * Salva o E-mail em um arquivo
184
	 * @return boolean
185
	 */
186
	private function saveOnDisk() {
187
		$fileName = date('Y.m.d-H.i.s-') . strtolower(md5(uniqid(time()))) . '.html';
188
		$file = new File('data/email/' . $fileName);
189
		return $file->write($this->layout->toString());
190
	}
191
192
}
193