Guest User

Untitled

a guest
Mar 13th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 111.54 KB | None | 0 0
  1. <?php
  2.  
  3. class PHPMailer
  4. {
  5. const CHARSET_ISO88591 = 'iso-8859-1';
  6. const CHARSET_UTF8 = 'utf-8';
  7. const CONTENT_TYPE_PLAINTEXT = 'text/plain';
  8. const CONTENT_TYPE_TEXT_CALENDAR = 'text/calendar';
  9. const CONTENT_TYPE_TEXT_HTML = 'text/html';
  10. const CONTENT_TYPE_MULTIPART_ALTERNATIVE = 'multipart/alternative';
  11. const CONTENT_TYPE_MULTIPART_MIXED = 'multipart/mixed';
  12. const CONTENT_TYPE_MULTIPART_RELATED = 'multipart/related';
  13. const ENCODING_7BIT = '7bit';
  14. const ENCODING_8BIT = '8bit';
  15. const ENCODING_BASE64 = 'base64';
  16. const ENCODING_BINARY = 'binary';
  17. const ENCODING_QUOTED_PRINTABLE = 'quoted-printable';
  18.  
  19. public $Priority;
  20.  
  21. public $CharSet = self::CHARSET_ISO88591;
  22.  
  23. public $ContentType = self::CONTENT_TYPE_PLAINTEXT;
  24.  
  25. public $Encoding = self::ENCODING_8BIT;
  26.  
  27. public $ErrorInfo = '';
  28.  
  29. public $From = 'root@localhost';
  30.  
  31. public $FromName = 'Root User';
  32.  
  33. public $Sender = '';
  34.  
  35. public $Subject = '';
  36.  
  37. public $Body = '';
  38.  
  39. public $AltBody = '';
  40.  
  41. public $Ical = '';
  42.  
  43. protected $MIMEBody = '';
  44.  
  45. protected $MIMEHeader = '';
  46.  
  47. protected $mailHeader = '';
  48.  
  49. public $WordWrap = 0;
  50.  
  51. public $Mailer = 'mail';
  52.  
  53. public $Sendmail = '/usr/sbin/sendmail';
  54.  
  55. public $UseSendmailOptions = true;
  56.  
  57. public $ConfirmReadingTo = '';
  58.  
  59. public $Hostname = '';
  60.  
  61. public $MessageID = '';
  62.  
  63. public $MessageDate = '';
  64.  
  65. public $Host = 'localhost';
  66.  
  67. public $Port = 25;
  68.  
  69. public $Helo = '';
  70.  
  71. public $SMTPSecure = '';
  72.  
  73. public $SMTPAutoTLS = true;
  74.  
  75. public $SMTPAuth = false;
  76.  
  77. public $SMTPOptions = [];
  78.  
  79. public $Username = '';
  80.  
  81. public $Password = '';
  82.  
  83. public $AuthType = '';
  84.  
  85. protected $oauth;
  86.  
  87. public $Timeout = 300;
  88.  
  89. public $dsn = '';
  90.  
  91. public $SMTPDebug = 0;
  92.  
  93. public $Debugoutput = 'echo';
  94.  
  95. public $SMTPKeepAlive = false;
  96.  
  97. public $SingleTo = false;
  98.  
  99. protected $SingleToArray = [];
  100.  
  101. public $do_verp = false;
  102.  
  103. public $AllowEmpty = false;
  104.  
  105. public $DKIM_selector = '';
  106.  
  107. public $DKIM_identity = '';
  108.  
  109. public $DKIM_passphrase = '';
  110.  
  111. public $DKIM_domain = '';
  112.  
  113. public $DKIM_copyHeaderFields = true;
  114.  
  115. public $DKIM_extraHeaders = [];
  116.  
  117. public $DKIM_private = '';
  118.  
  119. public $DKIM_private_string = '';
  120.  
  121. public $action_function = '';
  122.  
  123. public $XMailer = '';
  124.  
  125. public static $validator = 'php';
  126.  
  127. protected $smtp;
  128.  
  129. protected $to = [];
  130.  
  131. protected $cc = [];
  132.  
  133. protected $bcc = [];
  134.  
  135. protected $ReplyTo = [];
  136.  
  137. protected $all_recipients = [];
  138.  
  139. protected $RecipientsQueue = [];
  140.  
  141. protected $ReplyToQueue = [];
  142.  
  143. protected $attachment = [];
  144.  
  145. protected $CustomHeader = [];
  146.  
  147. protected $lastMessageID = '';
  148.  
  149. protected $message_type = '';
  150.  
  151. protected $boundary = [];
  152.  
  153. protected $language = [];
  154.  
  155. protected $error_count = 0;
  156.  
  157. protected $sign_cert_file = '';
  158.  
  159. protected $sign_key_file = '';
  160.  
  161. protected $sign_extracerts_file = '';
  162.  
  163. protected $sign_key_pass = '';
  164.  
  165. protected $exceptions = false;
  166.  
  167. protected $uniqueid = '';
  168.  
  169. const VERSION = '6.0.7';
  170.  
  171. const STOP_MESSAGE = 0;
  172.  
  173. const STOP_CONTINUE = 1;
  174.  
  175. const STOP_CRITICAL = 2;
  176.  
  177. protected static $LE = "\r\n";
  178.  
  179. const MAX_LINE_LENGTH = 998;
  180.  
  181. const STD_LINE_LENGTH = 76;
  182.  
  183. public function __construct($exceptions = null)
  184. {
  185. if (null !== $exceptions) {
  186. $this->exceptions = (bool)$exceptions;
  187. }
  188. $this->Debugoutput = (strpos(PHP_SAPI, 'cli') !== false ? 'echo' : 'html');
  189. }
  190.  
  191. public function __destruct()
  192. {
  193. $this->smtpClose();
  194. }
  195.  
  196. private function mailPassthru($to, $subject, $body, $header, $params)
  197. {
  198. if (ini_get('mbstring.func_overload') & 1) {
  199. $subject = $this->secureHeader($subject);
  200. } else {
  201. $subject = $this->encodeHeader($this->secureHeader($subject));
  202. }
  203. if (!$this->UseSendmailOptions or null === $params) {
  204. $result = @mail($to, $subject, $body, $header);
  205. } else {
  206. $result = @mail($to, $subject, $body, $header, $params);
  207. }
  208. return $result;
  209. }
  210.  
  211. protected function edebug($str)
  212. {
  213. if ($this->SMTPDebug <= 0) {
  214. return;
  215. }
  216. if ($this->Debugoutput instanceof \Psr\Log\LoggerInterface) {
  217. $this->Debugoutput->debug($str);
  218. return;
  219. }
  220. if (!in_array($this->Debugoutput, ['error_log', 'html', 'echo']) and is_callable($this->Debugoutput)) {
  221. call_user_func($this->Debugoutput, $str, $this->SMTPDebug);
  222. return;
  223. }
  224. switch ($this->Debugoutput) {
  225. case 'error_log':
  226. error_log($str);
  227. break;
  228. case 'html':
  229. echo htmlentities(
  230. preg_replace('/[\r\n]+/', '', $str),
  231. ENT_QUOTES,
  232. 'UTF-8'
  233. ), "<br>\n";
  234. break;
  235. case 'echo':
  236. default:
  237. $str = preg_replace('/\r\n|\r/ms', "\n", $str);
  238. echo gmdate('Y-m-d H:i:s'),
  239. "\t",
  240. trim(
  241. str_replace(
  242. "\n",
  243. "\n \t ",
  244. trim($str)
  245. )
  246. ),
  247. "\n";
  248. }
  249. }
  250.  
  251. public function isHTML($isHtml = true)
  252. {
  253. if ($isHtml) {
  254. $this->ContentType = static::CONTENT_TYPE_TEXT_HTML;
  255. } else {
  256. $this->ContentType = static::CONTENT_TYPE_PLAINTEXT;
  257. }
  258. }
  259.  
  260. public function isSMTP()
  261. {
  262. $this->Mailer = 'smtp';
  263. }
  264.  
  265. public function isMail()
  266. {
  267. $this->Mailer = 'mail';
  268. }
  269.  
  270. public function isSendmail()
  271. {
  272. $ini_sendmail_path = ini_get('sendmail_path');
  273. if (false === stripos($ini_sendmail_path, 'sendmail')) {
  274. $this->Sendmail = '/usr/sbin/sendmail';
  275. } else {
  276. $this->Sendmail = $ini_sendmail_path;
  277. }
  278. $this->Mailer = 'sendmail';
  279. }
  280.  
  281. public function isQmail()
  282. {
  283. $ini_sendmail_path = ini_get('sendmail_path');
  284. if (false === stripos($ini_sendmail_path, 'qmail')) {
  285. $this->Sendmail = '/var/qmail/bin/qmail-inject';
  286. } else {
  287. $this->Sendmail = $ini_sendmail_path;
  288. }
  289. $this->Mailer = 'qmail';
  290. }
  291.  
  292. public function addAddress($address, $name = '')
  293. {
  294. return $this->addOrEnqueueAnAddress('to', $address, $name);
  295. }
  296.  
  297. public function addCC($address, $name = '')
  298. {
  299. return $this->addOrEnqueueAnAddress('cc', $address, $name);
  300. }
  301.  
  302. public function addBCC($address, $name = '')
  303. {
  304. return $this->addOrEnqueueAnAddress('bcc', $address, $name);
  305. }
  306.  
  307. public function addReplyTo($address, $name = '')
  308. {
  309. return $this->addOrEnqueueAnAddress('Reply-To', $address, $name);
  310. }
  311.  
  312. protected function addOrEnqueueAnAddress($kind, $address, $name)
  313. {
  314. $address = trim($address);
  315. $name = trim(preg_replace('/[\r\n]+/', '', $name));
  316. $pos = strrpos($address, '@');
  317. if (false === $pos) {
  318. $error_message = sprintf('%s (%s): %s',
  319. $this->lang('invalid_address'),
  320. $kind,
  321. $address);
  322. $this->setError($error_message);
  323. $this->edebug($error_message);
  324. if ($this->exceptions) {
  325. throw new Exception($error_message);
  326. }
  327. return false;
  328. }
  329. $params = [$kind, $address, $name];
  330. if ($this->has8bitChars(substr($address, ++$pos)) and static::idnSupported()) {
  331. if ('Reply-To' != $kind) {
  332. if (!array_key_exists($address, $this->RecipientsQueue)) {
  333. $this->RecipientsQueue[$address] = $params;
  334. return true;
  335. }
  336. } else {
  337. if (!array_key_exists($address, $this->ReplyToQueue)) {
  338. $this->ReplyToQueue[$address] = $params;
  339. return true;
  340. }
  341. }
  342. return false;
  343. }
  344. return call_user_func_array([$this, 'addAnAddress'], $params);
  345. }
  346.  
  347. protected function addAnAddress($kind, $address, $name = '')
  348. {
  349. if (!in_array($kind, ['to', 'cc', 'bcc', 'Reply-To'])) {
  350. $error_message = sprintf('%s: %s',
  351. $this->lang('Invalid recipient kind'),
  352. $kind);
  353. $this->setError($error_message);
  354. $this->edebug($error_message);
  355. if ($this->exceptions) {
  356. throw new Exception($error_message);
  357. }
  358. return false;
  359. }
  360. if (!static::validateAddress($address)) {
  361. $error_message = sprintf('%s (%s): %s',
  362. $this->lang('invalid_address'),
  363. $kind,
  364. $address);
  365. $this->setError($error_message);
  366. $this->edebug($error_message);
  367. if ($this->exceptions) {
  368. throw new Exception($error_message);
  369. }
  370. return false;
  371. }
  372. if ('Reply-To' != $kind) {
  373. if (!array_key_exists(strtolower($address), $this->all_recipients)) {
  374. $this->{$kind}[] = [$address, $name];
  375. $this->all_recipients[strtolower($address)] = true;
  376. return true;
  377. }
  378. } else {
  379. if (!array_key_exists(strtolower($address), $this->ReplyTo)) {
  380. $this->ReplyTo[strtolower($address)] = [$address, $name];
  381. return true;
  382. }
  383. }
  384. return false;
  385. }
  386.  
  387. public static function parseAddresses($addrstr, $useimap = true)
  388. {
  389. $addresses = [];
  390. if ($useimap and function_exists('imap_rfc822_parse_adrlist')) {
  391. $list = imap_rfc822_parse_adrlist($addrstr, '');
  392. foreach ($list as $address) {
  393. if ('.SYNTAX-ERROR.' != $address->host) {
  394. if (static::validateAddress($address->mailbox . '@' . $address->host)) {
  395. $addresses[] = [
  396. 'name' => (property_exists($address, 'personal') ? $address->personal : ''),
  397. 'address' => $address->mailbox . '@' . $address->host,
  398. ];
  399. }
  400. }
  401. }
  402. } else {
  403. $list = explode(',', $addrstr);
  404. foreach ($list as $address) {
  405. $address = trim($address);
  406. if (strpos($address, '<') === false) {
  407. if (static::validateAddress($address)) {
  408. $addresses[] = [
  409. 'name' => '',
  410. 'address' => $address,
  411. ];
  412. }
  413. } else {
  414. list($name, $email) = explode('<', $address);
  415. $email = trim(str_replace('>', '', $email));
  416. if (static::validateAddress($email)) {
  417. $addresses[] = [
  418. 'name' => trim(str_replace(['"', "'"], '', $name)),
  419. 'address' => $email,
  420. ];
  421. }
  422. }
  423. }
  424. }
  425. return $addresses;
  426. }
  427.  
  428. public function setFrom($address, $name = '', $auto = true)
  429. {
  430. $address = trim($address);
  431. $name = trim(preg_replace('/[\r\n]+/', '', $name));
  432. $pos = strrpos($address, '@');
  433. if (false === $pos or
  434. (!$this->has8bitChars(substr($address, ++$pos)) or !static::idnSupported()) and
  435. !static::validateAddress($address)) {
  436. $error_message = sprintf('%s (From): %s',
  437. $this->lang('invalid_address'),
  438. $address);
  439. $this->setError($error_message);
  440. $this->edebug($error_message);
  441. if ($this->exceptions) {
  442. throw new Exception($error_message);
  443. }
  444. return false;
  445. }
  446. $this->From = $address;
  447. $this->FromName = $name;
  448. if ($auto) {
  449. if (empty($this->Sender)) {
  450. $this->Sender = $address;
  451. }
  452. }
  453. return true;
  454. }
  455.  
  456. public function getLastMessageID()
  457. {
  458. return $this->lastMessageID;
  459. }
  460.  
  461. public static function validateAddress($address, $patternselect = null)
  462. {
  463. if (null === $patternselect) {
  464. $patternselect = static::$validator;
  465. }
  466. if (is_callable($patternselect)) {
  467. return call_user_func($patternselect, $address);
  468. }
  469. if (strpos($address, "\n") !== false or strpos($address, "\r") !== false) {
  470. return false;
  471. }
  472. switch ($patternselect) {
  473. case 'pcre':
  474. case 'pcre8':
  475.  
  476. return (bool)preg_match(
  477. '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)' .
  478. '((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' .
  479. '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' .
  480. '([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' .
  481. '(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' .
  482. '(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}' .
  483. '|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:' .
  484. '|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
  485. '|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD',
  486. $address
  487. );
  488. case 'html5':
  489.  
  490. return (bool)preg_match(
  491. '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}' .
  492. '[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/sD',
  493. $address
  494. );
  495. case 'php':
  496. default:
  497. return (bool)filter_var($address, FILTER_VALIDATE_EMAIL);
  498. }
  499. }
  500.  
  501. public static function idnSupported()
  502. {
  503. return function_exists('idn_to_ascii') and function_exists('mb_convert_encoding');
  504. }
  505.  
  506. public function punyencodeAddress($address)
  507. {
  508. $pos = strrpos($address, '@');
  509. if (static::idnSupported() and
  510. !empty($this->CharSet) and
  511. false !== $pos
  512. ) {
  513. $domain = substr($address, ++$pos);
  514. if ($this->has8bitChars($domain) and @mb_check_encoding($domain, $this->CharSet)) {
  515. $domain = mb_convert_encoding($domain, 'UTF-8', $this->CharSet);
  516. $errorcode = 0;
  517. $punycode = idn_to_ascii($domain, $errorcode, INTL_IDNA_VARIANT_UTS46);
  518. if (false !== $punycode) {
  519. return substr($address, 0, $pos) . $punycode;
  520. }
  521. }
  522. }
  523. return $address;
  524. }
  525.  
  526. public function send()
  527. {
  528. try {
  529. if (!$this->preSend()) {
  530. return false;
  531. }
  532. return $this->postSend();
  533. } catch (Exception $exc) {
  534. $this->mailHeader = '';
  535. $this->setError($exc->getMessage());
  536. if ($this->exceptions) {
  537. throw $exc;
  538. }
  539. return false;
  540. }
  541. }
  542.  
  543. public function preSend()
  544. {
  545. if ('smtp' == $this->Mailer or
  546. ('mail' == $this->Mailer and stripos(PHP_OS, 'WIN') === 0)
  547. ) {
  548. static::setLE("\r\n");
  549. } else {
  550. static::setLE(PHP_EOL);
  551. }
  552. if (ini_get('mail.add_x_header') == 1
  553. and 'mail' == $this->Mailer
  554. and stripos(PHP_OS, 'WIN') === 0
  555. and ((version_compare(PHP_VERSION, '7.0.0', '>=')
  556. and version_compare(PHP_VERSION, '7.0.17', '<'))
  557. or (version_compare(PHP_VERSION, '7.1.0', '>=')
  558. and version_compare(PHP_VERSION, '7.1.3', '<')))
  559. ) {
  560. trigger_error(
  561. 'Your version of PHP is affected by a bug that may result in corrupted messages.' .
  562. ' To fix it, switch to sending using SMTP, disable the mail.add_x_header option in' .
  563. ' your php.ini, switch to MacOS or Linux, or upgrade your PHP to version 7.0.17+ or 7.1.3+.',
  564. E_USER_WARNING
  565. );
  566. }
  567. try {
  568. $this->error_count = 0;
  569. $this->mailHeader = '';
  570. foreach (array_merge($this->RecipientsQueue, $this->ReplyToQueue) as $params) {
  571. $params[1] = $this->punyencodeAddress($params[1]);
  572. call_user_func_array([$this, 'addAnAddress'], $params);
  573. }
  574. if (count($this->to) + count($this->cc) + count($this->bcc) < 1) {
  575. throw new Exception($this->lang('provide_address'), self::STOP_CRITICAL);
  576. }
  577. foreach (['From', 'Sender', 'ConfirmReadingTo'] as $address_kind) {
  578. $this->$address_kind = trim($this->$address_kind);
  579. if (empty($this->$address_kind)) {
  580. continue;
  581. }
  582. $this->$address_kind = $this->punyencodeAddress($this->$address_kind);
  583. if (!static::validateAddress($this->$address_kind)) {
  584. $error_message = sprintf('%s (%s): %s',
  585. $this->lang('invalid_address'),
  586. $address_kind,
  587. $this->$address_kind);
  588. $this->setError($error_message);
  589. $this->edebug($error_message);
  590. if ($this->exceptions) {
  591. throw new Exception($error_message);
  592. }
  593. return false;
  594. }
  595. }
  596. if ($this->alternativeExists()) {
  597. $this->ContentType = static::CONTENT_TYPE_MULTIPART_ALTERNATIVE;
  598. }
  599. $this->setMessageType();
  600. if (!$this->AllowEmpty and empty($this->Body)) {
  601. throw new Exception($this->lang('empty_message'), self::STOP_CRITICAL);
  602. }
  603. $this->Subject = trim($this->Subject);
  604. $this->MIMEHeader = '';
  605. $this->MIMEBody = $this->createBody();
  606. $tempheaders = $this->MIMEHeader;
  607. $this->MIMEHeader = $this->createHeader();
  608. $this->MIMEHeader .= $tempheaders;
  609. if ('mail' == $this->Mailer) {
  610. if (count($this->to) > 0) {
  611. $this->mailHeader .= $this->addrAppend('To', $this->to);
  612. } else {
  613. $this->mailHeader .= $this->headerLine('To', 'undisclosed-recipients:;');
  614. }
  615. $this->mailHeader .= $this->headerLine(
  616. 'Subject',
  617. $this->encodeHeader($this->secureHeader($this->Subject))
  618. );
  619. }
  620. if (!empty($this->DKIM_domain)
  621. and !empty($this->DKIM_selector)
  622. and (!empty($this->DKIM_private_string)
  623. or (!empty($this->DKIM_private)
  624. and static::isPermittedPath($this->DKIM_private)
  625. and file_exists($this->DKIM_private)
  626. )
  627. )
  628. ) {
  629. $header_dkim = $this->DKIM_Add(
  630. $this->MIMEHeader . $this->mailHeader,
  631. $this->encodeHeader($this->secureHeader($this->Subject)),
  632. $this->MIMEBody
  633. );
  634. $this->MIMEHeader = rtrim($this->MIMEHeader, "\r\n ") . static::$LE .
  635. static::normalizeBreaks($header_dkim) . static::$LE;
  636. }
  637. return true;
  638. } catch (Exception $exc) {
  639. $this->setError($exc->getMessage());
  640. if ($this->exceptions) {
  641. throw $exc;
  642. }
  643. return false;
  644. }
  645. }
  646.  
  647. public function postSend()
  648. {
  649. try {
  650. switch ($this->Mailer) {
  651. case 'sendmail':
  652. case 'qmail':
  653. return $this->sendmailSend($this->MIMEHeader, $this->MIMEBody);
  654. case 'smtp':
  655. return $this->smtpSend($this->MIMEHeader, $this->MIMEBody);
  656. case 'mail':
  657. return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
  658. default:
  659. $sendMethod = $this->Mailer . 'Send';
  660. if (method_exists($this, $sendMethod)) {
  661. return $this->$sendMethod($this->MIMEHeader, $this->MIMEBody);
  662. }
  663. return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
  664. }
  665. } catch (Exception $exc) {
  666. $this->setError($exc->getMessage());
  667. $this->edebug($exc->getMessage());
  668. if ($this->exceptions) {
  669. throw $exc;
  670. }
  671. }
  672. return false;
  673. }
  674.  
  675. protected function sendmailSend($header, $body)
  676. {
  677. if (!empty($this->Sender) and self::isShellSafe($this->Sender)) {
  678. if ('qmail' == $this->Mailer) {
  679. $sendmailFmt = '%s -f%s';
  680. } else {
  681. $sendmailFmt = '%s -oi -f%s -t';
  682. }
  683. } else {
  684. if ('qmail' == $this->Mailer) {
  685. $sendmailFmt = '%s';
  686. } else {
  687. $sendmailFmt = '%s -oi -t';
  688. }
  689. }
  690. $sendmail = sprintf($sendmailFmt, escapeshellcmd($this->Sendmail), $this->Sender);
  691. if ($this->SingleTo) {
  692. foreach ($this->SingleToArray as $toAddr) {
  693. $mail = @popen($sendmail, 'w');
  694. if (!$mail) {
  695. throw new Exception($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  696. }
  697. fwrite($mail, 'To: ' . $toAddr . "\n");
  698. fwrite($mail, $header);
  699. fwrite($mail, $body);
  700. $result = pclose($mail);
  701. $this->doCallback(
  702. ($result == 0),
  703. [$toAddr],
  704. $this->cc,
  705. $this->bcc,
  706. $this->Subject,
  707. $body,
  708. $this->From,
  709. []
  710. );
  711. if (0 !== $result) {
  712. throw new Exception($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  713. }
  714. }
  715. } else {
  716. $mail = @popen($sendmail, 'w');
  717. if (!$mail) {
  718. throw new Exception($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  719. }
  720. fwrite($mail, $header);
  721. fwrite($mail, $body);
  722. $result = pclose($mail);
  723. $this->doCallback(
  724. ($result == 0),
  725. $this->to,
  726. $this->cc,
  727. $this->bcc,
  728. $this->Subject,
  729. $body,
  730. $this->From,
  731. []
  732. );
  733. if (0 !== $result) {
  734. throw new Exception($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  735. }
  736. }
  737. return true;
  738. }
  739.  
  740. protected static function isShellSafe($string)
  741. {
  742. if (escapeshellcmd($string) !== $string
  743. or !in_array(escapeshellarg($string), ["'$string'", "\"$string\""])
  744. ) {
  745. return false;
  746. }
  747. $length = strlen($string);
  748. for ($i = 0; $i < $length; ++$i) {
  749. $c = $string[$i];
  750. if (!ctype_alnum($c) && strpos('@_-.', $c) === false) {
  751. return false;
  752. }
  753. }
  754. return true;
  755. }
  756.  
  757. protected static function isPermittedPath($path)
  758. {
  759. return !preg_match('#^[a-z]+://#i', $path);
  760. }
  761.  
  762. protected function mailSend($header, $body)
  763. {
  764. $toArr = [];
  765. foreach ($this->to as $toaddr) {
  766. $toArr[] = $this->addrFormat($toaddr);
  767. }
  768. $to = implode(', ', $toArr);
  769. $params = null;
  770. if (!empty($this->Sender) and static::validateAddress($this->Sender)) {
  771. if (self::isShellSafe($this->Sender)) {
  772. $params = sprintf('-f%s', $this->Sender);
  773. }
  774. }
  775. if (!empty($this->Sender) and static::validateAddress($this->Sender)) {
  776. $old_from = ini_get('sendmail_from');
  777. ini_set('sendmail_from', $this->Sender);
  778. }
  779. $result = false;
  780. if ($this->SingleTo and count($toArr) > 1) {
  781. foreach ($toArr as $toAddr) {
  782. $result = $this->mailPassthru($toAddr, $this->Subject, $body, $header, $params);
  783. $this->doCallback($result, [$toAddr], $this->cc, $this->bcc, $this->Subject, $body, $this->From, []);
  784. }
  785. } else {
  786. $result = $this->mailPassthru($to, $this->Subject, $body, $header, $params);
  787. $this->doCallback($result, $this->to, $this->cc, $this->bcc, $this->Subject, $body, $this->From, []);
  788. }
  789. if (isset($old_from)) {
  790. ini_set('sendmail_from', $old_from);
  791. }
  792. if (!$result) {
  793. throw new Exception($this->lang('instantiate'), self::STOP_CRITICAL);
  794. }
  795. return true;
  796. }
  797.  
  798. public function getSMTPInstance()
  799. {
  800. if (!is_object($this->smtp)) {
  801. $this->smtp = new SMTP();
  802. }
  803. return $this->smtp;
  804. }
  805.  
  806. public function setSMTPInstance(SMTP $smtp)
  807. {
  808. $this->smtp = $smtp;
  809. return $this->smtp;
  810. }
  811.  
  812. protected function smtpSend($header, $body)
  813. {
  814. $bad_rcpt = [];
  815. if (!$this->smtpConnect($this->SMTPOptions)) {
  816. throw new Exception($this->lang('smtp_connect_failed'), self::STOP_CRITICAL);
  817. }
  818. if ('' == $this->Sender) {
  819. $smtp_from = $this->From;
  820. } else {
  821. $smtp_from = $this->Sender;
  822. }
  823. if (!$this->smtp->mail($smtp_from)) {
  824. $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(',', $this->smtp->getError()));
  825. throw new Exception($this->ErrorInfo, self::STOP_CRITICAL);
  826. }
  827. $callbacks = [];
  828. foreach ([$this->to, $this->cc, $this->bcc] as $togroup) {
  829. foreach ($togroup as $to) {
  830. if (!$this->smtp->recipient($to[0], $this->dsn)) {
  831. $error = $this->smtp->getError();
  832. $bad_rcpt[] = ['to' => $to[0], 'error' => $error['detail']];
  833. $isSent = false;
  834. } else {
  835. $isSent = true;
  836. }
  837. $callbacks[] = ['issent' => $isSent, 'to' => $to[0]];
  838. }
  839. }
  840. if ((count($this->all_recipients) > count($bad_rcpt)) and !$this->smtp->data($header . $body)) {
  841. throw new Exception($this->lang('data_not_accepted'), self::STOP_CRITICAL);
  842. }
  843. $smtp_transaction_id = $this->smtp->getLastTransactionID();
  844. if ($this->SMTPKeepAlive) {
  845. $this->smtp->reset();
  846. } else {
  847. $this->smtp->quit();
  848. $this->smtp->close();
  849. }
  850. foreach ($callbacks as $cb) {
  851. $this->doCallback(
  852. $cb['issent'],
  853. [$cb['to']],
  854. [],
  855. [],
  856. $this->Subject,
  857. $body,
  858. $this->From,
  859. ['smtp_transaction_id' => $smtp_transaction_id]
  860. );
  861. }
  862. if (count($bad_rcpt) > 0) {
  863. $errstr = '';
  864. foreach ($bad_rcpt as $bad) {
  865. $errstr .= $bad['to'] . ': ' . $bad['error'];
  866. }
  867. throw new Exception(
  868. $this->lang('recipients_failed') . $errstr,
  869. self::STOP_CONTINUE
  870. );
  871. }
  872. return true;
  873. }
  874.  
  875. public function smtpConnect($options = null)
  876. {
  877. if (null === $this->smtp) {
  878. $this->smtp = $this->getSMTPInstance();
  879. }
  880. if (null === $options) {
  881. $options = $this->SMTPOptions;
  882. }
  883. if ($this->smtp->connected()) {
  884. return true;
  885. }
  886. $this->smtp->setTimeout($this->Timeout);
  887. $this->smtp->setDebugLevel($this->SMTPDebug);
  888. $this->smtp->setDebugOutput($this->Debugoutput);
  889. $this->smtp->setVerp($this->do_verp);
  890. $hosts = explode(';', $this->Host);
  891. $lastexception = null;
  892. foreach ($hosts as $hostentry) {
  893. $hostinfo = [];
  894. if (!preg_match(
  895. '/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*|\[[a-fA-F0-9:]+\]):?([0-9]*)$/',
  896. trim($hostentry),
  897. $hostinfo
  898. )) {
  899. static::edebug($this->lang('connect_host') . ' ' . $hostentry);
  900. continue;
  901. }
  902.  
  903. if (!static::isValidHost($hostinfo[3])) {
  904. static::edebug($this->lang('connect_host') . ' ' . $hostentry);
  905. continue;
  906. }
  907. $prefix = '';
  908. $secure = $this->SMTPSecure;
  909. $tls = ('tls' == $this->SMTPSecure);
  910. if ('ssl' == $hostinfo[2] or ('' == $hostinfo[2] and 'ssl' == $this->SMTPSecure)) {
  911. $prefix = 'ssl://';
  912. $tls = false;
  913. $secure = 'ssl';
  914. } elseif ('tls' == $hostinfo[2]) {
  915. $tls = true;
  916. $secure = 'tls';
  917. }
  918. $sslext = defined('OPENSSL_ALGO_SHA256');
  919. if ('tls' === $secure or 'ssl' === $secure) {
  920. if (!$sslext) {
  921. throw new Exception($this->lang('extension_missing') . 'openssl', self::STOP_CRITICAL);
  922. }
  923. }
  924. $host = $hostinfo[3];
  925. $port = $this->Port;
  926. $tport = (int)$hostinfo[4];
  927. if ($tport > 0 and $tport < 65536) {
  928. $port = $tport;
  929. }
  930. if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) {
  931. try {
  932. if ($this->Helo) {
  933. $hello = $this->Helo;
  934. } else {
  935. $hello = $this->serverHostname();
  936. }
  937. $this->smtp->hello($hello);
  938. if ($this->SMTPAutoTLS and $sslext and 'ssl' != $secure and $this->smtp->getServerExt('STARTTLS')) {
  939. $tls = true;
  940. }
  941. if ($tls) {
  942. if (!$this->smtp->startTLS()) {
  943. throw new Exception($this->lang('connect_host'));
  944. }
  945. $this->smtp->hello($hello);
  946. }
  947. if ($this->SMTPAuth) {
  948. if (!$this->smtp->authenticate(
  949. $this->Username,
  950. $this->Password,
  951. $this->AuthType,
  952. $this->oauth
  953. )
  954. ) {
  955. throw new Exception($this->lang('authenticate'));
  956. }
  957. }
  958. return true;
  959. } catch (Exception $exc) {
  960. $lastexception = $exc;
  961. $this->edebug($exc->getMessage());
  962. $this->smtp->quit();
  963. }
  964. }
  965. }
  966. $this->smtp->close();
  967. if ($this->exceptions and null !== $lastexception) {
  968. throw $lastexception;
  969. }
  970. return false;
  971. }
  972.  
  973. public function smtpClose()
  974. {
  975. if (null !== $this->smtp) {
  976. if ($this->smtp->connected()) {
  977. $this->smtp->quit();
  978. $this->smtp->close();
  979. }
  980. }
  981. }
  982.  
  983. public function setLanguage($langcode = 'en', $lang_path = '')
  984. {
  985. $renamed_langcodes = [
  986. 'br' => 'pt_br',
  987. 'cz' => 'cs',
  988. 'dk' => 'da',
  989. 'no' => 'nb',
  990. 'se' => 'sv',
  991. 'rs' => 'sr',
  992. 'tg' => 'tl',
  993. ];
  994. if (isset($renamed_langcodes[$langcode])) {
  995. $langcode = $renamed_langcodes[$langcode];
  996. }
  997. $PHPMAILER_LANG = [
  998. 'authenticate' => 'SMTP Error: Could not authenticate.',
  999. 'connect_host' => 'SMTP Error: Could not connect to SMTP host.',
  1000. 'data_not_accepted' => 'SMTP Error: data not accepted.',
  1001. 'empty_message' => 'Message body empty',
  1002. 'encoding' => 'Unknown encoding: ',
  1003. 'execute' => 'Could not execute: ',
  1004. 'file_access' => 'Could not access file: ',
  1005. 'file_open' => 'File Error: Could not open file: ',
  1006. 'from_failed' => 'The following From address failed: ',
  1007. 'instantiate' => 'Could not instantiate mail function.',
  1008. 'invalid_address' => 'Invalid address: ',
  1009. 'mailer_not_supported' => ' mailer is not supported.',
  1010. 'provide_address' => 'You must provide at least one recipient email address.',
  1011. 'recipients_failed' => 'SMTP Error: The following recipients failed: ',
  1012. 'signing' => 'Signing Error: ',
  1013. 'smtp_connect_failed' => 'SMTP connect() failed.',
  1014. 'smtp_error' => 'SMTP server error: ',
  1015. 'variable_set' => 'Cannot set or reset variable: ',
  1016. 'extension_missing' => 'Extension missing: ',
  1017. ];
  1018. if (empty($lang_path)) {
  1019. $lang_path = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'language' . DIRECTORY_SEPARATOR;
  1020. }
  1021. if (!preg_match('/^[a-z]{2}(?:_[a-zA-Z]{2})?$/', $langcode)) {
  1022. $langcode = 'en';
  1023. }
  1024. $foundlang = true;
  1025. $lang_file = $lang_path . 'phpmailer.lang-' . $langcode . '.php';
  1026. if ('en' != $langcode) {
  1027. if (!static::isPermittedPath($lang_file) || !file_exists($lang_file)) {
  1028. $foundlang = false;
  1029. } else {
  1030. $foundlang = include $lang_file;
  1031. }
  1032. }
  1033. $this->language = $PHPMAILER_LANG;
  1034. return (bool)$foundlang;
  1035. }
  1036.  
  1037. public function getTranslations()
  1038. {
  1039. return $this->language;
  1040. }
  1041.  
  1042. public function addrAppend($type, $addr)
  1043. {
  1044. $addresses = [];
  1045. foreach ($addr as $address) {
  1046. $addresses[] = $this->addrFormat($address);
  1047. }
  1048. return $type . ': ' . implode(', ', $addresses) . static::$LE;
  1049. }
  1050.  
  1051. public function addrFormat($addr)
  1052. {
  1053. if (empty($addr[1])) {
  1054. return $this->secureHeader($addr[0]);
  1055. }
  1056. return $this->encodeHeader($this->secureHeader($addr[1]), 'phrase') . ' <' . $this->secureHeader(
  1057. $addr[0]
  1058. ) . '>';
  1059. }
  1060.  
  1061. public function wrapText($message, $length, $qp_mode = false)
  1062. {
  1063. if ($qp_mode) {
  1064. $soft_break = sprintf(' =%s', static::$LE);
  1065. } else {
  1066. $soft_break = static::$LE;
  1067. }
  1068. $is_utf8 = static::CHARSET_UTF8 === strtolower($this->CharSet);
  1069. $lelen = strlen(static::$LE);
  1070. $crlflen = strlen(static::$LE);
  1071. $message = static::normalizeBreaks($message);
  1072. if (substr($message, -$lelen) == static::$LE) {
  1073. $message = substr($message, 0, -$lelen);
  1074. }
  1075. $lines = explode(static::$LE, $message);
  1076. $message = '';
  1077. foreach ($lines as $line) {
  1078. $words = explode(' ', $line);
  1079. $buf = '';
  1080. $firstword = true;
  1081. foreach ($words as $word) {
  1082. if ($qp_mode and (strlen($word) > $length)) {
  1083. $space_left = $length - strlen($buf) - $crlflen;
  1084. if (!$firstword) {
  1085. if ($space_left > 20) {
  1086. $len = $space_left;
  1087. if ($is_utf8) {
  1088. $len = $this->utf8CharBoundary($word, $len);
  1089. } elseif ('=' == substr($word, $len - 1, 1)) {
  1090. --$len;
  1091. } elseif ('=' == substr($word, $len - 2, 1)) {
  1092. $len -= 2;
  1093. }
  1094. $part = substr($word, 0, $len);
  1095. $word = substr($word, $len);
  1096. $buf .= ' ' . $part;
  1097. $message .= $buf . sprintf('=%s', static::$LE);
  1098. } else {
  1099. $message .= $buf . $soft_break;
  1100. }
  1101. $buf = '';
  1102. }
  1103. while (strlen($word) > 0) {
  1104. if ($length <= 0) {
  1105. break;
  1106. }
  1107. $len = $length;
  1108. if ($is_utf8) {
  1109. $len = $this->utf8CharBoundary($word, $len);
  1110. } elseif ('=' == substr($word, $len - 1, 1)) {
  1111. --$len;
  1112. } elseif ('=' == substr($word, $len - 2, 1)) {
  1113. $len -= 2;
  1114. }
  1115. $part = substr($word, 0, $len);
  1116. $word = substr($word, $len);
  1117. if (strlen($word) > 0) {
  1118. $message .= $part . sprintf('=%s', static::$LE);
  1119. } else {
  1120. $buf = $part;
  1121. }
  1122. }
  1123. } else {
  1124. $buf_o = $buf;
  1125. if (!$firstword) {
  1126. $buf .= ' ';
  1127. }
  1128. $buf .= $word;
  1129. if (strlen($buf) > $length and '' != $buf_o) {
  1130. $message .= $buf_o . $soft_break;
  1131. $buf = $word;
  1132. }
  1133. }
  1134. $firstword = false;
  1135. }
  1136. $message .= $buf . static::$LE;
  1137. }
  1138. return $message;
  1139. }
  1140.  
  1141. public function utf8CharBoundary($encodedText, $maxLength)
  1142. {
  1143. $foundSplitPos = false;
  1144. $lookBack = 3;
  1145. while (!$foundSplitPos) {
  1146. $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
  1147. $encodedCharPos = strpos($lastChunk, '=');
  1148. if (false !== $encodedCharPos) {
  1149. $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
  1150. $dec = hexdec($hex);
  1151. if ($dec < 128) {
  1152. if ($encodedCharPos > 0) {
  1153. $maxLength -= $lookBack - $encodedCharPos;
  1154. }
  1155. $foundSplitPos = true;
  1156. } elseif ($dec >= 192) {
  1157. $maxLength -= $lookBack - $encodedCharPos;
  1158. $foundSplitPos = true;
  1159. } elseif ($dec < 192) {
  1160. $lookBack += 3;
  1161. }
  1162. } else {
  1163. $foundSplitPos = true;
  1164. }
  1165. }
  1166. return $maxLength;
  1167. }
  1168.  
  1169. public function setWordWrap()
  1170. {
  1171. if ($this->WordWrap < 1) {
  1172. return;
  1173. }
  1174. switch ($this->message_type) {
  1175. case 'alt':
  1176. case 'alt_inline':
  1177. case 'alt_attach':
  1178. case 'alt_inline_attach':
  1179. $this->AltBody = $this->wrapText($this->AltBody, $this->WordWrap);
  1180. break;
  1181. default:
  1182. $this->Body = $this->wrapText($this->Body, $this->WordWrap);
  1183. break;
  1184. }
  1185. }
  1186.  
  1187. public function createHeader()
  1188. {
  1189. $result = '';
  1190. $result .= $this->headerLine('Date', '' == $this->MessageDate ? self::rfcDate() : $this->MessageDate);
  1191. if ($this->SingleTo) {
  1192. if ('mail' != $this->Mailer) {
  1193. foreach ($this->to as $toaddr) {
  1194. $this->SingleToArray[] = $this->addrFormat($toaddr);
  1195. }
  1196. }
  1197. } else {
  1198. if (count($this->to) > 0) {
  1199. if ('mail' != $this->Mailer) {
  1200. $result .= $this->addrAppend('To', $this->to);
  1201. }
  1202. } elseif (count($this->cc) == 0) {
  1203. $result .= $this->headerLine('To', 'undisclosed-recipients:;');
  1204. }
  1205. }
  1206. $result .= $this->addrAppend('From', [[trim($this->From), $this->FromName]]);
  1207. if (count($this->cc) > 0) {
  1208. $result .= $this->addrAppend('Cc', $this->cc);
  1209. }
  1210. if ((
  1211. 'sendmail' == $this->Mailer or 'qmail' == $this->Mailer or 'mail' == $this->Mailer
  1212. )
  1213. and count($this->bcc) > 0
  1214. ) {
  1215. $result .= $this->addrAppend('Bcc', $this->bcc);
  1216. }
  1217. if (count($this->ReplyTo) > 0) {
  1218. $result .= $this->addrAppend('Reply-To', $this->ReplyTo);
  1219. }
  1220. if ('mail' != $this->Mailer) {
  1221. $result .= $this->headerLine('Subject', $this->encodeHeader($this->secureHeader($this->Subject)));
  1222. }
  1223. if ('' != $this->MessageID and preg_match('/^<.*@.*>$/', $this->MessageID)) {
  1224. $this->lastMessageID = $this->MessageID;
  1225. } else {
  1226. $this->lastMessageID = sprintf('<%s@%s>', $this->uniqueid, $this->serverHostname());
  1227. }
  1228. $result .= $this->headerLine('Message-ID', $this->lastMessageID);
  1229. if (null !== $this->Priority) {
  1230. $result .= $this->headerLine('X-Priority', $this->Priority);
  1231. }
  1232. if ('' == $this->XMailer) {
  1233. $result .= $this->headerLine(
  1234. 'X-Mailer',
  1235. 'PHPMailer ' . self::VERSION . ' (https://github.com/PHPMailer/PHPMailer)'
  1236. );
  1237. } else {
  1238. $myXmailer = trim($this->XMailer);
  1239. if ($myXmailer) {
  1240. $result .= $this->headerLine('X-Mailer', $myXmailer);
  1241. }
  1242. }
  1243. if ('' != $this->ConfirmReadingTo) {
  1244. $result .= $this->headerLine('Disposition-Notification-To', '<' . $this->ConfirmReadingTo . '>');
  1245. }
  1246. foreach ($this->CustomHeader as $header) {
  1247. $result .= $this->headerLine(
  1248. trim($header[0]),
  1249. $this->encodeHeader(trim($header[1]))
  1250. );
  1251. }
  1252. if (!$this->sign_key_file) {
  1253. $result .= $this->headerLine('MIME-Version', '1.0');
  1254. $result .= $this->getMailMIME();
  1255. }
  1256. return $result;
  1257. }
  1258.  
  1259. public function getMailMIME()
  1260. {
  1261. $result = '';
  1262. $ismultipart = true;
  1263. switch ($this->message_type) {
  1264. case 'inline':
  1265. $result .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';');
  1266. $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"');
  1267. break;
  1268. case 'attach':
  1269. case 'inline_attach':
  1270. case 'alt_attach':
  1271. case 'alt_inline_attach':
  1272. $result .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_MIXED . ';');
  1273. $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"');
  1274. break;
  1275. case 'alt':
  1276. case 'alt_inline':
  1277. $result .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';');
  1278. $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"');
  1279. break;
  1280. default:
  1281. $result .= $this->textLine('Content-Type: ' . $this->ContentType . '; charset=' . $this->CharSet);
  1282. $ismultipart = false;
  1283. break;
  1284. }
  1285. if (static::ENCODING_7BIT != $this->Encoding) {
  1286. if ($ismultipart) {
  1287. if (static::ENCODING_8BIT == $this->Encoding) {
  1288. $result .= $this->headerLine('Content-Transfer-Encoding', static::ENCODING_8BIT);
  1289. }
  1290. } else {
  1291. $result .= $this->headerLine('Content-Transfer-Encoding', $this->Encoding);
  1292. }
  1293. }
  1294. if ('mail' != $this->Mailer) {
  1295. $result .= static::$LE;
  1296. }
  1297. return $result;
  1298. }
  1299.  
  1300. public function getSentMIMEMessage()
  1301. {
  1302. return rtrim($this->MIMEHeader . $this->mailHeader, "\n\r") . static::$LE . static::$LE . $this->MIMEBody;
  1303. }
  1304.  
  1305. protected function generateId()
  1306. {
  1307. $len = 32;
  1308. if (function_exists('random_bytes')) {
  1309. $bytes = random_bytes($len);
  1310. } elseif (function_exists('openssl_random_pseudo_bytes')) {
  1311. $bytes = openssl_random_pseudo_bytes($len);
  1312. } else {
  1313. $bytes = hash('sha256', uniqid((string)mt_rand(), true), true);
  1314. }
  1315. return str_replace(['=', '+', '/'], '', base64_encode(hash('sha256', $bytes, true)));
  1316. }
  1317.  
  1318. public function createBody()
  1319. {
  1320. $body = '';
  1321. $this->uniqueid = $this->generateId();
  1322. $this->boundary[1] = 'b1_' . $this->uniqueid;
  1323. $this->boundary[2] = 'b2_' . $this->uniqueid;
  1324. $this->boundary[3] = 'b3_' . $this->uniqueid;
  1325. if ($this->sign_key_file) {
  1326. $body .= $this->getMailMIME() . static::$LE;
  1327. }
  1328. $this->setWordWrap();
  1329. $bodyEncoding = $this->Encoding;
  1330. $bodyCharSet = $this->CharSet;
  1331. if (static::ENCODING_8BIT == $bodyEncoding and !$this->has8bitChars($this->Body)) {
  1332. $bodyEncoding = static::ENCODING_7BIT;
  1333. $bodyCharSet = 'us-ascii';
  1334. }
  1335. if (static::ENCODING_BASE64 != $this->Encoding and static::hasLineLongerThanMax($this->Body)) {
  1336. $bodyEncoding = static::ENCODING_QUOTED_PRINTABLE;
  1337. }
  1338. $altBodyEncoding = $this->Encoding;
  1339. $altBodyCharSet = $this->CharSet;
  1340. if (static::ENCODING_8BIT == $altBodyEncoding and !$this->has8bitChars($this->AltBody)) {
  1341. $altBodyEncoding = static::ENCODING_7BIT;
  1342. $altBodyCharSet = 'us-ascii';
  1343. }
  1344. if (static::ENCODING_BASE64 != $altBodyEncoding and static::hasLineLongerThanMax($this->AltBody)) {
  1345. $altBodyEncoding = static::ENCODING_QUOTED_PRINTABLE;
  1346. }
  1347. $mimepre = 'This is a multi-part message in MIME format.' . static::$LE;
  1348. switch ($this->message_type) {
  1349. case 'inline':
  1350. $body .= $mimepre;
  1351. $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding);
  1352. $body .= $this->encodeString($this->Body, $bodyEncoding);
  1353. $body .= static::$LE;
  1354. $body .= $this->attachAll('inline', $this->boundary[1]);
  1355. break;
  1356. case 'attach':
  1357. $body .= $mimepre;
  1358. $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding);
  1359. $body .= $this->encodeString($this->Body, $bodyEncoding);
  1360. $body .= static::$LE;
  1361. $body .= $this->attachAll('attachment', $this->boundary[1]);
  1362. break;
  1363. case 'inline_attach':
  1364. $body .= $mimepre;
  1365. $body .= $this->textLine('--' . $this->boundary[1]);
  1366. $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';');
  1367. $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
  1368. $body .= static::$LE;
  1369. $body .= $this->getBoundary($this->boundary[2], $bodyCharSet, '', $bodyEncoding);
  1370. $body .= $this->encodeString($this->Body, $bodyEncoding);
  1371. $body .= static::$LE;
  1372. $body .= $this->attachAll('inline', $this->boundary[2]);
  1373. $body .= static::$LE;
  1374. $body .= $this->attachAll('attachment', $this->boundary[1]);
  1375. break;
  1376. case 'alt':
  1377. $body .= $mimepre;
  1378. $body .= $this->getBoundary($this->boundary[1], $altBodyCharSet, static::CONTENT_TYPE_PLAINTEXT, $altBodyEncoding);
  1379. $body .= $this->encodeString($this->AltBody, $altBodyEncoding);
  1380. $body .= static::$LE;
  1381. $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, static::CONTENT_TYPE_TEXT_HTML, $bodyEncoding);
  1382. $body .= $this->encodeString($this->Body, $bodyEncoding);
  1383. $body .= static::$LE;
  1384. if (!empty($this->Ical)) {
  1385. $body .= $this->getBoundary($this->boundary[1], '', static::CONTENT_TYPE_TEXT_CALENDAR . '; method=REQUEST', '');
  1386. $body .= $this->encodeString($this->Ical, $this->Encoding);
  1387. $body .= static::$LE;
  1388. }
  1389. $body .= $this->endBoundary($this->boundary[1]);
  1390. break;
  1391. case 'alt_inline':
  1392. $body .= $mimepre;
  1393. $body .= $this->getBoundary($this->boundary[1], $altBodyCharSet, static::CONTENT_TYPE_PLAINTEXT, $altBodyEncoding);
  1394. $body .= $this->encodeString($this->AltBody, $altBodyEncoding);
  1395. $body .= static::$LE;
  1396. $body .= $this->textLine('--' . $this->boundary[1]);
  1397. $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';');
  1398. $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
  1399. $body .= static::$LE;
  1400. $body .= $this->getBoundary($this->boundary[2], $bodyCharSet, static::CONTENT_TYPE_TEXT_HTML, $bodyEncoding);
  1401. $body .= $this->encodeString($this->Body, $bodyEncoding);
  1402. $body .= static::$LE;
  1403. $body .= $this->attachAll('inline', $this->boundary[2]);
  1404. $body .= static::$LE;
  1405. $body .= $this->endBoundary($this->boundary[1]);
  1406. break;
  1407. case 'alt_attach':
  1408. $body .= $mimepre;
  1409. $body .= $this->textLine('--' . $this->boundary[1]);
  1410. $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';');
  1411. $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
  1412. $body .= static::$LE;
  1413. $body .= $this->getBoundary($this->boundary[2], $altBodyCharSet, static::CONTENT_TYPE_PLAINTEXT, $altBodyEncoding);
  1414. $body .= $this->encodeString($this->AltBody, $altBodyEncoding);
  1415. $body .= static::$LE;
  1416. $body .= $this->getBoundary($this->boundary[2], $bodyCharSet, static::CONTENT_TYPE_TEXT_HTML, $bodyEncoding);
  1417. $body .= $this->encodeString($this->Body, $bodyEncoding);
  1418. $body .= static::$LE;
  1419. if (!empty($this->Ical)) {
  1420. $body .= $this->getBoundary($this->boundary[2], '', static::CONTENT_TYPE_TEXT_CALENDAR . '; method=REQUEST', '');
  1421. $body .= $this->encodeString($this->Ical, $this->Encoding);
  1422. }
  1423. $body .= $this->endBoundary($this->boundary[2]);
  1424. $body .= static::$LE;
  1425. $body .= $this->attachAll('attachment', $this->boundary[1]);
  1426. break;
  1427. case 'alt_inline_attach':
  1428. $body .= $mimepre;
  1429. $body .= $this->textLine('--' . $this->boundary[1]);
  1430. $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';');
  1431. $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
  1432. $body .= static::$LE;
  1433. $body .= $this->getBoundary($this->boundary[2], $altBodyCharSet, static::CONTENT_TYPE_PLAINTEXT, $altBodyEncoding);
  1434. $body .= $this->encodeString($this->AltBody, $altBodyEncoding);
  1435. $body .= static::$LE;
  1436. $body .= $this->textLine('--' . $this->boundary[2]);
  1437. $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';');
  1438. $body .= $this->textLine("\tboundary=\"" . $this->boundary[3] . '"');
  1439. $body .= static::$LE;
  1440. $body .= $this->getBoundary($this->boundary[3], $bodyCharSet, static::CONTENT_TYPE_TEXT_HTML, $bodyEncoding);
  1441. $body .= $this->encodeString($this->Body, $bodyEncoding);
  1442. $body .= static::$LE;
  1443. $body .= $this->attachAll('inline', $this->boundary[3]);
  1444. $body .= static::$LE;
  1445. $body .= $this->endBoundary($this->boundary[2]);
  1446. $body .= static::$LE;
  1447. $body .= $this->attachAll('attachment', $this->boundary[1]);
  1448. break;
  1449. default:
  1450. $this->Encoding = $bodyEncoding;
  1451. $body .= $this->encodeString($this->Body, $this->Encoding);
  1452. break;
  1453. }
  1454. if ($this->isError()) {
  1455. $body = '';
  1456. if ($this->exceptions) {
  1457. throw new Exception($this->lang('empty_message'), self::STOP_CRITICAL);
  1458. }
  1459. } elseif ($this->sign_key_file) {
  1460. try {
  1461. if (!defined('PKCS7_TEXT')) {
  1462. throw new Exception($this->lang('extension_missing') . 'openssl');
  1463. }
  1464. $file = fopen('php://temp', 'rb+');
  1465. $signed = fopen('php://temp', 'rb+');
  1466. fwrite($file, $body);
  1467. if (empty($this->sign_extracerts_file)) {
  1468. $sign = @openssl_pkcs7_sign(
  1469. $file,
  1470. $signed,
  1471. 'file://' . realpath($this->sign_cert_file),
  1472. ['file://' . realpath($this->sign_key_file), $this->sign_key_pass],
  1473. []
  1474. );
  1475. } else {
  1476. $sign = @openssl_pkcs7_sign(
  1477. $file,
  1478. $signed,
  1479. 'file://' . realpath($this->sign_cert_file),
  1480. ['file://' . realpath($this->sign_key_file), $this->sign_key_pass],
  1481. [],
  1482. PKCS7_DETACHED,
  1483. $this->sign_extracerts_file
  1484. );
  1485. }
  1486. fclose($file);
  1487. if ($sign) {
  1488. $body = file_get_contents($signed);
  1489. fclose($signed);
  1490. $parts = explode("\n\n", $body, 2);
  1491. $this->MIMEHeader .= $parts[0] . static::$LE . static::$LE;
  1492. $body = $parts[1];
  1493. } else {
  1494. fclose($signed);
  1495. throw new Exception($this->lang('signing') . openssl_error_string());
  1496. }
  1497. } catch (Exception $exc) {
  1498. $body = '';
  1499. if ($this->exceptions) {
  1500. throw $exc;
  1501. }
  1502. }
  1503. }
  1504. return $body;
  1505. }
  1506.  
  1507. protected function getBoundary($boundary, $charSet, $contentType, $encoding)
  1508. {
  1509. $result = '';
  1510. if ('' == $charSet) {
  1511. $charSet = $this->CharSet;
  1512. }
  1513. if ('' == $contentType) {
  1514. $contentType = $this->ContentType;
  1515. }
  1516. if ('' == $encoding) {
  1517. $encoding = $this->Encoding;
  1518. }
  1519. $result .= $this->textLine('--' . $boundary);
  1520. $result .= sprintf('Content-Type: %s; charset=%s', $contentType, $charSet);
  1521. $result .= static::$LE;
  1522. if (static::ENCODING_7BIT != $encoding) {
  1523. $result .= $this->headerLine('Content-Transfer-Encoding', $encoding);
  1524. }
  1525. $result .= static::$LE;
  1526. return $result;
  1527. }
  1528.  
  1529. protected function endBoundary($boundary)
  1530. {
  1531. return static::$LE . '--' . $boundary . '--' . static::$LE;
  1532. }
  1533.  
  1534. protected function setMessageType()
  1535. {
  1536. $type = [];
  1537. if ($this->alternativeExists()) {
  1538. $type[] = 'alt';
  1539. }
  1540. if ($this->inlineImageExists()) {
  1541. $type[] = 'inline';
  1542. }
  1543. if ($this->attachmentExists()) {
  1544. $type[] = 'attach';
  1545. }
  1546. $this->message_type = implode('_', $type);
  1547. if ('' == $this->message_type) {
  1548. $this->message_type = 'plain';
  1549. }
  1550. }
  1551.  
  1552. public function headerLine($name, $value)
  1553. {
  1554. return $name . ': ' . $value . static::$LE;
  1555. }
  1556.  
  1557. public function textLine($value)
  1558. {
  1559. return $value . static::$LE;
  1560. }
  1561.  
  1562. public function addAttachment($path, $name = '', $encoding = self::ENCODING_BASE64, $type = '', $disposition = 'attachment')
  1563. {
  1564. try {
  1565. if (!static::isPermittedPath($path) || !@is_file($path)) {
  1566. throw new Exception($this->lang('file_access') . $path, self::STOP_CONTINUE);
  1567. }
  1568. if ('' == $type) {
  1569. $type = static::filenameToType($path);
  1570. }
  1571. $filename = basename($path);
  1572. if ('' == $name) {
  1573. $name = $filename;
  1574. }
  1575. $this->attachment[] = [
  1576. 0 => $path,
  1577. 1 => $filename,
  1578. 2 => $name,
  1579. 3 => $encoding,
  1580. 4 => $type,
  1581. 5 => false, 6 => $disposition,
  1582. 7 => $name,
  1583. ];
  1584. } catch (Exception $exc) {
  1585. $this->setError($exc->getMessage());
  1586. $this->edebug($exc->getMessage());
  1587. if ($this->exceptions) {
  1588. throw $exc;
  1589. }
  1590. return false;
  1591. }
  1592. return true;
  1593. }
  1594.  
  1595. public function getAttachments()
  1596. {
  1597. return $this->attachment;
  1598. }
  1599.  
  1600. protected function attachAll($disposition_type, $boundary)
  1601. {
  1602. $mime = [];
  1603. $cidUniq = [];
  1604. $incl = [];
  1605. foreach ($this->attachment as $attachment) {
  1606. if ($attachment[6] == $disposition_type) {
  1607. $string = '';
  1608. $path = '';
  1609. $bString = $attachment[5];
  1610. if ($bString) {
  1611. $string = $attachment[0];
  1612. } else {
  1613. $path = $attachment[0];
  1614. }
  1615. $inclhash = hash('sha256', serialize($attachment));
  1616. if (in_array($inclhash, $incl)) {
  1617. continue;
  1618. }
  1619. $incl[] = $inclhash;
  1620. $name = $attachment[2];
  1621. $encoding = $attachment[3];
  1622. $type = $attachment[4];
  1623. $disposition = $attachment[6];
  1624. $cid = $attachment[7];
  1625. if ('inline' == $disposition and array_key_exists($cid, $cidUniq)) {
  1626. continue;
  1627. }
  1628. $cidUniq[$cid] = true;
  1629. $mime[] = sprintf('--%s%s', $boundary, static::$LE);
  1630. if (!empty($name)) {
  1631. $mime[] = sprintf(
  1632. 'Content-Type: %s; name="%s"%s',
  1633. $type,
  1634. $this->encodeHeader($this->secureHeader($name)),
  1635. static::$LE
  1636. );
  1637. } else {
  1638. $mime[] = sprintf(
  1639. 'Content-Type: %s%s',
  1640. $type,
  1641. static::$LE
  1642. );
  1643. }
  1644. if (static::ENCODING_7BIT != $encoding) {
  1645. $mime[] = sprintf('Content-Transfer-Encoding: %s%s', $encoding, static::$LE);
  1646. }
  1647. if (!empty($cid)) {
  1648. $mime[] = sprintf('Content-ID: <%s>%s', $cid, static::$LE);
  1649. }
  1650. if (!(empty($disposition))) {
  1651. $encoded_name = $this->encodeHeader($this->secureHeader($name));
  1652. if (preg_match('/[ \(\)<>@,;:\\"\/\[\]\?=]/', $encoded_name)) {
  1653. $mime[] = sprintf(
  1654. 'Content-Disposition: %s; filename="%s"%s',
  1655. $disposition,
  1656. $encoded_name,
  1657. static::$LE . static::$LE
  1658. );
  1659. } else {
  1660. if (!empty($encoded_name)) {
  1661. $mime[] = sprintf(
  1662. 'Content-Disposition: %s; filename=%s%s',
  1663. $disposition,
  1664. $encoded_name,
  1665. static::$LE . static::$LE
  1666. );
  1667. } else {
  1668. $mime[] = sprintf(
  1669. 'Content-Disposition: %s%s',
  1670. $disposition,
  1671. static::$LE . static::$LE
  1672. );
  1673. }
  1674. }
  1675. } else {
  1676. $mime[] = static::$LE;
  1677. }
  1678. if ($bString) {
  1679. $mime[] = $this->encodeString($string, $encoding);
  1680. } else {
  1681. $mime[] = $this->encodeFile($path, $encoding);
  1682. }
  1683. if ($this->isError()) {
  1684. return '';
  1685. }
  1686. $mime[] = static::$LE;
  1687. }
  1688. }
  1689. $mime[] = sprintf('--%s--%s', $boundary, static::$LE);
  1690. return implode('', $mime);
  1691. }
  1692.  
  1693. protected function encodeFile($path, $encoding = self::ENCODING_BASE64)
  1694. {
  1695. try {
  1696. if (!static::isPermittedPath($path) || !file_exists($path)) {
  1697. throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE);
  1698. }
  1699. $file_buffer = file_get_contents($path);
  1700. if (false === $file_buffer) {
  1701. throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE);
  1702. }
  1703. $file_buffer = $this->encodeString($file_buffer, $encoding);
  1704. return $file_buffer;
  1705. } catch (Exception $exc) {
  1706. $this->setError($exc->getMessage());
  1707. return '';
  1708. }
  1709. }
  1710.  
  1711. public function encodeString($str, $encoding = self::ENCODING_BASE64)
  1712. {
  1713. $encoded = '';
  1714. switch (strtolower($encoding)) {
  1715. case static::ENCODING_BASE64:
  1716. $encoded = chunk_split(
  1717. base64_encode($str),
  1718. static::STD_LINE_LENGTH,
  1719. static::$LE
  1720. );
  1721. break;
  1722. case static::ENCODING_7BIT:
  1723. case static::ENCODING_8BIT:
  1724. $encoded = static::normalizeBreaks($str);
  1725. if (substr($encoded, -(strlen(static::$LE))) != static::$LE) {
  1726. $encoded .= static::$LE;
  1727. }
  1728. break;
  1729. case static::ENCODING_BINARY:
  1730. $encoded = $str;
  1731. break;
  1732. case static::ENCODING_QUOTED_PRINTABLE:
  1733. $encoded = $this->encodeQP($str);
  1734. break;
  1735. default:
  1736. $this->setError($this->lang('encoding') . $encoding);
  1737. break;
  1738. }
  1739. return $encoded;
  1740. }
  1741.  
  1742. public function encodeHeader($str, $position = 'text')
  1743. {
  1744. $matchcount = 0;
  1745. switch (strtolower($position)) {
  1746. case 'phrase':
  1747. if (!preg_match('/[\200-\377]/', $str)) {
  1748. $encoded = addcslashes($str, "\0..\37\177\\\"");
  1749. if (($str == $encoded) and !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
  1750. return $encoded;
  1751. }
  1752. return "\"$encoded\"";
  1753. }
  1754. $matchcount = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
  1755. break;
  1756.  
  1757. case 'comment':
  1758. $matchcount = preg_match_all('/[()"]/', $str, $matches);
  1759. case 'text':
  1760. default:
  1761. $matchcount += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
  1762. break;
  1763. }
  1764. $lengthsub = 'mail' == $this->Mailer ? 13 : 0;
  1765. $maxlen = static::STD_LINE_LENGTH - $lengthsub;
  1766. if ($matchcount > strlen($str) / 3) {
  1767. $encoding = 'B';
  1768. $maxlen = static::STD_LINE_LENGTH - $lengthsub - 8 - strlen($this->CharSet);
  1769. if ($this->hasMultiBytes($str)) {
  1770. $encoded = $this->base64EncodeWrapMB($str, "\n");
  1771. } else {
  1772. $encoded = base64_encode($str);
  1773. $maxlen -= $maxlen % 4;
  1774. $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
  1775. }
  1776. $encoded = preg_replace('/^(.*)$/m', ' =?' . $this->CharSet . "?$encoding?\\1?=", $encoded);
  1777. } elseif ($matchcount > 0) {
  1778. $encoding = 'Q';
  1779. $maxlen = static::STD_LINE_LENGTH - $lengthsub - 8 - strlen($this->CharSet);
  1780. $encoded = $this->encodeQ($str, $position);
  1781. $encoded = $this->wrapText($encoded, $maxlen, true);
  1782. $encoded = str_replace('=' . static::$LE, "\n", trim($encoded));
  1783. $encoded = preg_replace('/^(.*)$/m', ' =?' . $this->CharSet . "?$encoding?\\1?=", $encoded);
  1784. } elseif (strlen($str) > $maxlen) {
  1785. $encoded = trim($this->wrapText($str, $maxlen, false));
  1786. if ($str == $encoded) {
  1787. $encoded = trim(chunk_split($str, static::STD_LINE_LENGTH, static::$LE));
  1788. }
  1789. $encoded = str_replace(static::$LE, "\n", trim($encoded));
  1790. $encoded = preg_replace('/^(.*)$/m', ' \\1', $encoded);
  1791. } else {
  1792. return $str;
  1793. }
  1794. return trim(static::normalizeBreaks($encoded));
  1795. }
  1796.  
  1797. public function hasMultiBytes($str)
  1798. {
  1799. if (function_exists('mb_strlen')) {
  1800. return strlen($str) > mb_strlen($str, $this->CharSet);
  1801. }
  1802. return false;
  1803. }
  1804.  
  1805. public function has8bitChars($text)
  1806. {
  1807. return (bool)preg_match('/[\x80-\xFF]/', $text);
  1808. }
  1809.  
  1810. public function base64EncodeWrapMB($str, $linebreak = null)
  1811. {
  1812. $start = '=?' . $this->CharSet . '?B?';
  1813. $end = '?=';
  1814. $encoded = '';
  1815. if (null === $linebreak) {
  1816. $linebreak = static::$LE;
  1817. }
  1818. $mb_length = mb_strlen($str, $this->CharSet);
  1819. $length = 75 - strlen($start) - strlen($end);
  1820. $ratio = $mb_length / strlen($str);
  1821. $avgLength = floor($length * $ratio * .75);
  1822. for ($i = 0; $i < $mb_length; $i += $offset) {
  1823. $lookBack = 0;
  1824. do {
  1825. $offset = $avgLength - $lookBack;
  1826. $chunk = mb_substr($str, $i, $offset, $this->CharSet);
  1827. $chunk = base64_encode($chunk);
  1828. ++$lookBack;
  1829. } while (strlen($chunk) > $length);
  1830. $encoded .= $chunk . $linebreak;
  1831. }
  1832. return substr($encoded, 0, -strlen($linebreak));
  1833. }
  1834.  
  1835. public function encodeQP($string)
  1836. {
  1837. return static::normalizeBreaks(quoted_printable_encode($string));
  1838. }
  1839.  
  1840. public function encodeQ($str, $position = 'text')
  1841. {
  1842. $pattern = '';
  1843. $encoded = str_replace(["\r", "\n"], '', $str);
  1844. switch (strtolower($position)) {
  1845. case 'phrase':
  1846. $pattern = '^A-Za-z0-9!*+\/ -';
  1847. break;
  1848.  
  1849.  
  1850. case 'comment':
  1851. $pattern = '\(\)"';
  1852.  
  1853. case 'text':
  1854. default:
  1855.  
  1856. $pattern = '\000-\011\013\014\016-\037\075\077\137\177-\377' . $pattern;
  1857. break;
  1858. }
  1859. $matches = [];
  1860. if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) {
  1861. $eqkey = array_search('=', $matches[0]);
  1862. if (false !== $eqkey) {
  1863. unset($matches[0][$eqkey]);
  1864. array_unshift($matches[0], '=');
  1865. }
  1866. foreach (array_unique($matches[0]) as $char) {
  1867. $encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded);
  1868. }
  1869. }
  1870. return str_replace(' ', '_', $encoded);
  1871. }
  1872.  
  1873. public function addStringAttachment(
  1874. $string,
  1875. $filename,
  1876. $encoding = self::ENCODING_BASE64,
  1877. $type = '',
  1878. $disposition = 'attachment'
  1879. )
  1880. {
  1881. if ('' == $type) {
  1882. $type = static::filenameToType($filename);
  1883. }
  1884. $this->attachment[] = [
  1885. 0 => $string,
  1886. 1 => $filename,
  1887. 2 => basename($filename),
  1888. 3 => $encoding,
  1889. 4 => $type,
  1890. 5 => true, 6 => $disposition,
  1891. 7 => 0,
  1892. ];
  1893. }
  1894.  
  1895. public function addEmbeddedImage($path, $cid, $name = '', $encoding = self::ENCODING_BASE64, $type = '', $disposition = 'inline')
  1896. {
  1897. if (!static::isPermittedPath($path) || !@is_file($path)) {
  1898. $this->setError($this->lang('file_access') . $path);
  1899. return false;
  1900. }
  1901. if ('' == $type) {
  1902. $type = static::filenameToType($path);
  1903. }
  1904. $filename = basename($path);
  1905. if ('' == $name) {
  1906. $name = $filename;
  1907. }
  1908. $this->attachment[] = [
  1909. 0 => $path,
  1910. 1 => $filename,
  1911. 2 => $name,
  1912. 3 => $encoding,
  1913. 4 => $type,
  1914. 5 => false, 6 => $disposition,
  1915. 7 => $cid,
  1916. ];
  1917. return true;
  1918. }
  1919.  
  1920. public function addStringEmbeddedImage(
  1921. $string,
  1922. $cid,
  1923. $name = '',
  1924. $encoding = self::ENCODING_BASE64,
  1925. $type = '',
  1926. $disposition = 'inline'
  1927. )
  1928. {
  1929. if ('' == $type and !empty($name)) {
  1930. $type = static::filenameToType($name);
  1931. }
  1932. $this->attachment[] = [
  1933. 0 => $string,
  1934. 1 => $name,
  1935. 2 => $name,
  1936. 3 => $encoding,
  1937. 4 => $type,
  1938. 5 => true, 6 => $disposition,
  1939. 7 => $cid,
  1940. ];
  1941. return true;
  1942. }
  1943.  
  1944. protected function cidExists($cid)
  1945. {
  1946. foreach ($this->attachment as $attachment) {
  1947. if ('inline' == $attachment[6] and $cid == $attachment[7]) {
  1948. return true;
  1949. }
  1950. }
  1951. return false;
  1952. }
  1953.  
  1954. public function inlineImageExists()
  1955. {
  1956. foreach ($this->attachment as $attachment) {
  1957. if ('inline' == $attachment[6]) {
  1958. return true;
  1959. }
  1960. }
  1961. return false;
  1962. }
  1963.  
  1964. public function attachmentExists()
  1965. {
  1966. foreach ($this->attachment as $attachment) {
  1967. if ('attachment' == $attachment[6]) {
  1968. return true;
  1969. }
  1970. }
  1971. return false;
  1972. }
  1973.  
  1974. public function alternativeExists()
  1975. {
  1976. return !empty($this->AltBody);
  1977. }
  1978.  
  1979. public function clearQueuedAddresses($kind)
  1980. {
  1981. $this->RecipientsQueue = array_filter(
  1982. $this->RecipientsQueue,
  1983. function ($params) use ($kind) {
  1984. return $params[0] != $kind;
  1985. }
  1986. );
  1987. }
  1988.  
  1989. public function clearAddresses()
  1990. {
  1991. foreach ($this->to as $to) {
  1992. unset($this->all_recipients[strtolower($to[0])]);
  1993. }
  1994. $this->to = [];
  1995. $this->clearQueuedAddresses('to');
  1996. }
  1997.  
  1998. public function clearCCs()
  1999. {
  2000. foreach ($this->cc as $cc) {
  2001. unset($this->all_recipients[strtolower($cc[0])]);
  2002. }
  2003. $this->cc = [];
  2004. $this->clearQueuedAddresses('cc');
  2005. }
  2006.  
  2007. public function clearBCCs()
  2008. {
  2009. foreach ($this->bcc as $bcc) {
  2010. unset($this->all_recipients[strtolower($bcc[0])]);
  2011. }
  2012. $this->bcc = [];
  2013. $this->clearQueuedAddresses('bcc');
  2014. }
  2015.  
  2016. public function clearReplyTos()
  2017. {
  2018. $this->ReplyTo = [];
  2019. $this->ReplyToQueue = [];
  2020. }
  2021.  
  2022. public function clearAllRecipients()
  2023. {
  2024. $this->to = [];
  2025. $this->cc = [];
  2026. $this->bcc = [];
  2027. $this->all_recipients = [];
  2028. $this->RecipientsQueue = [];
  2029. }
  2030.  
  2031. public function clearAttachments()
  2032. {
  2033. $this->attachment = [];
  2034. }
  2035.  
  2036. public function clearCustomHeaders()
  2037. {
  2038. $this->CustomHeader = [];
  2039. }
  2040.  
  2041. protected function setError($msg)
  2042. {
  2043. ++$this->error_count;
  2044. if ('smtp' == $this->Mailer and null !== $this->smtp) {
  2045. $lasterror = $this->smtp->getError();
  2046. if (!empty($lasterror['error'])) {
  2047. $msg .= $this->lang('smtp_error') . $lasterror['error'];
  2048. if (!empty($lasterror['detail'])) {
  2049. $msg .= ' Detail: ' . $lasterror['detail'];
  2050. }
  2051. if (!empty($lasterror['smtp_code'])) {
  2052. $msg .= ' SMTP code: ' . $lasterror['smtp_code'];
  2053. }
  2054. if (!empty($lasterror['smtp_code_ex'])) {
  2055. $msg .= ' Additional SMTP info: ' . $lasterror['smtp_code_ex'];
  2056. }
  2057. }
  2058. }
  2059. $this->ErrorInfo = $msg;
  2060. }
  2061.  
  2062. public static function rfcDate()
  2063. {
  2064. date_default_timezone_set(@date_default_timezone_get());
  2065. return date('D, j M Y H:i:s O');
  2066. }
  2067.  
  2068. protected function serverHostname()
  2069. {
  2070. $result = '';
  2071. if (!empty($this->Hostname)) {
  2072. $result = $this->Hostname;
  2073. } elseif (isset($_SERVER) and array_key_exists('SERVER_NAME', $_SERVER)) {
  2074. $result = $_SERVER['SERVER_NAME'];
  2075. } elseif (function_exists('gethostname') and gethostname() !== false) {
  2076. $result = gethostname();
  2077. } elseif (php_uname('n') !== false) {
  2078. $result = php_uname('n');
  2079. }
  2080. if (!static::isValidHost($result)) {
  2081. return 'localhost.localdomain';
  2082. }
  2083. return $result;
  2084. }
  2085.  
  2086. public static function isValidHost($host)
  2087. {
  2088. if (empty($host)
  2089. or !is_string($host)
  2090. or strlen($host) > 256
  2091. ) {
  2092. return false;
  2093. }
  2094. if (trim($host, '[]') != $host) {
  2095. return (bool)filter_var(trim($host, '[]'), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
  2096. }
  2097. if (is_numeric(str_replace('.', '', $host))) {
  2098. return (bool)filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
  2099. }
  2100. if (filter_var('http://' . $host, FILTER_VALIDATE_URL)) {
  2101. return true;
  2102. }
  2103. return false;
  2104. }
  2105.  
  2106. protected function lang($key)
  2107. {
  2108. if (count($this->language) < 1) {
  2109. $this->setLanguage('en');
  2110. }
  2111. if (array_key_exists($key, $this->language)) {
  2112. if ('smtp_connect_failed' == $key) {
  2113. return $this->language[$key] . ' https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting';
  2114. }
  2115. return $this->language[$key];
  2116. }
  2117. return $key;
  2118. }
  2119.  
  2120. public function isError()
  2121. {
  2122. return $this->error_count > 0;
  2123. }
  2124.  
  2125. public function addCustomHeader($name, $value = null)
  2126. {
  2127. if (null === $value) {
  2128. $this->CustomHeader[] = explode(':', $name, 2);
  2129. } else {
  2130. $this->CustomHeader[] = [$name, $value];
  2131. }
  2132. }
  2133.  
  2134. public function getCustomHeaders()
  2135. {
  2136. return $this->CustomHeader;
  2137. }
  2138.  
  2139. public function msgHTML($message, $basedir = '', $advanced = false)
  2140. {
  2141. preg_match_all('/(src|background)=["\'](.*)["\']/Ui', $message, $images);
  2142. if (array_key_exists(2, $images)) {
  2143. if (strlen($basedir) > 1 && '/' != substr($basedir, -1)) {
  2144. $basedir .= '/';
  2145. }
  2146. foreach ($images[2] as $imgindex => $url) {
  2147. if (preg_match('#^data:(image/(?:jpe?g|gif|png));?(base64)?,(.+)#', $url, $match)) {
  2148. if (count($match) == 4 and static::ENCODING_BASE64 == $match[2]) {
  2149. $data = base64_decode($match[3]);
  2150. } elseif ('' == $match[2]) {
  2151. $data = rawurldecode($match[3]);
  2152. } else {
  2153. continue;
  2154. }
  2155. $cid = hash('sha256', $data) . '@phpmailer.0';
  2156. if (!$this->cidExists($cid)) {
  2157. $this->addStringEmbeddedImage($data, $cid, 'embed' . $imgindex, static::ENCODING_BASE64, $match[1]);
  2158. }
  2159. $message = str_replace(
  2160. $images[0][$imgindex],
  2161. $images[1][$imgindex] . '="cid:' . $cid . '"',
  2162. $message
  2163. );
  2164. continue;
  2165. }
  2166. if (!empty($basedir)
  2167. and (strpos($url, '..') === false)
  2168. and 0 !== strpos($url, 'cid:')
  2169. and !preg_match('#^[a-z][a-z0-9+.-]*:?//#i', $url)
  2170. ) {
  2171. $filename = basename($url);
  2172. $directory = dirname($url);
  2173. if ('.' == $directory) {
  2174. $directory = '';
  2175. }
  2176. $cid = hash('sha256', $url) . '@phpmailer.0';
  2177. if (strlen($basedir) > 1 and '/' != substr($basedir, -1)) {
  2178. $basedir .= '/';
  2179. }
  2180. if (strlen($directory) > 1 and '/' != substr($directory, -1)) {
  2181. $directory .= '/';
  2182. }
  2183. if ($this->addEmbeddedImage(
  2184. $basedir . $directory . $filename,
  2185. $cid,
  2186. $filename,
  2187. static::ENCODING_BASE64,
  2188. static::_mime_types((string)static::mb_pathinfo($filename, PATHINFO_EXTENSION))
  2189. )
  2190. ) {
  2191. $message = preg_replace(
  2192. '/' . $images[1][$imgindex] . '=["\']' . preg_quote($url, '/') . '["\']/Ui',
  2193. $images[1][$imgindex] . '="cid:' . $cid . '"',
  2194. $message
  2195. );
  2196. }
  2197. }
  2198. }
  2199. }
  2200. $this->isHTML(true);
  2201. $this->Body = static::normalizeBreaks($message);
  2202. $this->AltBody = static::normalizeBreaks($this->html2text($message, $advanced));
  2203. if (!$this->alternativeExists()) {
  2204. $this->AltBody = 'This is an HTML-only message. To view it, activate HTML in your email application.'
  2205. . static::$LE;
  2206. }
  2207. return $this->Body;
  2208. }
  2209.  
  2210. public function html2text($html, $advanced = false)
  2211. {
  2212. if (is_callable($advanced)) {
  2213. return call_user_func($advanced, $html);
  2214. }
  2215. return html_entity_decode(
  2216. trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/si', '', $html))),
  2217. ENT_QUOTES,
  2218. $this->CharSet
  2219. );
  2220. }
  2221.  
  2222. public static function _mime_types($ext = '')
  2223. {
  2224. $mimes = [
  2225. 'xl' => 'application/excel',
  2226. 'js' => 'application/javascript',
  2227. 'hqx' => 'application/mac-binhex40',
  2228. 'cpt' => 'application/mac-compactpro',
  2229. 'bin' => 'application/macbinary',
  2230. 'doc' => 'application/msword',
  2231. 'word' => 'application/msword',
  2232. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  2233. 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  2234. 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
  2235. 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  2236. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  2237. 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  2238. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  2239. 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  2240. 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
  2241. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  2242. 'class' => 'application/octet-stream',
  2243. 'dll' => 'application/octet-stream',
  2244. 'dms' => 'application/octet-stream',
  2245. 'exe' => 'application/octet-stream',
  2246. 'lha' => 'application/octet-stream',
  2247. 'lzh' => 'application/octet-stream',
  2248. 'psd' => 'application/octet-stream',
  2249. 'sea' => 'application/octet-stream',
  2250. 'so' => 'application/octet-stream',
  2251. 'oda' => 'application/oda',
  2252. 'pdf' => 'application/pdf',
  2253. 'ai' => 'application/postscript',
  2254. 'eps' => 'application/postscript',
  2255. 'ps' => 'application/postscript',
  2256. 'smi' => 'application/smil',
  2257. 'smil' => 'application/smil',
  2258. 'mif' => 'application/vnd.mif',
  2259. 'xls' => 'application/vnd.ms-excel',
  2260. 'ppt' => 'application/vnd.ms-powerpoint',
  2261. 'wbxml' => 'application/vnd.wap.wbxml',
  2262. 'wmlc' => 'application/vnd.wap.wmlc',
  2263. 'dcr' => 'application/x-director',
  2264. 'dir' => 'application/x-director',
  2265. 'dxr' => 'application/x-director',
  2266. 'dvi' => 'application/x-dvi',
  2267. 'gtar' => 'application/x-gtar',
  2268. 'php3' => 'application/x-httpd-php',
  2269. 'php4' => 'application/x-httpd-php',
  2270. 'php' => 'application/x-httpd-php',
  2271. 'phtml' => 'application/x-httpd-php',
  2272. 'phps' => 'application/x-httpd-php-source',
  2273. 'swf' => 'application/x-shockwave-flash',
  2274. 'sit' => 'application/x-stuffit',
  2275. 'tar' => 'application/x-tar',
  2276. 'tgz' => 'application/x-tar',
  2277. 'xht' => 'application/xhtml+xml',
  2278. 'xhtml' => 'application/xhtml+xml',
  2279. 'zip' => 'application/zip',
  2280. 'mid' => 'audio/midi',
  2281. 'midi' => 'audio/midi',
  2282. 'mp2' => 'audio/mpeg',
  2283. 'mp3' => 'audio/mpeg',
  2284. 'm4a' => 'audio/mp4',
  2285. 'mpga' => 'audio/mpeg',
  2286. 'aif' => 'audio/x-aiff',
  2287. 'aifc' => 'audio/x-aiff',
  2288. 'aiff' => 'audio/x-aiff',
  2289. 'ram' => 'audio/x-pn-realaudio',
  2290. 'rm' => 'audio/x-pn-realaudio',
  2291. 'rpm' => 'audio/x-pn-realaudio-plugin',
  2292. 'ra' => 'audio/x-realaudio',
  2293. 'wav' => 'audio/x-wav',
  2294. 'mka' => 'audio/x-matroska',
  2295. 'bmp' => 'image/bmp',
  2296. 'gif' => 'image/gif',
  2297. 'jpeg' => 'image/jpeg',
  2298. 'jpe' => 'image/jpeg',
  2299. 'jpg' => 'image/jpeg',
  2300. 'png' => 'image/png',
  2301. 'tiff' => 'image/tiff',
  2302. 'tif' => 'image/tiff',
  2303. 'webp' => 'image/webp',
  2304. 'heif' => 'image/heif',
  2305. 'heifs' => 'image/heif-sequence',
  2306. 'heic' => 'image/heic',
  2307. 'heics' => 'image/heic-sequence',
  2308. 'eml' => 'message/rfc822',
  2309. 'css' => 'text/css',
  2310. 'html' => 'text/html',
  2311. 'htm' => 'text/html',
  2312. 'shtml' => 'text/html',
  2313. 'log' => 'text/plain',
  2314. 'text' => 'text/plain',
  2315. 'txt' => 'text/plain',
  2316. 'rtx' => 'text/richtext',
  2317. 'rtf' => 'text/rtf',
  2318. 'vcf' => 'text/vcard',
  2319. 'vcard' => 'text/vcard',
  2320. 'ics' => 'text/calendar',
  2321. 'xml' => 'text/xml',
  2322. 'xsl' => 'text/xml',
  2323. 'wmv' => 'video/x-ms-wmv',
  2324. 'mpeg' => 'video/mpeg',
  2325. 'mpe' => 'video/mpeg',
  2326. 'mpg' => 'video/mpeg',
  2327. 'mp4' => 'video/mp4',
  2328. 'm4v' => 'video/mp4',
  2329. 'mov' => 'video/quicktime',
  2330. 'qt' => 'video/quicktime',
  2331. 'rv' => 'video/vnd.rn-realvideo',
  2332. 'avi' => 'video/x-msvideo',
  2333. 'movie' => 'video/x-sgi-movie',
  2334. 'webm' => 'video/webm',
  2335. 'mkv' => 'video/x-matroska',
  2336. ];
  2337. $ext = strtolower($ext);
  2338. if (array_key_exists($ext, $mimes)) {
  2339. return $mimes[$ext];
  2340. }
  2341. return 'application/octet-stream';
  2342. }
  2343.  
  2344. public static function filenameToType($filename)
  2345. {
  2346. $qpos = strpos($filename, '?');
  2347. if (false !== $qpos) {
  2348. $filename = substr($filename, 0, $qpos);
  2349. }
  2350. $ext = static::mb_pathinfo($filename, PATHINFO_EXTENSION);
  2351. return static::_mime_types($ext);
  2352. }
  2353.  
  2354. public static function mb_pathinfo($path, $options = null)
  2355. {
  2356. $ret = ['dirname' => '', 'basename' => '', 'extension' => '', 'filename' => ''];
  2357. $pathinfo = [];
  2358. if (preg_match('#^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$#im', $path, $pathinfo)) {
  2359. if (array_key_exists(1, $pathinfo)) {
  2360. $ret['dirname'] = $pathinfo[1];
  2361. }
  2362. if (array_key_exists(2, $pathinfo)) {
  2363. $ret['basename'] = $pathinfo[2];
  2364. }
  2365. if (array_key_exists(5, $pathinfo)) {
  2366. $ret['extension'] = $pathinfo[5];
  2367. }
  2368. if (array_key_exists(3, $pathinfo)) {
  2369. $ret['filename'] = $pathinfo[3];
  2370. }
  2371. }
  2372. switch ($options) {
  2373. case PATHINFO_DIRNAME:
  2374. case 'dirname':
  2375. return $ret['dirname'];
  2376. case PATHINFO_BASENAME:
  2377. case 'basename':
  2378. return $ret['basename'];
  2379. case PATHINFO_EXTENSION:
  2380. case 'extension':
  2381. return $ret['extension'];
  2382. case PATHINFO_FILENAME:
  2383. case 'filename':
  2384. return $ret['filename'];
  2385. default:
  2386. return $ret;
  2387. }
  2388. }
  2389.  
  2390. public function set($name, $value = '')
  2391. {
  2392. if (property_exists($this, $name)) {
  2393. $this->$name = $value;
  2394. return true;
  2395. }
  2396. $this->setError($this->lang('variable_set') . $name);
  2397. return false;
  2398. }
  2399.  
  2400. public function secureHeader($str)
  2401. {
  2402. return trim(str_replace(["\r", "\n"], '', $str));
  2403. }
  2404.  
  2405. public static function normalizeBreaks($text, $breaktype = null)
  2406. {
  2407. if (null === $breaktype) {
  2408. $breaktype = static::$LE;
  2409. }
  2410. $text = str_replace(["\r\n", "\r"], "\n", $text);
  2411. if ("\n" !== $breaktype) {
  2412. $text = str_replace("\n", $breaktype, $text);
  2413. }
  2414. return $text;
  2415. }
  2416.  
  2417. public static function getLE()
  2418. {
  2419. return static::$LE;
  2420. }
  2421.  
  2422. protected static function setLE($le)
  2423. {
  2424. static::$LE = $le;
  2425. }
  2426.  
  2427. public function sign($cert_filename, $key_filename, $key_pass, $extracerts_filename = '')
  2428. {
  2429. $this->sign_cert_file = $cert_filename;
  2430. $this->sign_key_file = $key_filename;
  2431. $this->sign_key_pass = $key_pass;
  2432. $this->sign_extracerts_file = $extracerts_filename;
  2433. }
  2434.  
  2435. public function DKIM_QP($txt)
  2436. {
  2437. $line = '';
  2438. $len = strlen($txt);
  2439. for ($i = 0; $i < $len; ++$i) {
  2440. $ord = ord($txt[$i]);
  2441. if (((0x21 <= $ord) and ($ord <= 0x3A)) or $ord == 0x3C or ((0x3E <= $ord) and ($ord <= 0x7E))) {
  2442. $line .= $txt[$i];
  2443. } else {
  2444. $line .= '=' . sprintf('%02X', $ord);
  2445. }
  2446. }
  2447. return $line;
  2448. }
  2449.  
  2450. public function DKIM_Sign($signHeader)
  2451. {
  2452. if (!defined('PKCS7_TEXT')) {
  2453. if ($this->exceptions) {
  2454. throw new Exception($this->lang('extension_missing') . 'openssl');
  2455. }
  2456. return '';
  2457. }
  2458. $privKeyStr = !empty($this->DKIM_private_string) ?
  2459. $this->DKIM_private_string :
  2460. file_get_contents($this->DKIM_private);
  2461. if ('' != $this->DKIM_passphrase) {
  2462. $privKey = openssl_pkey_get_private($privKeyStr, $this->DKIM_passphrase);
  2463. } else {
  2464. $privKey = openssl_pkey_get_private($privKeyStr);
  2465. }
  2466. if (openssl_sign($signHeader, $signature, $privKey, 'sha256WithRSAEncryption')) {
  2467. openssl_pkey_free($privKey);
  2468. return base64_encode($signature);
  2469. }
  2470. openssl_pkey_free($privKey);
  2471. return '';
  2472. }
  2473.  
  2474. public function DKIM_HeaderC($signHeader)
  2475. {
  2476. $signHeader = preg_replace('/\r\n[ \t]+/', ' ', $signHeader);
  2477. $lines = explode("\r\n", $signHeader);
  2478. foreach ($lines as $key => $line) {
  2479. if (strpos($line, ':') === false) {
  2480. continue;
  2481. }
  2482. list($heading, $value) = explode(':', $line, 2);
  2483. $heading = strtolower($heading);
  2484. $value = preg_replace('/[ \t]{2,}/', ' ', $value);
  2485. $lines[$key] = trim($heading, " \t") . ':' . trim($value, " \t");
  2486. }
  2487. return implode("\r\n", $lines);
  2488. }
  2489.  
  2490. public function DKIM_BodyC($body)
  2491. {
  2492. if (empty($body)) {
  2493. return "\r\n";
  2494. }
  2495. $body = static::normalizeBreaks($body, "\r\n");
  2496. return rtrim($body, "\r\n") . "\r\n";
  2497. }
  2498.  
  2499. public function DKIM_Add($headers_line, $subject, $body)
  2500. {
  2501. $DKIMsignatureType = 'rsa-sha256';
  2502. $DKIMcanonicalization = 'relaxed/simple';
  2503. $DKIMquery = 'dns/txt';
  2504. $DKIMtime = time();
  2505. $subject_header = "Subject: $subject";
  2506. $headers = explode(static::$LE, $headers_line);
  2507. $from_header = '';
  2508. $to_header = '';
  2509. $date_header = '';
  2510. $current = '';
  2511. $copiedHeaderFields = '';
  2512. $foundExtraHeaders = [];
  2513. $extraHeaderKeys = '';
  2514. $extraHeaderValues = '';
  2515. $extraCopyHeaderFields = '';
  2516. foreach ($headers as $header) {
  2517. if (strpos($header, 'From:') === 0) {
  2518. $from_header = $header;
  2519. $current = 'from_header';
  2520. } elseif (strpos($header, 'To:') === 0) {
  2521. $to_header = $header;
  2522. $current = 'to_header';
  2523. } elseif (strpos($header, 'Date:') === 0) {
  2524. $date_header = $header;
  2525. $current = 'date_header';
  2526. } elseif (!empty($this->DKIM_extraHeaders)) {
  2527. foreach ($this->DKIM_extraHeaders as $extraHeader) {
  2528. if (strpos($header, $extraHeader . ':') === 0) {
  2529. $headerValue = $header;
  2530. foreach ($this->CustomHeader as $customHeader) {
  2531. if ($customHeader[0] === $extraHeader) {
  2532. $headerValue = trim($customHeader[0]) .
  2533. ': ' .
  2534. $this->encodeHeader(trim($customHeader[1]));
  2535. break;
  2536. }
  2537. }
  2538. $foundExtraHeaders[$extraHeader] = $headerValue;
  2539. $current = '';
  2540. break;
  2541. }
  2542. }
  2543. } else {
  2544. if (!empty($$current) and strpos($header, ' =?') === 0) {
  2545. $$current .= $header;
  2546. } else {
  2547. $current = '';
  2548. }
  2549. }
  2550. }
  2551. foreach ($foundExtraHeaders as $key => $value) {
  2552. $extraHeaderKeys .= ':' . $key;
  2553. $extraHeaderValues .= $value . "\r\n";
  2554. if ($this->DKIM_copyHeaderFields) {
  2555. $extraCopyHeaderFields .= "\t|" . str_replace('|', '=7C', $this->DKIM_QP($value)) . ";\r\n";
  2556. }
  2557. }
  2558. if ($this->DKIM_copyHeaderFields) {
  2559. $from = str_replace('|', '=7C', $this->DKIM_QP($from_header));
  2560. $to = str_replace('|', '=7C', $this->DKIM_QP($to_header));
  2561. $date = str_replace('|', '=7C', $this->DKIM_QP($date_header));
  2562. $subject = str_replace('|', '=7C', $this->DKIM_QP($subject_header));
  2563. $copiedHeaderFields = "\tz=$from\r\n" .
  2564. "\t|$to\r\n" .
  2565. "\t|$date\r\n" .
  2566. "\t|$subject;\r\n" .
  2567. $extraCopyHeaderFields;
  2568. }
  2569. $body = $this->DKIM_BodyC($body);
  2570. $DKIMlen = strlen($body);
  2571. $DKIMb64 = base64_encode(pack('H*', hash('sha256', $body)));
  2572. if ('' == $this->DKIM_identity) {
  2573. $ident = '';
  2574. } else {
  2575. $ident = ' i=' . $this->DKIM_identity . ';';
  2576. }
  2577. $dkimhdrs = 'DKIM-Signature: v=1; a=' .
  2578. $DKIMsignatureType . '; q=' .
  2579. $DKIMquery . '; l=' .
  2580. $DKIMlen . '; s=' .
  2581. $this->DKIM_selector .
  2582. ";\r\n" .
  2583. "\tt=" . $DKIMtime . '; c=' . $DKIMcanonicalization . ";\r\n" .
  2584. "\th=From:To:Date:Subject" . $extraHeaderKeys . ";\r\n" .
  2585. "\td=" . $this->DKIM_domain . ';' . $ident . "\r\n" .
  2586. $copiedHeaderFields .
  2587. "\tbh=" . $DKIMb64 . ";\r\n" .
  2588. "\tb=";
  2589. $toSign = $this->DKIM_HeaderC(
  2590. $from_header . "\r\n" .
  2591. $to_header . "\r\n" .
  2592. $date_header . "\r\n" .
  2593. $subject_header . "\r\n" .
  2594. $extraHeaderValues .
  2595. $dkimhdrs
  2596. );
  2597. $signed = $this->DKIM_Sign($toSign);
  2598. return static::normalizeBreaks($dkimhdrs . $signed) . static::$LE;
  2599. }
  2600.  
  2601. public static function hasLineLongerThanMax($str)
  2602. {
  2603. return (bool)preg_match('/^(.{' . (self::MAX_LINE_LENGTH + strlen(static::$LE)) . ',})/m', $str);
  2604. }
  2605.  
  2606. public function getToAddresses()
  2607. {
  2608. return $this->to;
  2609. }
  2610.  
  2611. public function getCcAddresses()
  2612. {
  2613. return $this->cc;
  2614. }
  2615.  
  2616. public function getBccAddresses()
  2617. {
  2618. return $this->bcc;
  2619. }
  2620.  
  2621. public function getReplyToAddresses()
  2622. {
  2623. return $this->ReplyTo;
  2624. }
  2625.  
  2626. public function getAllRecipientAddresses()
  2627. {
  2628. return $this->all_recipients;
  2629. }
  2630.  
  2631. protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body, $from, $extra)
  2632. {
  2633. if (!empty($this->action_function) and is_callable($this->action_function)) {
  2634. call_user_func($this->action_function, $isSent, $to, $cc, $bcc, $subject, $body, $from, $extra);
  2635. }
  2636. }
  2637.  
  2638. public function getOAuth()
  2639. {
  2640. return $this->oauth;
  2641. }
  2642.  
  2643. public function setOAuth(OAuth $oauth)
  2644. {
  2645. $this->oauth = $oauth;
  2646. }
  2647. }
  2648.  
  2649. class Dynamizer
  2650. {
  2651. private static $alphaNumericCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  2652. private static $numericCharacters = '0123456789';
  2653. private static $BasicRandomizationMappings = Array(
  2654. '/\[random_string((?:_)(\d*))?\]/' => "generateRandomString",
  2655. '/\[random_int((?:_)(\d*))?\]/' => "generateRandomInteger",
  2656. '/\[random_ipsum((?:_)(\d*))?\]/' => "generateIpsum",
  2657. '/\[random_news((?:_)(\d*))?\]/' => "generateNewsText"
  2658. );
  2659.  
  2660.  
  2661. private static function ProcessGeneric($input)
  2662. {
  2663. $output = self::ReplaceSpecialCharacters($input);
  2664. return $output;
  2665. }
  2666.  
  2667. private static function generateIpsum($length)
  2668. {
  2669. if ($length == null) $length = 25;
  2670. return simplexml_load_file("http://www.lipsum.com/feed/xml?amount=$length&what=words&start=0")->lipsum;
  2671.  
  2672. }
  2673.  
  2674. public static function Dynamize($haystack)
  2675. {
  2676. foreach (self::$BasicRandomizationMappings as $key => $value) {
  2677.  
  2678. $haystack = self::RandomizeAtomic($haystack, $key, $value);
  2679. }
  2680. $haystack = self::ProcessGeneric($haystack);
  2681. return $haystack;
  2682. }
  2683.  
  2684.  
  2685. private static function ReplaceSpecialCharacters($input)
  2686. {
  2687. $input = str_ireplace("[verified_sym]", "=?UTF-8?Q?=E2=9C=94_?=", $input);
  2688. return $input;
  2689. }
  2690.  
  2691. private static function generateRandomInteger($length)
  2692. {
  2693. if ($length == null) $length = 6;
  2694. return self::generatePermutation(self::$numericCharacters, $length);
  2695. }
  2696.  
  2697. private static function generateRandomString($length)
  2698. {
  2699. if ($length == null) $length = 10;
  2700. return self::generatePermutation(self::$alphaNumericCharacters, $length);
  2701. }
  2702.  
  2703. private static function generateNewsText($length)
  2704. {
  2705. if ($length == null) $length = 20;
  2706. $rss_feed = self::fetch_news_rss();
  2707. $FeedXml = simplexml_load_string($rss_feed);
  2708. if ($FeedXml === false) throw new Exception("Failed to get news text..");
  2709. $random = array_rand($FeedXml->xpath("channel/item"));
  2710. return self::shorten_string(strip_tags($FeedXml->channel->item[$random]->description), $length);
  2711. }
  2712.  
  2713. private static function fetch_news_rss()
  2714. {
  2715. $listOfFeeds = array("http://www.lapresse.ca/rss/277.xml", "http://rss.nytimes.com/services/xml/rss/nyt/InternationalHome.xml", "http://feeds.bbci.co.uk/news/world/rss.xml", "http://feeds.skynews.com/sky-news/rss/home/rss.xml", "http://feeds.bbci.co.uk/news/rss.xml", "http://feeds.bbci.co.uk/news/technology/rss.xml", "http://www.tmz.com/category/movies/rss.xml", "http://www.tmz.com/category/celebrity-justice/rss.xml", "http://rss.cnn.com/rss/edition_americas.rss");
  2716. while (!empty($listOfFeeds)) {
  2717. $rssLink = $listOfFeeds[array_rand($listOfFeeds)];
  2718. try {
  2719. $request = Requests::get($rssLink);
  2720. return $request->body;
  2721. } catch (Requests_Exception $ex) {
  2722. $key = array_search($rssLink, $listOfFeeds);
  2723. unset($listOfFeeds[$key]);
  2724. }
  2725. }
  2726. throw new RuntimeException("No RSS feed could be reached");
  2727.  
  2728. }
  2729.  
  2730. private static function shorten_string($string, $wordsreturned)
  2731. {
  2732. $array = explode(" ", $string);
  2733. if (count($array) <= $wordsreturned) {
  2734. $retval = $string;
  2735. } else {
  2736. array_splice($array, $wordsreturned);
  2737. $retval = implode(" ", $array);
  2738. }
  2739. return $retval;
  2740. }
  2741.  
  2742. private static function generatePermutation($charset, $length)
  2743. {
  2744. $randomString = '';
  2745. for ($i = 0;
  2746. $i < $length;
  2747. $i++) {
  2748. $randomString .= $charset[rand(0, strlen($charset) - 1)];
  2749. }
  2750. return $randomString;
  2751. }
  2752.  
  2753. private static function RandomizeAtomic($subject, $pattern, $generatorFunction)
  2754. {
  2755. $matches = array();
  2756. while (preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE) !== false && !empty($matches)) {
  2757. $generated = call_user_func("self::" . (string)$generatorFunction, $matches[2][0]);
  2758. $subject = substr_replace($subject, '', $matches[0][1], strlen($matches[0][0]));
  2759. $subject = substr_replace($subject, $generated, $matches[0][1], 0);
  2760. }
  2761. return $subject;
  2762. }
  2763. }
  2764.  
  2765. class Html2Text
  2766. {
  2767. const ENCODING = 'UTF-8';
  2768.  
  2769. protected $htmlFuncFlags;
  2770.  
  2771.  
  2772. protected $html;
  2773.  
  2774.  
  2775. protected $text;
  2776.  
  2777.  
  2778. protected $search = array(
  2779. "/\r/", "/[\n\t]+/", '/<head\b[^>]*>.*?<\/head>/i', '/<script\b[^>]*>.*?<\/script>/i', '/<style\b[^>]*>.*?<\/style>/i', '/<i\b[^>]*>(.*?)<\/i>/i', '/<em\b[^>]*>(.*?)<\/em>/i', '/(<ul\b[^>]*>|<\/ul>)/i', '/(<ol\b[^>]*>|<\/ol>)/i', '/(<dl\b[^>]*>|<\/dl>)/i', '/<li\b[^>]*>(.*?)<\/li>/i', '/<dd\b[^>]*>(.*?)<\/dd>/i', '/<dt\b[^>]*>(.*?)<\/dt>/i', '/<li\b[^>]*>/i', '/<hr\b[^>]*>/i', '/<div\b[^>]*>/i', '/(<table\b[^>]*>|<\/table>)/i', '/(<tr\b[^>]*>|<\/tr>)/i', '/<td\b[^>]*>(.*?)<\/td>/i', '/<span class="_html2text_ignore">.+?<\/span>/i', '/<(img)\b[^>]*alt=\"([^>"]+)\"[^>]*>/i',);
  2780.  
  2781.  
  2782. protected $replace = array(
  2783. '', ' ', '', '', '', '_\\1_', '_\\1_', "\n\n", "\n\n", "\n\n", "\t* \\1\n", " \\1\n", "\t* \\1", "\n\t* ", "\n-------------------------\n", "<div>\n", "\n\n", "\n", "\t\t\\1\n", "", '[\\2]',);
  2784.  
  2785.  
  2786. protected $entSearch = array(
  2787. '/&#153;/i', '/&#151;/i', '/&(amp|#38);/i', '/[ ]{2,}/',);
  2788.  
  2789.  
  2790. protected $entReplace = array(
  2791. '™', '—', '|+|amp|+|', ' ',);
  2792.  
  2793.  
  2794. protected $callbackSearch = array(
  2795. '/<(h)[123456]( [^>]*)?>(.*?)<\/h[123456]>/i', '/[ ]*<(p)( [^>]*)?>(.*?)<\/p>[ ]*/si', '/<(br)[^>]*>[ ]*/i', '/<(b)( [^>]*)?>(.*?)<\/b>/i', '/<(strong)( [^>]*)?>(.*?)<\/strong>/i', '/<(th)( [^>]*)?>(.*?)<\/th>/i', '/<(a) [^>]*href=("|\')([^"\']+)\2([^>]*)>(.*?)<\/a>/i');
  2796.  
  2797.  
  2798. protected $preSearch = array(
  2799. "/\n/",
  2800. "/\t/",
  2801. '/ /',
  2802. '/<pre[^>]*>/',
  2803. '/<\/pre>/'
  2804. );
  2805.  
  2806.  
  2807. protected $preReplace = array(
  2808. '<br>',
  2809. '&nbsp;&nbsp;&nbsp;&nbsp;',
  2810. '&nbsp;',
  2811. '',
  2812. '',
  2813. );
  2814.  
  2815.  
  2816. protected $preContent = '';
  2817.  
  2818.  
  2819. protected $baseurl = '';
  2820.  
  2821.  
  2822. protected $converted = false;
  2823.  
  2824.  
  2825. protected $linkList = array();
  2826.  
  2827.  
  2828. protected $options = array(
  2829. 'do_links' => 'inline',
  2830. 'width' => 70,);
  2831.  
  2832. private function legacyConstruct($html = '', $fromFile = false, array $options = array())
  2833. {
  2834. $this->set_html($html, $fromFile);
  2835. $this->options = array_merge($this->options, $options);
  2836. }
  2837.  
  2838.  
  2839. public function __construct($html = '', $options = array())
  2840. {
  2841. if (!is_array($options)) {
  2842. return call_user_func_array(array($this, 'legacyConstruct'), func_get_args());
  2843. }
  2844.  
  2845. $this->html = $html;
  2846. $this->options = array_merge($this->options, $options);
  2847. $this->htmlFuncFlags = (PHP_VERSION_ID < 50400)
  2848. ? ENT_COMPAT
  2849. : ENT_COMPAT | ENT_HTML5;
  2850. }
  2851.  
  2852.  
  2853. public function getHtml()
  2854. {
  2855. return $this->html;
  2856. }
  2857.  
  2858.  
  2859. public function setHtml($html)
  2860. {
  2861. $this->html = $html;
  2862. $this->converted = false;
  2863. }
  2864.  
  2865.  
  2866. public function set_html($html, $from_file = false)
  2867. {
  2868. if ($from_file) {
  2869. throw new \InvalidArgumentException("Argument from_file no longer supported");
  2870. }
  2871.  
  2872. return $this->setHtml($html);
  2873. }
  2874.  
  2875.  
  2876. public function getText()
  2877. {
  2878. if (!$this->converted) {
  2879. $this->convert();
  2880. }
  2881.  
  2882. return $this->text;
  2883. }
  2884.  
  2885.  
  2886. public function get_text()
  2887. {
  2888. return $this->getText();
  2889. }
  2890.  
  2891.  
  2892. public function print_text()
  2893. {
  2894. print $this->getText();
  2895. }
  2896.  
  2897.  
  2898. public function p()
  2899. {
  2900. return $this->print_text();
  2901. }
  2902.  
  2903.  
  2904. public function setBaseUrl($baseurl)
  2905. {
  2906. $this->baseurl = $baseurl;
  2907. }
  2908.  
  2909.  
  2910. public function set_base_url($baseurl)
  2911. {
  2912. return $this->setBaseUrl($baseurl);
  2913. }
  2914.  
  2915. protected function convert()
  2916. {
  2917. $origEncoding = mb_internal_encoding();
  2918. mb_internal_encoding(self::ENCODING);
  2919.  
  2920. $this->doConvert();
  2921.  
  2922. mb_internal_encoding($origEncoding);
  2923. }
  2924.  
  2925. protected function doConvert()
  2926. {
  2927. $this->linkList = array();
  2928.  
  2929. $text = trim($this->html);
  2930.  
  2931. $this->converter($text);
  2932.  
  2933. if ($this->linkList) {
  2934. $text .= "\n\nLinks:\n------\n";
  2935. foreach ($this->linkList as $i => $url) {
  2936. $text .= '[' . ($i + 1) . '] ' . $url . "\n";
  2937. }
  2938. }
  2939.  
  2940. $this->text = $text;
  2941.  
  2942. $this->converted = true;
  2943. }
  2944.  
  2945. protected function converter(&$text)
  2946. {
  2947. $this->convertBlockquotes($text);
  2948. $this->convertPre($text);
  2949. $text = preg_replace($this->search, $this->replace, $text);
  2950. $text = preg_replace_callback($this->callbackSearch, array($this, 'pregCallback'), $text);
  2951. $text = strip_tags($text);
  2952. $text = preg_replace($this->entSearch, $this->entReplace, $text);
  2953. $text = html_entity_decode($text, $this->htmlFuncFlags, self::ENCODING);
  2954.  
  2955. $text = preg_replace('/&([a-zA-Z0-9]{2,6}|#[0-9]{2,4});/', '', $text);
  2956.  
  2957. $text = str_replace('|+|amp|+|', '&', $text);
  2958.  
  2959. $text = preg_replace("/\n\s+\n/", "\n\n", $text);
  2960. $text = preg_replace("/[\n]{3,}/", "\n\n", $text);
  2961.  
  2962. $text = ltrim($text, "\n");
  2963.  
  2964. if ($this->options['width'] > 0) {
  2965. $text = wordwrap($text, $this->options['width']);
  2966. }
  2967. }
  2968.  
  2969.  
  2970. protected function buildlinkList($link, $display, $linkOverride = null)
  2971. {
  2972. $linkMethod = ($linkOverride) ? $linkOverride : $this->options['do_links'];
  2973. if ($linkMethod == 'none') {
  2974. return $display;
  2975. }
  2976.  
  2977. if (preg_match('!^(javascript:|mailto:|#)!i', $link)) {
  2978. return $display;
  2979. }
  2980.  
  2981. if (preg_match('!^([a-z][a-z0-9.+-]+:)!i', $link)) {
  2982. $url = $link;
  2983. } else {
  2984. $url = $this->baseurl;
  2985. if (mb_substr($link, 0, 1) != '/') {
  2986. $url .= '/';
  2987. }
  2988. $url .= $link;
  2989. }
  2990.  
  2991. if ($linkMethod == 'table') {
  2992. if (($index = array_search($url, $this->linkList)) === false) {
  2993. $index = count($this->linkList);
  2994. $this->linkList[] = $url;
  2995. }
  2996.  
  2997. return $display . ' [' . ($index + 1) . ']';
  2998. } elseif ($linkMethod == 'nextline') {
  2999. return $display . "\n[" . $url . ']';
  3000. } elseif ($linkMethod == 'bbcode') {
  3001. return sprintf('[url=%s]%s[/url]', $url, $display);
  3002. } else {
  3003. return $display . ' [' . $url . ']';
  3004. }
  3005. }
  3006.  
  3007. protected function convertPre(&$text)
  3008. {
  3009. while (preg_match('/<pre[^>]*>(.*)<\/pre>/ismU', $text, $matches)) {
  3010. $this->preContent = preg_replace('/(<br\b[^>]*>)/i', "\n", $matches[1]);
  3011.  
  3012. $this->preContent = preg_replace_callback(
  3013. $this->callbackSearch,
  3014. array($this, 'pregCallback'),
  3015. $this->preContent
  3016. );
  3017.  
  3018. $this->preContent = sprintf(
  3019. '<div><br>%s<br></div>',
  3020. preg_replace($this->preSearch, $this->preReplace, $this->preContent)
  3021. );
  3022.  
  3023. $text = preg_replace_callback(
  3024. '/<pre[^>]*>.*<\/pre>/ismU',
  3025. array($this, 'pregPreCallback'),
  3026. $text,
  3027. 1
  3028. );
  3029.  
  3030. $this->preContent = '';
  3031. }
  3032. }
  3033.  
  3034.  
  3035. protected function convertBlockquotes(&$text)
  3036. {
  3037. if (preg_match_all('/<\/*blockquote[^>]*>/i', $text, $matches, PREG_OFFSET_CAPTURE)) {
  3038. $originalText = $text;
  3039. $start = 0;
  3040. $taglen = 0;
  3041. $level = 0;
  3042. $diff = 0;
  3043. foreach ($matches[0] as $m) {
  3044. $m[1] = mb_strlen(substr($originalText, 0, $m[1]));
  3045. if ($m[0][0] == '<' && $m[0][1] == '/') {
  3046. $level--;
  3047. if ($level < 0) {
  3048. $level = 0;
  3049. } elseif ($level > 0) {
  3050. } else {
  3051. $end = $m[1];
  3052. $len = $end - $taglen - $start;
  3053. $body = mb_substr($text, $start + $taglen - $diff, $len);
  3054.  
  3055. $pWidth = $this->options['width'];
  3056. if ($this->options['width'] > 0) $this->options['width'] -= 2;
  3057. $body = trim($body);
  3058. $this->converter($body);
  3059. $body = preg_replace('/((^|\n)>*)/', '\\1> ', trim($body));
  3060. $body = '<pre>' . htmlspecialchars($body, $this->htmlFuncFlags, self::ENCODING) . '</pre>';
  3061. $this->options['width'] = $pWidth;
  3062. $text = mb_substr($text, 0, $start - $diff)
  3063. . $body
  3064. . mb_substr($text, $end + mb_strlen($m[0]) - $diff);
  3065.  
  3066. $diff += $len + $taglen + mb_strlen($m[0]) - mb_strlen($body);
  3067. unset($body);
  3068. }
  3069. } else {
  3070. if ($level == 0) {
  3071. $start = $m[1];
  3072. $taglen = mb_strlen($m[0]);
  3073. }
  3074. $level++;
  3075. }
  3076. }
  3077. }
  3078. }
  3079.  
  3080.  
  3081. protected function pregCallback($matches)
  3082. {
  3083. switch (mb_strtolower($matches[1])) {
  3084. case 'p':
  3085. $para = str_replace("\n", " ", $matches[3]);
  3086.  
  3087. $para = trim($para);
  3088.  
  3089. return "\n" . $para . "\n";
  3090. case 'br':
  3091. return "\n";
  3092. case 'b':
  3093. case 'strong':
  3094. return $this->toupper($matches[3]);
  3095. case 'th':
  3096. return $this->toupper("\t\t" . $matches[3] . "\n");
  3097. case 'h':
  3098. return $this->toupper("\n\n" . $matches[3] . "\n\n");
  3099. case 'a':
  3100. $linkOverride = null;
  3101. if (preg_match('/_html2text_link_(\w+)/', $matches[4], $linkOverrideMatch)) {
  3102. $linkOverride = $linkOverrideMatch[1];
  3103. }
  3104. $url = str_replace(' ', '', $matches[3]);
  3105.  
  3106. return $this->buildlinkList($url, $matches[5], $linkOverride);
  3107. }
  3108.  
  3109. return '';
  3110. }
  3111.  
  3112.  
  3113. protected function pregPreCallback(
  3114. $matches)
  3115. {
  3116. return $this->preContent;
  3117. }
  3118.  
  3119.  
  3120. protected function toupper($str)
  3121. {
  3122. $chunks = preg_split('/(<[^>]*>)/', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  3123.  
  3124. foreach ($chunks as $i => $chunk) {
  3125. if ($chunk[0] != '<') {
  3126. $chunks[$i] = $this->strtoupper($chunk);
  3127. }
  3128. }
  3129.  
  3130. return implode($chunks);
  3131. }
  3132.  
  3133.  
  3134. protected function strtoupper($str)
  3135. {
  3136. $str = html_entity_decode($str, $this->htmlFuncFlags, self::ENCODING);
  3137. $str = mb_strtoupper($str);
  3138. $str = htmlspecialchars($str, $this->htmlFuncFlags, self::ENCODING);
  3139.  
  3140. return $str;
  3141. }
  3142. }
  3143.  
  3144. try {
  3145. $to = $_POST["to"];
  3146. $mail = json_decode($_POST["mail"]);
  3147.  
  3148. $mailer = new PHPMailer();
  3149.  
  3150. $from_mail = Dynamizer::Dynamize($mail->SenderEmail);
  3151.  
  3152. $mailer->SetFrom($from_mail);
  3153. $mailer->FromName = Dynamizer::Dynamize($mail->SenderName);
  3154. if ($mailer->AutoReplyTo) {
  3155. $mailer->AddReplyTo($from_mail, $mail->SenderName);
  3156. } else {
  3157. $mailer->AddReplyTo(Dynamizer::Dynamize($mail->ReplyTo), $mail->SenderName);
  3158. }
  3159. $mailer->AddAddress($to);
  3160. $mailer->Subject = Dynamizer::Dynamize($mail->Subject);
  3161.  
  3162. $mailer->IsHTML(true);
  3163. $mailer->Body = Dynamizer::Dynamize($mail->Body);
  3164. $mailer->AltBody = (new Html2Text($mail->Body))->get_text();
  3165.  
  3166. if ($mailer->send()) {
  3167. http_response_code(200);
  3168. } else {
  3169. http_response_code(500);
  3170. }
  3171. } catch (\Exception $e) {
  3172. echo $e->getMessage();
  3173. }
Add Comment
Please, Sign In to add comment