1

I have download a file from the link & has to save in my local system folder or in a remote server folder. The scenario is that: I have a mailgun domain, when I send a mail to it, Mailgun store function (store()) stores it with all attachments & notifies me. The response from mailgun is catched in catch_email_attachment(), I'm able fetch the response & got the link of attached files. When I run the link directly in browser it gives me the attached file, no problem on that. But I need to download the file inside catch_email_attachment() & to save it in a folder.

The downloadable file is as: "https://API:<API-KEY>@api.mailgun.net/v2/domains/sandboxa6e6ebce3f68475aa3xxxxxxxd60.mailgun.org/messages/eyJwIjogZmFsc2UsICJrIjogImQ0MmZjxxxxxxxxxxxxxxDQwNy1iYzhlLTA2OWMxY2U3MDg2NCIsIxxxxxxxxxxxxxx1Y2UiLCAiYyI6ICJpYWR0cmFpbGVycyJ9/attachments/0"

My codes are below:

public function catch_email_attachment()
{
    $data = $this->input->post(null, true);
    if (!empty($data)) {
        if (isset($data['attachments'])) {
            /*
            Output of $data['attachments'] is below:
            [{"url": "https://api.mailgun.net/v2/domains/sandboxa6e6ebce3f68475aa3xxxxxxxd60.mailgun.org/messages/eyJwIjogZmFsc2UsICJrIjogImQ0MmZjxxxxxxxxxxxxxxDQwNy1iYzhlLTA2OWMxY2U3MDg2NCIsIxxxxxxxxxxxxxx1Y2UiLCAiYyI6ICJpYWR0cmFpbGVycyJ9/attachments/0", "content-type": "image/jpeg", "name": "xxxxxxx.jpeg", "size": 9498}]
            */

            copy('https://API:key-e5ae9afab1fa9xxxxxxxxxxxxxxa95a@api.mailgun.net/v2/domains/sandboxa6e6ebce3f68475axxxxxxxxxxxxxxxxxxxxxxxxxxxx.mailgun.org/messages/eyJwIjogZmFsc2UxxxxxxxxxxxxxxxxxxxxxxxxxxxxmUtNDQwNy1iYzhlLTA2OWMxY2U3MDg2NCIxxxxxxxxxxxxxxxxxxxxxxxxxxxx1Y2UiLCAiYyI6ICJpYWR0cmFpbGVycyJ9/attachments/0', '/var/www/download_loc/');
        }
    }
}

I have refered: https://stackoverflow.com/a/26330976/4229270

https://stackoverflow.com/a/6594030/4229270

https://stackoverflow.com/a/724449/4229270

Can you help me to solve the issue... Thanking in advance.

Community
  • 1
  • 1
Sinto
  • 3,915
  • 11
  • 36
  • 70

2 Answers2

1

It looks like $data['attachments'] is a json array so you need something like:

    $attachments = json_decode($data['attachments']);
        $api_key = 'APIKEY';
        if ($attachments) {
            foreach ($attachments as $attachment) {

                $context = stream_context_create(array(
                    'http' => array(
                        'header'  => "Authorization: Basic " . base64_encode("API:$api_key")
                    )
                ));

                file_put_contents('/var/www/download_loc/' . $attachment->name, file_get_contents($attachment->url, false, $context));

            }
        }
Farkie
  • 3,307
  • 2
  • 22
  • 33
  • yes, $data['attachments'] its a jsode data. I have also checked your code, but not solved. And, if I run "$attachment->url" in browser as I said earlier, the file gets downloaded. – Sinto May 06 '16 at 07:32
  • i have also set folder permissions, still it does not goes well. – Sinto May 06 '16 at 07:34
  • Hi Sinto, I've checked and my code definitely does what you'd expect. Don't you need to add the `API:@` part? – Farkie May 06 '16 at 07:36
  • Ya, I have added it while checking. Still not worked – Sinto May 06 '16 at 07:39
  • Still not, Is there anything wrong in my local path? – Sinto May 06 '16 at 07:48
0

The above answer is surly helpful for me. What I have done & got is:

Mailgun will give response to our hook function which we set in our maingun domain. Catch the response from Mailgun as:

$data = $this->input->post(null, true);
$attachments = json_decode($data['attachments']);
$apikey = 'API:<API_KEY>@';
foreach ($attachments as $attachment) {
    $st = strpos($attachment->url, 'https://');
    if ($st !== false) {
        $attachment->url = str_replace('https://', 'https://'.$apikey, $attachment->url);
        $temp_name = $attachment->name;
        $file_path = '<PATH_TO_DOWNLOAD>'.$temp_name;
        copy($attachment->url, $file_path); // Downloading file to our path
    }
}

You have to check endpoint from Mailgun response. And, have to set respective in or code. Also, we have to set API KEY & PATH TO DOWNLOAD.

The storage link from Mailgun will be to "xx.api.mailgun.net" over "api.mailgun.net". It will be like "si.api.mailgun.net", "so.api.mailgun.net", "se.api.mailgun.net", "sw.api.mailgun.net", etc.. The URL to obtain the attachment data would be

Eg: https://API:key-60mvxxxxxxxxxxxxxxxx@sw.api.mailgun.net/v3/domains/YOUR_MAILGUN_DOMAIN/messages/xxxxxxxxxxxxxxxxxxxxxxxxxxx=/attachments/0

The data returned from the API will return the URL to obtain the stored message : (items > attachments > url)

Please refer: https://documentation.mailgun.com/en/latest/api-sending.html#retrieving-stored-messages

Sinto
  • 3,915
  • 11
  • 36
  • 70
  • https://documentation.mailgun.com/en/latest/api-sending.html#retrieving-stored-messages – Sinto Jun 22 '17 at 13:10