[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/scripts/mail/ -> mail_handler.php (source)

   1  #!/usr/bin/env php
   2  <?php
   3  
   4  // NOTE: This script is very oldschool and takes the environment as an argument.
   5  // Some day, we could take a shot at cleaning this up.
   6  if ($argc > 1) {
   7    foreach (array_slice($argv, 1) as $arg) {
   8      if (!preg_match('/^-/', $arg)) {
   9        $_SERVER['PHABRICATOR_ENV'] = $arg;
  10        break;
  11      }
  12    }
  13  }
  14  
  15  $root = dirname(dirname(dirname(__FILE__)));
  16  require_once $root.'/scripts/__init_script__.php';
  17  require_once $root.'/externals/mimemailparser/MimeMailParser.class.php';
  18  
  19  $args = new PhutilArgumentParser($argv);
  20  $args->parseStandardArguments();
  21  $args->parse(
  22    array(
  23      array(
  24        'name' => 'process-duplicates',
  25        'help' => pht(
  26          "Process this message, even if it's a duplicate of another message. ".
  27          "This is mostly useful when debugging issues with mail routing."),
  28      ),
  29      array(
  30        'name' => 'env',
  31        'wildcard' => true,
  32      ),
  33    ));
  34  
  35  $parser = new MimeMailParser();
  36  $parser->setText(file_get_contents('php://stdin'));
  37  
  38  $text_body = $parser->getMessageBody('text');
  39  
  40  $text_body_headers = $parser->getMessageBodyHeaders('text');
  41  $content_type = idx($text_body_headers, 'content-type');
  42  if (
  43    !phutil_is_utf8($text_body) &&
  44    (preg_match('/charset="(.*?)"/', $content_type, $matches) ||
  45     preg_match('/charset=(\S+)/', $content_type, $matches))
  46  ) {
  47    $text_body = phutil_utf8_convert($text_body, 'UTF-8', $matches[1]);
  48  }
  49  
  50  $headers = $parser->getHeaders();
  51  $headers['subject'] = iconv_mime_decode($headers['subject'], 0, 'UTF-8');
  52  $headers['from'] = iconv_mime_decode($headers['from'], 0, 'UTF-8');
  53  
  54  if ($args->getArg('process-duplicates')) {
  55    $headers['message-id'] = Filesystem::readRandomCharacters(64);
  56  }
  57  
  58  $received = new PhabricatorMetaMTAReceivedMail();
  59  $received->setHeaders($headers);
  60  $received->setBodies(array(
  61    'text' => $text_body,
  62    'html' => $parser->getMessageBody('html'),
  63  ));
  64  
  65  $attachments = array();
  66  foreach ($parser->getAttachments() as $attachment) {
  67    if (preg_match('@text/(plain|html)@', $attachment->getContentType()) &&
  68        $attachment->getContentDisposition() == 'inline') {
  69      // If this is an "inline" attachment with some sort of text content-type,
  70      // do not treat it as a file for attachment. MimeMailParser already picked
  71      // it up in the getMessageBody() call above. We still want to treat 'inline'
  72      // attachments with other content types (e.g., images) as attachments.
  73      continue;
  74    }
  75  
  76    $file = PhabricatorFile::newFromFileData(
  77      $attachment->getContent(),
  78      array(
  79        'name' => $attachment->getFilename(),
  80      ));
  81    $attachments[] = $file->getPHID();
  82  }
  83  
  84  try {
  85    $received->setAttachments($attachments);
  86    $received->save();
  87    $received->processReceivedMail();
  88  } catch (Exception $e) {
  89    $received
  90      ->setMessage('EXCEPTION: '.$e->getMessage())
  91      ->save();
  92  
  93    throw $e;
  94  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1