Completed
Push — master ( fe24f6...c47c23 )
by Wanderson
02:39
created

Email::addReplyTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Win\Mailer;
4
5
use PHPMailer;
6
use Win\File\File;
7
use Win\Mvc\Application;
8
use Win\Mvc\Block;
9
use const BASE_PATH;
10
11
/**
12
 * Envios de E-mails
13
 *
14
 * Responsável por enviar emails, simplificando a forma de envio
15
 */
16
class Email {
17
18
	/** @var Block */
19
	private $layout;
20
21
	/** @var Block|string */
22
	private $content;
23
24
	/** @var object Classe responsável pelo envio real */
25
	private $mailer;
26
27
	/**
28
	 * Cria uma mensagem de E-mail
29
	 */
30
	public function __construct() {
31
		$this->setLayout('main');
32
33
		spl_autoload_register('\Win\Mailer\Email::autoload');
34
35
		$this->mailer = new PHPMailer();
36
		$this->mailer->CharSet = 'utf-8';
37
		$this->mailer->SetLanguage('br');
38
		$this->mailer->IsMail();
39
		$this->mailer->IsHTML(true);
40
	}
41
42
	/**
43
	 * Inclui bibliotecas necessarias
44
	 * @param string $className
45
	 */
46
	public static function autoload($className) {
47
		$file = BASE_PATH . '/lib/vendor/phpmailer/class.' . strtolower($className) . '.php';
48
		if (file_exists($file)):
49
			return require $file;
50
		endif;
51
	}
52
53
	/**
54
	 * Adiciona um Destinatário
55
	 * @param string $address E-mail destinatário
56
	 * @param string $name Nome destinatário
57
	 */
58
	public function addAddress($address, $name = '') {
59
		$this->mailer->AddAddress($address, $name);
60
	}
61
62
	/**
63
	 * Define pra quem será respondido
64
	 * @param string $address
65
	 * @param string $name
66
	 */
67
	public function addReplyTo($address, $name = '') {
68
		$this->mailer->AddReplyTo($address, $name);
69
	}
70
71
	/**
72
	 * Define o Remetente
73
	 * @param string $address E-mail remetente
74
	 * @param string $name Nome remetente
75
	 */
76
	public function setFrom($address, $name = '') {
77
		$this->mailer->SetFrom($address, $name);
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 (!Application::app()->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