title: PHP: Memory Optimizing Inbound Mail
body: <p>In my PHP application, I receive mails using <a href="http://postmarkapp.com" rel="nofollow">Postmarks's</a> inbound hook. This service receives the mail and sends it JSON encoded to a URL on my server, which works fine.</p>
  
  <p>The issue I have is, when the mail has attachments with more than 10MB.</p>
  
  <p>Which results in</p>
  
  <blockquote>
    <p>PHP Fatal error:  Allowed memory size of 104857600 bytes exhausted
    (tried to allocate 1821693 bytes)</p>
  </blockquote>
  
  <p>What I'm doing in this line is:</p>
  
  <pre><code>$in = json_decode(file_get_contents("php://input"));
  </code></pre>
  
  <p>I have two questions:</p>
  
  <ol>
  <li>Is there a way to do this more memory efficient?</li>
  <li>Why is it failing for 10MB mails, where the memory limit is actually 100MB? Does Base64 + JSON encoding produce an overhead which is 10 times bigger than the original size?</li>
  </ol>
  
  <p><strong>Edit after debuging with memory_get_usage()</strong>:</p>
  
  <pre><code>Script start
  47MB memory usage.
  $in = file_get_contents("php://input");
  63MB memory usage.
  json_decode($in);
  PHP terminates, due to memory size exhausted.
  </code></pre>
  
  <p>It is interesting, that the script already starts with a memory usage of 47MB, without issuing any command. I guess this is due to the large input data? Maybe because PHP stores it in $HTTP_RAW_POST_DATA?</p>
  
  <p>Is there any php.ini directive I could use, to let PHP create less variables?</p>