Last active
November 5, 2018 22:55
-
-
Save kaioken/219d98ca235aa4788dcd0cf461192e25 to your computer and use it in GitHub Desktop.
Mailgun Report via API using PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/../vendor/autoload.php'; | |
/** | |
* Get mailgun report | |
* | |
* @param string $url | |
* @return array | |
*/ | |
function getMailgunReport(string $url): array | |
{ | |
$client = new GuzzleHttp\Client(); | |
$response = $client->request( | |
'GET', | |
$url, | |
[ | |
'auth' => ['api', 'key'] | |
] | |
); | |
return json_decode($response->getBody(), true); | |
} | |
/** | |
* Get all mailgun reports | |
* | |
* @param string $url | |
* @param array $list | |
* @return void | |
*/ | |
function getAllMailgunReports(string $url, array &$list): void | |
{ | |
$results = getMailgunReport($url); | |
foreach ($results['items'] as $email) { | |
if (!array_key_exists(trim(strtolower($email['recipient'])), $list)) { | |
$list[trim(strtolower($email['recipient']))] = trim($email['recipient']); | |
echo strtolower($email['recipient']) . PHP_EOL; | |
} | |
} | |
getAllMailgunReports($results['paging']['next'], $list); | |
} | |
getAllMailgunReports('https://api.mailgun.net/v3/{domain}/events?subject={search text}&limit=300', $list); | |
//run in console php mailgunreport.php > log.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment