|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\Mail; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class for sending emails related to SPED services |
|
7
|
|
|
* |
|
8
|
|
|
* @category library |
|
9
|
|
|
* @package NFePHP\Mail\Mail |
|
10
|
|
|
* @copyright NFePHP Copyright (c) 2008-2019 |
|
11
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPLv3+ |
|
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
13
|
|
|
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+ |
|
14
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
|
15
|
|
|
* @link http://github.com/nfephp-org/sped-mail for the canonical source repository |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
use NFePHP\Mail\Base; |
|
19
|
|
|
use PHPMailer; |
|
20
|
|
|
use Html2Text\Html2Text; |
|
21
|
|
|
|
|
22
|
|
|
class Mail extends Base |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Html Body mail message |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
public $body; |
|
29
|
|
|
/** |
|
30
|
|
|
* Subject for email |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
public $subject; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor |
|
37
|
|
|
* @param \stdClass $config |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(\stdClass $config, \PHPMailer\PHPMailer\PHPMailer $mailer = null) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->mail = $mailer; |
|
42
|
|
|
if (is_null($mailer)) { |
|
43
|
|
|
$this->mail = new \PHPMailer\PHPMailer\PHPMailer(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->config = $config; |
|
47
|
|
|
$this->loadService($config); |
|
48
|
|
|
$this->fields = new \stdClass(); |
|
49
|
|
|
$this->fields->destinatario = ''; |
|
50
|
|
|
$this->fields->data = ''; |
|
51
|
|
|
$this->fields->numero = ''; |
|
52
|
|
|
$this->fields->valor = 0; |
|
53
|
|
|
$this->fields->chave = ''; |
|
54
|
|
|
$this->fields->data = ''; |
|
55
|
|
|
$this->fields->correcao = ''; |
|
56
|
|
|
$this->fields->conduso = ''; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Load parameters to PHPMailer class |
|
61
|
|
|
* @param \stdClass $config |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function loadService(\stdClass $config) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->mail->CharSet = 'UTF-8'; |
|
66
|
|
|
$this->mail->isSMTP(); |
|
67
|
|
|
$this->mail->Host = $config->host; |
|
68
|
|
|
$this->mail->SMTPAuth = $config->smtpauth; |
|
69
|
|
|
if (!empty($config->user) && !empty($config->password)) { |
|
70
|
|
|
$this->mail->SMTPAuth = true; |
|
71
|
|
|
$this->mail->Username = $config->user; |
|
72
|
|
|
$this->mail->Password = $config->password; |
|
73
|
|
|
} |
|
74
|
|
|
$this->mail->SMTPSecure = $config->secure; |
|
75
|
|
|
$this->mail->Port = $config->port; |
|
76
|
|
|
if (!empty($config->authtype)) { |
|
77
|
|
|
$this->mail->AuthType = $config->authtype; |
|
78
|
|
|
} |
|
79
|
|
|
if (!empty($config->timeout)) { |
|
80
|
|
|
$this->mail->Timeout = $config->timeout; |
|
81
|
|
|
} |
|
82
|
|
|
if (!empty($config->timelimit)) { |
|
83
|
|
|
$this->mail->Timelimit = $config->timelimit; |
|
84
|
|
|
} |
|
85
|
|
|
<<<<<<< HEAD |
|
|
|
|
|
|
86
|
|
|
if (isset($config->smtpoptions) && is_array($config->smtpoptions)) { |
|
87
|
|
|
======= |
|
88
|
|
|
if (is_array($config->smtpoptions)) { |
|
89
|
|
|
>>>>>>> 719185306c71d2a7ba7452771a9f03f92843fb36 |
|
90
|
|
|
$this->mail->SMTPOptions = $config->smtpoptions; |
|
91
|
|
|
} |
|
92
|
|
|
if (!empty($config->smtpdebug)) { |
|
93
|
|
|
$this->mail->SMTPDebug = $config->smtpdebug; |
|
94
|
|
|
} |
|
95
|
|
|
if (!empty($config->debugoutput)) { |
|
96
|
|
|
$this->mail->Debugoutput = $config->debugoutput; |
|
97
|
|
|
} |
|
98
|
|
|
$this->mail->setFrom($config->from, $config->fantasy); |
|
99
|
|
|
$this->mail->addReplyTo($config->replyTo, $config->replyName); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Sets a template for body mail |
|
104
|
|
|
* If no template is passed, it will be used a standard template |
|
105
|
|
|
* see Base::class |
|
106
|
|
|
* @param string $htmlTemplate |
|
107
|
|
|
*/ |
|
108
|
|
|
public function loadTemplate($htmlTemplate) |
|
109
|
|
|
{ |
|
110
|
|
|
if ($htmlTemplate != '') { |
|
111
|
|
|
$this->template = $htmlTemplate; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Load the documents to send |
|
117
|
|
|
* XML document is required, but PDF is not |
|
118
|
|
|
* @param string $xml content or path NFe, CTe or CCe in XML |
|
119
|
|
|
* @param string $pdf content or path document from NFe, CTe or CCe |
|
120
|
|
|
*/ |
|
121
|
|
|
public function loadDocuments($xml, $pdf = '') |
|
122
|
|
|
{ |
|
123
|
|
|
$this->xml = trim($xml); |
|
124
|
|
|
$this->pdf = trim($pdf); |
|
125
|
|
|
if ($this->isFile($this->xml)) { |
|
126
|
|
|
$this->xml = file_get_contents($this->xml); |
|
127
|
|
|
} |
|
128
|
|
|
if ($this->isFile($this->pdf)) { |
|
129
|
|
|
$this->pdf = file_get_contents($this->pdf); |
|
130
|
|
|
} |
|
131
|
|
|
//get xml data |
|
132
|
|
|
$this->getXmlData($this->xml); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Checks if given data is file |
|
137
|
|
|
* @param string $value |
|
138
|
|
|
* @return boolean |
|
139
|
|
|
*/ |
|
140
|
|
|
private function isFile($value) |
|
141
|
|
|
{ |
|
142
|
|
|
//se a string for maior que 500bytes, provavelmente é o conteudo |
|
143
|
|
|
//de um xml ou de um PDF então verificar |
|
144
|
|
|
if (strlen($value) > 500 |
|
145
|
|
|
&& (substr($value, 0, 1) == '<' || substr($value, 0, 5) == "%PDF-") |
|
146
|
|
|
) { |
|
147
|
|
|
return false; |
|
148
|
|
|
} |
|
149
|
|
|
//caso contrario pode ser um path muito longo !! |
|
150
|
|
|
$value = strval(str_replace("\0", "", $value)); |
|
151
|
|
|
return is_file($value); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Send mail |
|
156
|
|
|
* If no parameter was passed, only the email address contained in |
|
157
|
|
|
* the xml will be used |
|
158
|
|
|
* @param array $addresses |
|
159
|
|
|
* @return boolean |
|
160
|
|
|
* @throws \RuntimeException |
|
161
|
|
|
*/ |
|
162
|
|
|
public function send(array $addresses = [], $include = true) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->setAddresses($addresses, $include); |
|
165
|
|
|
if (empty($this->addresses)) { |
|
166
|
|
|
$msg = 'Não foram passados endereços de email validos !!'; |
|
167
|
|
|
throw new \RuntimeException($msg); |
|
168
|
|
|
} |
|
169
|
|
|
foreach ($this->addresses as $address) { |
|
170
|
|
|
$this->mail->addAddress($address); |
|
171
|
|
|
} |
|
172
|
|
|
$body = $this->render(); |
|
173
|
|
|
$this->mail->isHTML(true); |
|
174
|
|
|
$this->mail->Subject = $this->subject; |
|
175
|
|
|
$this->mail->Body = $body; |
|
176
|
|
|
$this->mail->AltBody = Html2Text::convert($body); |
|
177
|
|
|
$this->attach(); |
|
178
|
|
|
if (!$this->mail->send()) { |
|
179
|
|
|
$msg = 'A mensagem não pode ser enviada. Mail Error: ' . $this->mail->ErrorInfo; |
|
180
|
|
|
throw new \RuntimeException($msg); |
|
181
|
|
|
} |
|
182
|
|
|
$this->mail->clearAllRecipients(); |
|
183
|
|
|
$this->mail->clearAttachments(); |
|
184
|
|
|
return true; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Configure and send documents |
|
189
|
|
|
* @param \stdClass $config |
|
190
|
|
|
* @param string $xml |
|
191
|
|
|
* @param string $pdf |
|
192
|
|
|
* @param array $addresses |
|
193
|
|
|
* @param string $htmltemplate |
|
194
|
|
|
* @param PHPMailer $mailer |
|
195
|
|
|
* @return Mail |
|
196
|
|
|
*/ |
|
197
|
|
|
public static function sendMail( |
|
198
|
|
|
\stdClass $config, |
|
199
|
|
|
$xml, |
|
200
|
|
|
$pdf = '', |
|
201
|
|
|
array $addresses = [], |
|
202
|
|
|
$htmltemplate = '', |
|
203
|
|
|
PHPMailer $mailer = null |
|
204
|
|
|
) { |
|
205
|
|
|
$mail = new static($config, $mailer); |
|
206
|
|
|
$mail->loadDocuments($xml, $pdf); |
|
207
|
|
|
$mail->loadTemplate($htmltemplate); |
|
208
|
|
|
$mail->send($addresses, false); |
|
209
|
|
|
return $mail; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|