Completed
Push — master ( 67c338...c7b167 )
by Wanderson
04:24
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 Win\Mvc\Block;
6
use Win\Mvc\Application;
7
8
/**
9
 * Envios de E-mails
10
 *
11
 * Responsável por enviar emails, simplificando a forma de envio
12
 */
13
class Email {
14
15
	/** @var Block */
16
	private $layout;
17
18
	/** @var Block|string */
19
	private $body;
20
21
	/** @var object Classe responsável pelo envio real */
22
	private $mailer;
23
24
	/**
25
	 * Cria uma mensagem de E-mail
26
	 */
27
	public function __construct() {
28
		$this->setLayout('main');
29
30
		spl_autoload_register('\Win\Mailer\Email::autoload');
31
32
		$this->mailer = new \PHPMailer();
33
		$this->mailer->CharSet = 'utf-8';
34
		$this->mailer->SetLanguage('br');
35
		$this->mailer->IsMail();
36
		$this->mailer->IsHTML(true);
37
	}
38
39
	/**
40
	 * Inclui bibliotecas necessarias
41
	 * @param string $className
42
	 */
43
	public static function autoload($className) {
44
		$file = BASE_PATH . '/lib/vendor/phpmailer/class.' . strtolower($className) . '.php';
45
		if (file_exists($file)):
46
			return require $file;
47
		endif;
48
	}
49
50
	/**
51
	 * Adiciona um Destinatário
52
	 * @param string $address E-mail destinatário
53
	 * @param string $name Nome destinatário
54
	 */
55
	public function addAddress($address, $name = '') {
56
		$this->mailer->AddAddress($address, $name);
57
	}
58
59
	/**
60
	 * Define o Remetente
61
	 * @param string $address E-mail remetente
62
	 * @param string $name Nome remetente
63
	 */
64
	public function setFrom($address, $name = '') {
65
		$this->mailer->SetFrom($address, $name);
66
		$this->mailer->ClearReplyTos();
67
	}
68
69
	/**
70
	 * Define pra quem será respondido
71
	 * @param string $address
72
	 */
73
	public function addReplyTo($address, $name = '') {
74
		$this->mailer->AddReplyTo($address, $name);
75
	}
76
77
	/**
78
	 * Define qual será o arquivo de layout
79
	 *
80
	 * @param string $layout Nome do arquivo de layout
81
	 */
82
	public function setLayout($layout) {
83
		$file = 'email/' . $layout;
84
		$this->layout = new Block($file, ['email' => $this]);
85
	}
86
87
	/**
88
	 * Define o conteudo do E-mail
89
	 * que pode ser uma string ou um bloco
90
	 * @param string|Block $body
91
	 */
92
	public function setBody($body) {
93
		$this->body = $body;
94
	}
95
96
	/**
97
	 * Define o Assunto
98
	 * @param string $subject
99
	 */
100
	public function setSubject($subject) {
101
		$this->mailer->Subject = $subject;
102
	}
103
104
	/**
105
	 * Define o idioma
106
	 * @param string $lang
107
	 */
108
	public function setLanguage($lang) {
109
		$this->mailer->SetLanguage($lang);
110
	}
111
112
	/**
113
	 * Retorna o E-mail do Destinatário
114
	 * @return string
115
	 */
116
	public function getFrom() {
117
		return $this->mailer->From;
118
	}
119
120
	/**
121
	 * Retorna o Nome do destinatário
122
	 * @return string
123
	 */
124
	public function getFromName() {
125
		return $this->mailer->FromName;
126
	}
127
128
	/**
129
	 * Retorna o conteudo do E-mail
130
	 * @return string
131
	 */
132
	public function getBody() {
133
		return $this->body;
134
	}
135
136
	/**
137
	 * Retorna o Assunto
138
	 * @return string
139
	 */
140
	public function getSubject() {
141
		return $this->mailer->Subject;
142
	}
143
144
	/**
145
	 * Envia o email
146
	 *
147
	 * No localhost será mostrado o conteudo do E-mail
148
	 * @return null|string Retorna null ou string de erro
149
	 */
150
	public function send() {
151
		if (!Application::app()->isLocalHost()) {
152
			$this->mailer->Body = $this->layout->toString();
153
			$send = $this->mailer->Send();
154
			$this->mailer->ClearAllRecipients();
155
			$this->mailer->ClearAttachments();
156
			if (!$send) {
157
				return 'Houve um erro ao enviar o e-mail.<br /><span style="display:none">' . $this->mailer->ErrorInfo . '</span>';
158
			}
159
			return null;
160
		}
161
		$this->layout->toHtml();
162
		return null;
163
	}
164
165
}
166