Mailer::addCC()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Win\InfraServices;
4
5
use Exception;
6
use PHPMailer\PHPMailer\PHPMailer;
7
use Win\Common\Email;
8
use Win\Common\Server;
9
use Win\Common\Template;
10
use Win\Repositories\Filesystem;
11
12
/**
13
 * Envio de Emails
14
 *
15
 * Responsável por enviar Emails
16
 */
17
class Mailer
18
{
19
	const DIRECTORY = 'data/emails';
20
21
	/** @var PHPMailer */
22
	private $mailer;
23
24
	/** @var bool */
25
	public static $sendOnLocalHost = false;
26
27
	/**
28
	 * Instancia o serviço de E-mail
29
	 */
30
	public function __construct()
31
	{
32
		$this->mailer = new PHPMailer();
33
		$this->mailer->CharSet = 'utf-8';
34
		$this->mailer->IsMail();
35
		$this->mailer->IsHTML(true);
36
	}
37
38
	/**
39
	 * Define o idioma
40
	 * @param string $language
41
	 */
42
	public function setLanguage($language)
43
	{
44
		$this->mailer->SetLanguage($language);
45
	}
46
47
	/**
48
	 * Define o assunto
49
	 * @param string $subject
50
	 */
51
	public function setSubject($subject)
52
	{
53
		$this->mailer->Subject = $subject;
54
	}
55
56
	/**
57
	 * Define o remetente
58
	 * @param string $address
59
	 * @param string $name
60
	 */
61
	public function setFrom($address, $name = '')
62
	{
63
		$this->mailer->From = $address;
64
		$this->mailer->FromName = $name;
65
	}
66
67
	/**
68
	 * Add destinatário
69
	 * @param string $address
70
	 * @param string $name
71
	 */
72
	public function addTo($address, $name = '')
73
	{
74
		$this->mailer->addAddress($address, $name);
75
	}
76
77
	/**
78
	 * Add cópia
79
	 * @param string $address
80
	 * @param string $name
81
	 */
82
	public function addCC($address, $name = '')
83
	{
84
		$this->mailer->addCC($address, $name);
85
	}
86
87
	/**
88
	 * Add cópia oculta
89
	 * @param string $address
90
	 * @param string $name
91
	 */
92
	public function addBCC($address, $name = '')
93
	{
94
		$this->mailer->addBCC($address, $name);
95
	}
96
97
	/**
98
	 * Add responder para
99
	 * @param string $address
100
	 * @param string $name
101
	 */
102
	public function addReplyTo($address, $name = '')
103
	{
104
		$this->mailer->addReplyTo($address, $name);
105
	}
106
107
	/**
108
	 * Envia o E-mail
109
	 * @param string|Email $body
110
	 */
111
	public function send($body)
112
	{
113
		$this->setBody($body);
114
115
		if (!Server::isLocalHost() || static::$sendOnLocalHost) {
116
			$send = $this->mailer->Send();
117
			$this->flush();
118
119
			if (!$send) {
120
				throw new Exception('Houve um erro ao enviar o e-mail.<br />'
121
					. '<span style="display:none">' . $this->mailer->ErrorInfo . '</span>');
122
			}
123
		} else {
124
			$this->saveOnDisk();
125
		}
126
	}
127
128
	/**
129
	 * Define o corpo do email
130
	 * @param string|Email $body
131
	 */
132
	protected function setBody($body)
133
	{
134
		$this->mailer->Body = (string) $body;
135
	}
136
137
	/**
138
	 * Limpa dados
139
	 */
140
	private function flush()
141
	{
142
		$this->mailer->ClearAllRecipients();
143
		$this->mailer->ClearAttachments();
144
	}
145
146
	/**
147
	 * Salva o corpo do E-mail em um arquivo
148
	 * @return bool
149
	 */
150
	private function saveOnDisk()
151
	{
152
		$fs = new Filesystem();
153
		$name = date('Y.m.d-H.i.s-') . strtolower(md5(uniqid(time()))) . '.html';
154
		$body = $this->mailer->Body;
155
156
		return $fs->write(static::DIRECTORY . "/$name", $body);
157
	}
158
}
159