I am trying to use Sendgrid inbound parse and I have a script working correctly. The webhook gets executed and I am able to save the message into our app database. Now I have a problem on how to parse attachments. I have been researching for a while with no luck. in the inbound request there is an attachment-info that contains information about the attachment, but where is it actually stored? or how can I retrieve to store it on our server or view it online if possible?
thank you
$html = $_POST['html'];
$recipient = $_POST['to'];
$subject = $_POST['subject'];
$envelope = json_decode($_POST['envelope']);
$sender = $envelope->from;
$user = -1;
$created_at = date('Y-m-d H:i:s');
$attachments = $_POST['attachments'];
if($attachments > 0)
{
$attachment_info = json_decode($_POST['attachment-info'], true);
for($i = 1; $i <= $attachments; $i++)
{
//process attachments how???
$file = $attachment_info['attachment'.$i];
// {"filename":"shutterstock_745545163.jpg","name":"shutterstock_745545163.jpg","type":"image/jpeg","content-id":"ii_ku4rffz40"}
//what to I do whit this info? how do I retrieve the attachment from this emial??
}
}
$params = [
'type' => 'email',
'customer_id' => get_customer($sender),
'direction' => 'in',
'sender' => $sender,
'recipient' => $recipient,
'subject' => $subject,
'message' => $html,
'status' => 'delivered',
'user' => 0,
];
//process email
//this stores email into DB
process_email($params);
Twilio SendGrid developer evangelist here.
All the files are sent along with the rest of the data in the webhook. The request is a multipart/form-data
request so the files are sent as part of the request too.
You will find the content of the files in PHP's $_FILES
array. See this PHP documentation for how multipart requests are managed and this PHP example for handling the SendGrid inbound parse webhook for more detail.