title: Sending mail large attachment - ZF2
body: <p>i just ran into a problem, when sending mails with attachments larger than about 2.5Mb from a server. Sending emails with smaller attachments work, but as soon the critical size of about 2 or 2.5Mb is reached, the mail is not send anymore.</p>
  
  <p>The PDF files and merged target PDF are created without problem, no matter of the size. But only smaller PDF files are send by mail. Not even an empty mail is send, when the attachments are too large.</p>
  
  <p>The process is a follows:</p>
  
  <p>1) The php script creates several PDF files.</p>
  
  <p>2) Those files are merged through gs</p>
  
  <pre><code>    $finCmd = 'gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile='.$pathDest.$pdfFilename.' input1.pdf input2.pdf input3.pdf';
  
      // Create PDF
      $execResult = exec($finCmd);
  </code></pre>
  
  <p>3) The email body is created</p>
  
  <pre><code>protected function setBodyHtmlpart($content, $pdfFilepath = null, $pdfFilename = null) {
  
          $content="&lt;p&gt;&lt;span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\";color:black;'&gt;".$content.'&lt;/span&gt;&lt;/p&gt;';
  
          $html = new MimePart($content.$this-&gt;getSignature());
          $html-&gt;type = "text/html";
  
          $body = new MimeMessage();
  
          if ($pdfFilename != '') {
              $pdfAttach = new MimePart(file_get_contents($pdfFilepath.$pdfFilename));
              $pdfAttach-&gt;type = 'application/pdf';
              $pdfAttach-&gt;filename = $pdfFilename;
              $pdfAttach-&gt;encoding = \Zend\Mime\Mime::ENCODING_BASE64;
              $pdfAttach-&gt;disposition = \Zend\Mime\Mime::DISPOSITION_ATTACHMENT;
              $body-&gt;setParts(array($html, $pdfAttach));
          } else {
              $body-&gt;setParts(array($html));
          }
          return $body;
      }
  </code></pre>
  
  <p>4) The email is send with:</p>
  
  <pre><code>protected function send($fromAddress, $fromName, $toAddress, $toName, $subject, $bodyParts)
      {
          // setup SMTP options
          $options = new SmtpOptions(array(
              'name'              =&gt; 'XServer',
              'host'              =&gt; 'xServer',
              'port'              =&gt; 25,
              'connection_class'  =&gt; 'plain',
              'connection_config' =&gt; array(
                  'username' =&gt; 'Xusername',
                  'password' =&gt; 'Xpassword',
              ),
          ));
  
          $mail = new Message();
          $mail-&gt;setBody($bodyParts);
          $mail-&gt;setFrom($fromAddress, $fromName);
          $mail-&gt;setTo($toAddress, $toName);
          $mail-&gt;setSubject($subject);
  
          $transport = new SmtpTransport($options);
          $transport-&gt;send($mail);
      }
  </code></pre>
  
  <p>Any hints are welcome, as i am totaly lost.</p>
  
  <p>I thought there could be race problem: exec is not finished, but script already tries to send the mail and cancels. But i than would at least receive an empty email.</p>
  
  <p>Edit:
  Changing then Mime\Mime::ENCODING_BASE64 delivers the mails, but PDF files are corrupted.</p>