1

I'm getting my system set up to use Sendgrid and I have a question about inbound attachments. When Sendgrid hits my API these are the keys listed in the request:

[
    "headers",
    "dkim",
    "content-ids",
    "to",
    "html",
    "from",
    "text",
    "sender_ip",
    "envelope",
    "attachments",
    "subject",
    "attachment-info",
    "charsets",
    "SPF",
    "attachment1"
]

I've logged each of these to my Laravel log but none of them give me the size and the contents of the attachment.

Sorry if this is a stupid question, but how do I get the size and contents of the attachment? As always, a correct, clearly explained answer will be accepted and upvoted. Thanks in advance.

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
Gharbad The Weak
  • 1,541
  • 1
  • 17
  • 39

2 Answers2

1

When handling a webhook call from Sendgrid, an elegant way to handle the attachments is like this:

  1. $request->input('attachment-info') is a json array with some basic information about the attachments. You can iterate over its items.

  2. You can then use the keys from this attachment-info to get the actual files. For example $request->file('attachment1'), which will give you a Illuminate\Http\UploadedFile, from where you can get the file size, open it, read it, store it, ...

Sygmoral
  • 7,021
  • 2
  • 23
  • 31
0

So, after looking into this the problem was not with Sendgrid, it is with the Laravel functions that I was trying to use to get the details about the attachments. In the end, this is the code I used to get the details about the attached files that are coming in with the request:

$attachedFiles = $request->allFiles();

foreach($attachedFiles as $key => $file) {

    $doc = new Document;

    $fileName = $file->getClientOriginalname();

    $doc->fill([
        'size'           => $file->getClientSize(),
        'mimetype'       => $file->getMimeType(),
        'contents'       => fopen($file->getPathname(), 'r'),
    ]);
}
Gharbad The Weak
  • 1,541
  • 1
  • 17
  • 39