Fork of FusionPBX but with LDAP kinda working
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
5.5 KiB

2 years ago
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <markjcrane@fusionpbx.com>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2022
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <markjcrane@fusionpbx.com>
  20. Luis Daniel Lucio Quiroz <dlucio@okay.com.mx>
  21. */
  22. //set the include path
  23. if (defined('STDIN')) {
  24. $document_root = str_replace("\\", "/", $_SERVER["PHP_SELF"]);
  25. preg_match("/^(.*)\/secure\/.*$/", $document_root, $matches);
  26. $document_root = $matches[1];
  27. set_include_path($document_root);
  28. $_SERVER["DOCUMENT_ROOT"] = $document_root;
  29. }
  30. //includes
  31. if (!defined('STDIN')) { include_once "root.php"; }
  32. require_once "resources/require.php";
  33. //define a function to remove html tags
  34. if (!function_exists('remove_tags')) {
  35. function remove_tags($string) {
  36. //remove HTML tags
  37. $string = preg_replace ('/<[^>]*>/', ' ', $string);
  38. //remove control characters
  39. $string = str_replace("\r", '', $string); // --- replace with empty space
  40. $string = str_replace("\n", ' ', $string); // --- replace with space
  41. $string = str_replace("\t", ' ', $string); // --- replace with space
  42. //remove multiple spaces
  43. $string = trim(preg_replace('/ {2,}/', ' ', $string));
  44. return $string;
  45. }
  46. }
  47. //set init settings
  48. ini_set('max_execution_time',1800); //30 minutes
  49. ini_set('memory_limit', '512M');
  50. //listen for standard input
  51. if ($msg == '') {
  52. $fd = fopen("php://stdin", "r");
  53. $msg = file_get_contents ("php://stdin");
  54. fclose($fd);
  55. }
  56. //save output to
  57. $fp = fopen(sys_get_temp_dir()."/mailer-app.log", "a");
  58. //prepare the output buffers
  59. ob_end_clean();
  60. ob_start();
  61. //message divider for log file
  62. echo "\n\n======================================================================================================================================================================================\n\n";
  63. //testing show the raw email
  64. //echo "Message: \n".$msg."\n";
  65. //includes
  66. //require_once('resources/pop3/mime_parser.php');
  67. //require_once('resources/pop3/rfc822_addresses.php');
  68. //if (file_exists($_SERVER["PROJECT_ROOT"]."/app/emails/email_transcription.php")) {
  69. // require_once($_SERVER["PROJECT_ROOT"]."/app/emails/email_transcription.php");
  70. //}
  71. //parse the email
  72. $email = new email;
  73. $email->parse($msg);
  74. $email->debug_level = 0;
  75. $email->method = 'direct';
  76. $headers = $email->headers;
  77. $sent = $email->send();
  78. //debug information
  79. /*
  80. echo "<pre>\n";
  81. echo "recipients ".print_r($email->recipients)."\n";
  82. echo "recipients ".$email->recipients."\n";
  83. echo "subject ".$email->subject."\n";
  84. echo "body ".$email->body."\n";
  85. echo "from_address ".$email->from_address."\n";
  86. echo "from_name ".$email->from_name."\n";
  87. echo "headers ".print_r($email->headers)."\n";
  88. //echo "attachments ".print_r($email->attachments)."\n";
  89. echo "</pre>\n";
  90. exit;
  91. */
  92. //send the email
  93. if (!$sent) {
  94. echo "Mailer Error: ".$email_error."\n\n";
  95. $call_uuid = $headers["X-FusionPBX-Call-UUID"];
  96. if ($resend == true) {
  97. echo "Retained in v_email_logs \n";
  98. }
  99. else {
  100. //log the message in database for review
  101. if (!isset($email_log_uuid)) {
  102. //build insert array
  103. $email_log_uuid = uuid();
  104. $array['email_logs'][0]['email_log_uuid'] = $email_log_uuid;
  105. if (is_uuid($call_uuid)) {
  106. $array['email_logs'][0]['call_uuid'] = $call_uuid;
  107. }
  108. if (isset($headers["X-FusionPBX-Domain-UUID"])) {
  109. $array['email_logs'][0]['domain_uuid'] = $headers["X-FusionPBX-Domain-UUID"];
  110. }
  111. $array['email_logs'][0]['sent_date'] = 'now()';
  112. if (isset($headers["X-FusionPBX-Email-Type"])) {
  113. $array['email_logs'][0]['type'] = $headers["X-FusionPBX-Email-Type"];
  114. }
  115. $array['email_logs'][0]['status'] = 'failed';
  116. $array['email_logs'][0]['email'] = str_replace("'", "''", $msg);
  117. //grant temporary permissions
  118. $p = new permissions;
  119. $p->add('email_log_add', 'temp');
  120. //execute insert
  121. $database = new database;
  122. $database->app_name = 'v_mailto';
  123. $database->app_uuid = 'ba41954e-9d21-4b10-bbc2-fa5ceabeb184';
  124. $database->save($array);
  125. unset($array);
  126. //revoke temporary permissions
  127. $p->delete('email_log_add', 'temp');
  128. }
  129. echo "Retained in v_email_logs as email_log_uuid = ".$email_log_uuid."\n";
  130. }
  131. }
  132. else {
  133. echo "Message sent!";
  134. }
  135. //get and save the output from the buffer
  136. $content = ob_get_contents(); //get the output from the buffer
  137. $content = str_replace("<br />", "", $content);
  138. ob_end_clean(); //clean the buffer
  139. fwrite($fp, $content);
  140. fclose($fp);
  141. /*
  142. //save in /tmp as eml file
  143. $fp = fopen(sys_get_temp_dir()."/email.eml", "w");
  144. ob_end_clean();
  145. ob_start();
  146. $sql = "select email from v_email_logs where email_log_uuid = :email_log_uuid ";
  147. $parameters['email_log_uuid'] = $email_log_uuid;
  148. $database = new database;
  149. $email = $database->select($sql, $parameters, 'column');
  150. echo $email;
  151. unset($sql, $parameters, $email);
  152. $content = ob_get_contents(); //get the output from the buffer
  153. $content = str_replace("<br />", "", $content);
  154. ob_end_clean(); //clean the buffer
  155. fwrite($fp, $content);
  156. fclose($fp);
  157. */
  158. ?>