Skip to content

Instantly share code, notes, and snippets.

@umrysh
Created January 29, 2015 23:11
Show Gist options
  • Save umrysh/8b7bd3651817e5bdb1c5 to your computer and use it in GitHub Desktop.
Save umrysh/8b7bd3651817e5bdb1c5 to your computer and use it in GitHub Desktop.
Very simple PHP webhook for Mailgun that emails you on errors.
<?php
$key = "<API Key>";
$from = "info@example.com";
$to = "dave@example.com";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_POST['timestamp']) && isset($_POST['token']) && isset($_POST['signature']) && hash_hmac('sha256', $_POST['timestamp'] . $_POST['token'], $key) === $_POST['signature'])
{
if($_POST['event'] == 'complained') {
$subject = "[Mailgun] Spam Complaint";
$body = "Recipient: " . $_POST['recipient'] . "\nDomain: " . $_POST['domain'] . "\n\nMessage-headers: " . $_POST['message-headers'] . "\n";
mail($to, $subject, $body, "From: " . $from,"-f". $from);
}elseif($_POST['event'] == 'bounced'){
$subject = "[Mailgun] Bounced Email";
$body = "Recipient: " . $_POST['recipient'] . "\nDomain: " . $_POST['domain'] . "\nCode: " . $_POST['code'] . "\nError: " . $_POST['error'] . "\nNotification: " . $_POST['notification'] . "\n\nMessage-headers: " . $_POST['message-headers'] . "\n";
mail($to, $subject, $body, "From: " . $from,"-f". $from);
}elseif($_POST['event'] == 'dropped'){
$subject = "[Mailgun] Failed Email";
$body = "Recipient: " . $_POST['recipient'] . "\nDomain: " . $_POST['domain'] . "\nCode: " . $_POST['code'] . "\nReason: " . $_POST['reason'] . "\nDescription: " . $_POST['description'] . "\n\nMessage-headers: " . $_POST['message-headers'] . "\n";
mail($to, $subject, $body, "From: " . $from,"-f". $from);
}
}
}
header('X-PHP-Response-Code: 200', true, 200);
?>
@ChrisFlannagan
Copy link

You can shorten the isset check with

isset( $_POST['timestamp'], $_POST['token'], $_POST['signature'] )

@PCAssistSoftware
Copy link

Hi, I made use of this method for getting emails from Mailgun using the webhooks - but they have now made those webhooks legacy and will be getting rid of them at some point. I don't suppose you have got a similar working method for the new webhooks they have added?? thanks

@breakpoint
Copy link

$key = '<mailgun secret>';

$event = json_decode(file_get_contents('php://input'));

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    if (isset($event->signature) && hash_hmac('sha256', $event->signature->timestamp.$event->signature->token, $key) === $event->signature->signature) {

    // save event data for use later
    $data = $event->{'event-data'};

    // https://documentation.mailgun.com/en/latest/api-events.html#event-types
    switch ($data->event) {
        case 'accepted':
            // handle
            break;
        case 'complained':
            // handle
            break;
        case 'rejected':
            // handle
            break;
        }
    }
}

http_response_code(200);

@andrebruton
Copy link

@breakpoint How do you read other data in your example like the message ID. I've tried various options and do not get a result.

Eg:
$msg_id = $event->event_data->message->headers->{'message-id'};

How do I get custom variables? I pass the variable emails_id when I send the email and I've seen it in the JSON data.

@chrishow
Copy link

[In case anyone is similarly confused]

How do you read other data in your example like the message ID. I've tried various options and do not get a result.

Eg:
$msg_id = $event->event_data->message->headers->{'message-id'};

$data->message->headers->{'message-id'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment