title: CakePHP 4 Mail with PDF attachment
body: <p>I am trying to attach a PDF file to the CakePHP Mailer function by using the export function that I made. However not any of the solutions I came up with seem to work. Please have a look at my code.</p>
  <pre><code>public function exportOrder($orderId = null) {      
      if ($orderId != null) {
          $order = $this-&gt;Orders-&gt;findById($orderId)-&gt;contain(['Suppliers'])-&gt;first();
          $order-&gt;products = unserialize($order-&gt;products);
  
          $data = [];
          $data['order'] = $order;
          $data['route'] = 'coreSections/orderCoreSection/pdf/order';
  
          $this-&gt;set(compact('data'));
  
          /* Render PDF */
          $this-&gt;viewBuilder()-&gt;setClassName('CakePdf.Pdf');
  
          $this-&gt;viewBuilder()-&gt;setOption(
              'pdfConfig',
              [
                  'filename' =&gt; $data['order']['receipt_name'] . '.pdf',
                  'download' =&gt; false
              ]
          );
  
          return $this-&gt;render('/Element/coreSections/orderCoreSection/pdf/order');
      }
  }
  </code></pre>
  <p>First I create a instance of a PDF, which works for a direct export, by calling on the above function and store it. Secondly I try the send the mail with the rendered pdf as attachemnt, this gives me the error saying: <em>File must be a filepath or UploadedFileInterface instance. Found object instead</em>. The format the PDF is stored in is 'application/pdf'as indicated by a debug. This error, which is created by the mail function, is shown below:</p>
  <pre><code>$pdf = $this-&gt;exportOrder(6);
  
      debug($pdf);
  
      $email = new Mailer();
      $email-&gt;setTransport('mail');
      $email-&gt;setFrom(['mail@mail.com' =&gt; 'website.com'])
          -&gt;setTo('mail@mail.com')
          -&gt;setSubject('Test')
          -&gt;setAttachments([
              'test.pdf' =&gt;[
                  'file' =&gt; $pdf,
                  'mimetype' =&gt; 'MIME-Version: 1.0'
              ]
          ])
          -&gt;deliver('Test');
  </code></pre>
  <p>What I would like to know if is possible to send a mail with the current object or how I could possibly save it as a file so I can use that instead as an attachment?</p>
  <p>Other suggestions are also welcome!</p>
  <p>Thanks in advace</p>