Retrieve Attachment from Mailgun Form Post PHP
Asked Answered
H

2

8

How do i retrieve and save the attachment sent to me via a form post with Mailgun's POST

The following the some of the parameters

attachment-1    
{:filename=>"crabby.gif", :type=>"image/gif", :name=>"attachment-1", :tempfile=>#<Tempfile:/tmp/RackMultipart20140707-2-slsrkh>, :head=>"Content-Disposition: form-data; name=\"attachment-1\"; filename=\"crabby.gif\"\r\nContent-Type: image/gif\r\nContent-Length: 2785\r\n"}

attachment-2    
{:filename=>"attached_файл.txt", :type=>"text/plain", :name=>"attachment-2", :tempfile=>#<Tempfile:/tmp/RackMultipart20140707-2-sudxuf>, :head=>"Content-Disposition: form-data; name=\"attachment-2\"; filename=\"attached_файл.txt\"\r\nContent-Type: text/plain\r\nContent-Length: 32\r\n"}

Content-Type    
multipart/mixed; boundary="------------020601070403020003080006"
Helices answered 7/7, 2014 at 7:39 Comment(0)
T
8

So I know this is a year late, but I had the same problem and figured out how to download the attachments. The files in the post are in stored in the environmental variable $_FILES. The information for each file will look something like:

Array
(
    [attachment-1] => Array
        (
            [name] => ATextFile.txt
            [type] => text/plain
            [tmp_name] => /tmp/php8zhmlU
            [error] => 0
            [size] => 70144
        )
)

The path to the file is stored in tmp_name, so in this case, /tmp/php8zhmlU is the full path to the file. move_uploaded_file will overwrite any existing files! To download all the attachments from the POST I wrote a function:

function download_attachments($pathToDownloadDirectory)
{
    foreach($_FILES as $file)
    {
        if($file['error'] == "0")
        {
            if(!(move_uploaded_file($file['tmp_name'], $pathToDownloadDirectory . $file['name'])))
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }    
    }
    return 1;
}

download_attachments("/Full/Path/To/Some/Dir/");

Documentation for this top can be found here.

Triangle answered 8/7, 2015 at 18:14 Comment(4)
Can you clarify this a bit, perhaps with some sample code. I am having the same issueCoenurus
referencing mailgun documentation here, documentation.mailgun.com/…Hazelwood
adding a more detailed reference about attachments documentation.mailgun.com/…Hazelwood
I believe the question is around Mailgun's API post, not PHP in general.Britannic
I
4
  1. use store action from route session in order to obtain information about your email. (retrieving stored message
  2. json_decode "attachments" property in order to obtain information about your attachments
  3. get api key and use php library in order to use get api.

if you json decoded list of attachments is $files, you can do

$mgClient = new Mailgun('yourApiKey');

foreach ($files as $file){
    file_put_contents($file->name,$mgClient->get($file->url)->http_response_body);    
}

to download every attachment

Inflow answered 31/12, 2015 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.