Passed
Branch scrutinizer (dafd44)
by Wanderson
01:40
created

Email::getError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
	/** @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
	/**
128
	 * Retorna o conteúdo do E-mail
129
	 * @return string
130
	 */
131
	public function getContent() {
132
		return $this->content;
133
	}
134
135
	/**
136
	 * Retorna o Assunto
137
	 * @return string
138
	 */
139
	public function getSubject() {
140
		return $this->mailer->Subject;
141
	}
142
143
	/**
144
	 * Retorna o erro caso 'send()' tenha retornado FALSE
145
	 * @return string|null
146
	 */
147
	public function getError() {
148
		return $this->error;
149
	}
150
151
	/**
152
	 * Envia o E-mail
153
	 *
154
	 * @return null|string Retorna null ou string de erro
155
	 */
156
	public function send() {
157
		$send = false;
158
		if (!Server::isLocalHost() || static::$sendOnLocalHost) {
159
			$this->mailer->Body = $this->layout->toString();
160
			$send = $this->mailer->Send();
161
			$this->mailer->ClearAllRecipients();
162
			$this->mailer->ClearAttachments();
163
			if (!$send) {
164
				$this->error = 'Houve um erro ao enviar o e-mail.<br /><span style="display:none">' . $this->mailer->ErrorInfo . '</span>';
165
			}
166
		} else {
167
			$send = $this->saveOnDisk();
168
		}
169
		return $send;
170
	}
171
172
	/**
173
	 * Salva o E-mail em um arquivo
174
	 * @return boolean
175
	 */
176
	private function saveOnDisk() {
177
		$file = new File();
178
		$file->setDirectory('data/email');
179
180
		$fileName = date('Y.m.d-H.i.s-') . strtolower(md5(uniqid(time()))) . '.html';
181
		$file->setName($fileName);
182
		return $file->write($this->layout->toString());
183
	}
184
185
}
186