PageRenderTime 17ms CodeModel.GetById 11ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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