Skip to content

Instantly share code, notes, and snippets.

@ewandennis
Created November 17, 2016 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewandennis/13960117bd41d33f1074ae4fa784699d to your computer and use it in GitHub Desktop.
Save ewandennis/13960117bd41d33f1074ae4fa784699d to your computer and use it in GitHub Desktop.
SparkPost relay webhooks sample
<?php
/* Dependencies:
* - php-mime-mail-parser (https://github.com/php-mime-mail-parser/php-mime-mail-parser)
* - The mailparse extension (http://php.net/manual/en/book.mailparse.php)
*/
require 'vendor/autoload.php';
$messages = json_decode(file_get_contents('php://input'));
foreach ($messages as $msg) {
$emailBody = $msg->msys->relay_message->content->email_rfc822;
if ($msg->msys->relay_message->content->email_rfc822_is_base64) {
$emailBody = base64_decode($emailBody);
}
// Parse the message, save and list attachments
$parser = new PhpMimeMailParser\Parser();
$parser->setText($emailBody);
$parser->saveAttachments('/somewhere/safe/');
$attachments = $parser->getAttachments();
foreach ($attachments as $file) {
echo('Filename: ' . $file->getFilename() . '<br>');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment