/modules/sms_email_gateway/mta_scripts/mta_process_email.php

https://gitlab.com/vozmob/smsframework · PHP · 175 lines · 67 code · 22 blank · 86 comment · 13 complexity · 404a47a84bddd3e3f5eede9f07f14f72 MD5 · raw file

  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * E-mail processing script for the SMS email gateway module.
  5. *
  6. * This script processes an incoming email message, determines the appropriate
  7. * address code and identifier for the message, and sends the information to
  8. * the configured Drupal site via XML-RPC.
  9. *
  10. * MAIL SERVER SETUP:
  11. *
  12. * 1. Place the entire 'mta_scripts' directory from the sms_email_gateway
  13. * module somewhere on the mail server, where the mail server has
  14. * read/execute access.
  15. *
  16. * 2. Set up PHP on the mail server.
  17. *
  18. * 3. Make sure the first line of the mta_process_email.php script points to
  19. * the location of the PHP executable on the server, and that the script is
  20. * executable.
  21. *
  22. * 2. Configure the mail server to send the necessary addresses to the script
  23. * for processing.
  24. *
  25. * EXAMPLE:
  26. *
  27. * This example is for Postfix. For other MTA's, you're on your own... ;)
  28. *
  29. * To pipe any messages matching the regex /^ex\+.*?@example\.com$/ to the
  30. * email gateway script:
  31. *
  32. * a. Create a PCRE map file with the following line:
  33. * /^ex\+.*?@example\.com$/ ex
  34. *
  35. * Don't forget to add the PCRE map file to the virtual_alias_maps
  36. * parameter in main.cf. If the file is /etc/postfix/virtual_pcre,
  37. * then 'virtual_alias_maps = pcre:/etc/postfix/virtual_pcre' would
  38. * work.
  39. *
  40. * See http://www.postfix.org/virtual.5.html and
  41. * http://www.postfix.org/pcre_table.5.html for help configuring a
  42. * PCRE map file.
  43. *
  44. * b. Add the following to /etc/aliases:
  45. * ex: |/full/path/to/mta_process_email.php
  46. *
  47. * c. Run newaliases to load the new alias.
  48. *
  49. * 3. In the configuration section of mta_process_email.php, configure the
  50. * Drupal site to send the results to.
  51. *
  52. * It's totally possible to have one mail server processing messages for
  53. * multiple Drupal sites, by making different copies of the
  54. * mta_process_email.php script, and mapping the various emails to the different
  55. * scripts.
  56. */
  57. /**
  58. * BEGIN CONFIGURATION.
  59. */
  60. // The URL of the Drupal site, no trailing slash -- ex. www.example.com
  61. // If you set this parameter to an empty string, then the domain that the email
  62. // is sent to will be used.
  63. define('SITE_URL', '');
  64. // For sites that use SSL, set this to TRUE.
  65. define('USE_SSL', FALSE);
  66. // Set this if you need to send to a custom server port (defaults to port 80).
  67. define('SERVER_PORT', '');
  68. // Set this if you need to send custom query string (useful for debugging).
  69. define('SERVER_QUERY_STRING', '');
  70. // Set to TRUE to turn on script debugging.
  71. define('DEBUG', FALSE);
  72. // File to dump debug info to.
  73. define('DEBUG_FILE', '/tmp/mta_process_email.debug');
  74. // These should not need to be adjusted if you followed the installation
  75. // instructions.
  76. require_once('./xmlrpc.inc');
  77. require_once('./support.inc');
  78. /**
  79. * END CONFIGURATION.
  80. */
  81. // Read e-mail from stdin.
  82. $fd = fopen('php://stdin', 'r');
  83. $email = '';
  84. while (!feof($fd)) {
  85. $email .= fread($fd, 1024);
  86. }
  87. fclose($fd);
  88. if (SITE_URL) {
  89. $domain = SITE_URL;
  90. }
  91. // No site URL defined, so look for one in the headers.
  92. else {
  93. // Break up the lines.
  94. $lines = explode("\n", $email);
  95. // Loop through the email one line at a time. As soon as we find the To: line,
  96. // or the headers end, then stop looking.
  97. for ($i = 0; $i < count($lines); $i++) {
  98. if (preg_match('/^To:(.*)/', $lines[$i], $matches)) {
  99. // For combo name/email addresses, parse out the email address.
  100. $to = parse_email(trim($matches[1]));
  101. $to = explode('@', $to);
  102. if (!empty($to[1])) {
  103. $domain = $to[1];
  104. }
  105. break;
  106. }
  107. // Empty line, header section has ended.
  108. elseif (trim($lines[$i]) == '') {
  109. break;
  110. }
  111. }
  112. }
  113. if (!empty($domain)) {
  114. // Compose URL.
  115. $transport = USE_SSL ? 'https://' : 'http://';
  116. $port = SERVER_PORT ? ':' . SERVER_PORT : '';
  117. $query = SERVER_QUERY_STRING ? '?' . SERVER_QUERY_STRING : '';
  118. $url = $transport . $domain . $port . "/xmlrpc.php" . $query;
  119. $result = xmlrpc($url, 'emailGateway.send', $email);
  120. }
  121. // Debugging section.
  122. if (DEBUG) {
  123. $debug = "*****************************************************************\n";
  124. if ($result === 0) {
  125. $debug .= "Message delivered.\n";
  126. }
  127. else {
  128. $xml_return_output = print_r($result, TRUE);
  129. $debug .= "XML-RPC Error -- return value follows:\n[start return value]$xml_return_output\n[end return value]\n";
  130. }
  131. $debug .= "URL: $url\n";
  132. $debug .= "Full message:\n$email\n\n\n";
  133. $fh = fopen(DEBUG_FILE, 'a');
  134. fwrite($fh, $debug);
  135. fclose($fh);
  136. }
  137. return $result;
  138. /**
  139. * Parses out email address from addresses with human-readable names.
  140. *
  141. * @param $full_address The full email address.
  142. * @return The email address.
  143. */
  144. function parse_email($full_address) {
  145. if ($left = strpos($full_address, '<')) {
  146. $right = strpos($full_address, '>');
  147. $address = substr($full_address, $left + 1, $right - $left - 1);
  148. }
  149. else {
  150. $address = $full_address;
  151. }
  152. return $address;
  153. }