I’m working on a PHP application that receives webhook data from Mailgun when emails arrive. I need help figuring out how to properly extract file attachments from the incoming POST request and save them to my server.
Here’s what the attachment data structure looks like in the webhook payload:
$fileData1 = [
'filename' => 'document.pdf',
'type' => 'application/pdf',
'name' => 'file-1',
'tempfile' => '/tmp/MailgunUpload20240315-1-abc123',
'headers' => 'Content-Disposition: form-data; name="file-1"; filename="document.pdf"\r\nContent-Type: application/pdf\r\nContent-Length: 4521\r\n'
];
$fileData2 = [
'filename' => 'notes_текст.txt',
'type' => 'text/plain',
'name' => 'file-2',
'tempfile' => '/tmp/MailgunUpload20240315-1-xyz789',
'headers' => 'Content-Disposition: form-data; name="file-2"; filename="notes_текст.txt"\r\nContent-Type: text/plain\r\nContent-Length: 156\r\n'
];
$contentType = 'multipart/mixed; boundary="----WebKitFormBoundary7MA4YWxkTrZu0gW"';
What’s the best approach to handle these multipart attachments and move them from the temporary location to a permanent directory on my server?