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) 2016 |
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 $mailer = null) |
40
|
|
|
{ |
41
|
|
|
$this->mail = $mailer; |
42
|
|
|
if (is_null($mailer)) { |
43
|
|
|
$this->mail = new PHPMailer(); |
44
|
|
|
} |
45
|
|
|
$this->config = $config; |
46
|
|
|
$this->loadService($config); |
47
|
|
|
$this->fields = new \stdClass(); |
48
|
|
|
$this->fields->destinatario = ''; |
49
|
|
|
$this->fields->data = ''; |
50
|
|
|
$this->fields->numero = ''; |
51
|
|
|
$this->fields->valor = 0; |
52
|
|
|
$this->fields->chave = ''; |
53
|
|
|
$this->fields->data = ''; |
54
|
|
|
$this->fields->correcao = ''; |
55
|
|
|
$this->fields->conduso = ''; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Load parameters to PHPMailer class |
60
|
|
|
* @param \stdClass $config |
61
|
|
|
*/ |
62
|
|
|
protected function loadService(\stdClass $config) |
63
|
|
|
{ |
64
|
|
|
$this->mail->CharSet = 'UTF-8'; |
65
|
|
|
$this->mail->isSMTP(); |
66
|
|
|
$this->mail->Host = $config->host; |
67
|
|
|
$this->mail->SMTPAuth = true; |
68
|
|
|
$this->mail->Username = $config->user; |
69
|
|
|
$this->mail->Password = $config->password; |
70
|
|
|
$this->mail->SMTPSecure = $config->secure; |
71
|
|
|
$this->mail->Port = $config->port; |
72
|
|
|
$this->mail->setFrom($config->from, $config->fantasy); |
73
|
|
|
$this->mail->addReplyTo($config->replyTo, $config->replyName); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Sets a template for body mail |
78
|
|
|
* If no template is passed, it will be used a standard template |
79
|
|
|
* see Base::class |
80
|
|
|
* @param string $htmlTemplate |
81
|
|
|
*/ |
82
|
|
|
public function loadTemplate($htmlTemplate) |
83
|
|
|
{ |
84
|
|
|
if ($htmlTemplate != '') { |
85
|
|
|
$this->template = $htmlTemplate; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Load the documents to send |
91
|
|
|
* XML document is required, but PDF is not |
92
|
|
|
* @param string $xml content or path NFe, CTe or CCe in XML |
93
|
|
|
* @param string $pdf content or path document from NFe, CTe or CCe |
94
|
|
|
*/ |
95
|
|
|
public function loadDocuments($xml, $pdf = '') |
96
|
|
|
{ |
97
|
|
|
$this->xml = $xml; |
98
|
|
|
$this->pdf = $pdf; |
99
|
|
|
if ($this->isFile($xml)) { |
100
|
|
|
$this->xml = file_get_contents($xml); |
101
|
|
|
} |
102
|
|
|
if ($this->isFile($pdf)) { |
103
|
|
|
$this->pdf = file_get_contents($pdf); |
104
|
|
|
} |
105
|
|
|
//get xml data |
106
|
|
|
$this->getXmlData($this->xml); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Checks if given data is file, handles mixed input |
111
|
|
|
* @param mixed $value |
112
|
|
|
* @return boolean |
113
|
|
|
*/ |
114
|
|
|
private function isFile($value) |
115
|
|
|
{ |
116
|
|
|
$value = strval(str_replace("\0", "", $value)); |
117
|
|
|
return is_file($value); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Send mail |
122
|
|
|
* If no parameter was passed, only the email address contained in |
123
|
|
|
* the xml will be used |
124
|
|
|
* @param array $addresses |
125
|
|
|
* @return boolean |
126
|
|
|
* @throws \RuntimeException |
127
|
|
|
*/ |
128
|
|
|
public function send(array $addresses = [], $include = true) |
129
|
|
|
{ |
130
|
|
|
$this->setAddresses($addresses, $include); |
131
|
|
|
if (empty($this->addresses)) { |
132
|
|
|
$msg = 'Não foram passados endereços de email validos !!'; |
133
|
|
|
throw new \RuntimeException($msg); |
134
|
|
|
} |
135
|
|
|
foreach ($this->addresses as $address) { |
136
|
|
|
$this->mail->addAddress($address); |
137
|
|
|
} |
138
|
|
|
$body = $this->render(); |
139
|
|
|
$this->mail->isHTML(true); |
140
|
|
|
$this->mail->Subject = $this->subject; |
141
|
|
|
$this->mail->Body = $body; |
142
|
|
|
$this->mail->AltBody = Html2Text::convert($body); |
143
|
|
|
$this->attach(); |
144
|
|
|
if (!$this->mail->send()) { |
145
|
|
|
$msg = 'A mensagem não pode ser enviada. Mail Error: ' . $this->mail->ErrorInfo; |
146
|
|
|
throw new \RuntimeException($msg); |
147
|
|
|
} |
148
|
|
|
$this->mail->ClearAllRecipients(); |
149
|
|
|
$this->mail->ClearAttachments(); |
150
|
|
|
return true; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Configure and send documents |
155
|
|
|
* @param \stdClass $config |
156
|
|
|
* @param string $xml |
157
|
|
|
* @param string $pdf |
158
|
|
|
* @param array $addresses |
159
|
|
|
* @param string $htmltemplate |
160
|
|
|
* @param PHPMailer $mailer |
161
|
|
|
* @return Mail |
162
|
|
|
*/ |
163
|
|
|
public static function sendMail( |
164
|
|
|
\stdClass $config, |
165
|
|
|
$xml, |
166
|
|
|
$pdf = '', |
167
|
|
|
array $addresses = [], |
168
|
|
|
$htmltemplate = '', |
169
|
|
|
PHPMailer $mailer = null |
170
|
|
|
) { |
171
|
|
|
$mail = new static($config, $mailer); |
172
|
|
|
$mail->loadDocuments($xml, $pdf); |
173
|
|
|
$mail->loadTemplate($htmltemplate); |
174
|
|
|
$mail->send($addresses, false); |
175
|
|
|
return $mail; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|