Guest User

Untitled

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