Mailgun – Inbound email attachment save to laravel storage

When the request of an inbound email is parsed by the controller, the attachments are a json blob array of the form below.

"attachments": [
    {
        "url": "https://se.api.mailgun.net/v3/domains/mg.example.com/messages/eyJwIjpmYWxzZSwiayI6IjBhYjM5MWE5LTU5YzUtNGJkMS1hMzE5LTBhNjU0ODAwOTY4ZCIsInMiOiIyYWMyN2YxYzc2IiwiYyI6InRhbmtiIn0=/attachments/0",
        "content-type": "text/csv",
        "name": "widget-order.csv",
        "size": 554
    }
]

An inbound email has one or more attachments accessible through authentication. For this purpose, the method below makes use of GuzzleHttpClient.
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
use IlluminateSupportFacadesStorage;
use IlluminateDatabaseEloquentCollection;
use GuzzleHttpClient;

class MailgunWidgetsController extends Controller
{
    public function store(Request $request)
    {
        try
        {
            $attachs = request('attachments');

            if(!is_null($attachs)) {
                $attachments = json_decode($attachs, true);

                foreach($attachments as $k => $a) {
                    $httpClient = new Client();
                    $resp = $httpClient->request('GET', $a['url'], ['auth' => ['api' => 'key-example']]);
                    $imageData = $resp->getBody();
                    $base64 = base64_encode($imageData);
                    $this->saveTicketAttachment($base64, $a['name'], $ticket);
                }
            }
            return response()->json(['status' => 'ok']);
        }   
        catch(Exception $e) 
        {
            return response()->json(['status' => 'ok']);
        }
    }
}

Now, the code block enters the foreach, the url of the attachment is given by $a['url'] but the GET request fails. I think an exception of some kind has occurred but it does not get logged in the laravel.log file because the code execution stops at the GET request.

Objective is to access this attachment and convert it to its base64 encode.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The correct implementation to generate a base64 encoded string from an inbound email attachment using GuzzleHttpClient as:

private function getTicketAttachment($attachment)
{
    $httpClient = new Client();
    $response = $httpClient->get($attachment['url'], [
        'auth' => ['api', env("MAILGUN_SECRET")], 
    ]);
    $imageData = (string)$response->getBody();
    $base64 = base64_encode($imageData);
    return $base64;
}

Method 2

I write this for others coming here following the tutorial on laravel news

this is how you grab the file and puts it in your storage folder:

Storage::put(.$file['name'], $response->getBody());


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x