如何按时间筛选Mailgun退信?Laravel获取近1小时退信求助
Hey there! I’ve tinkered with Mailgun’s APIs a fair bit, so let me walk you through how to filter bounces to only the last hour—you’re closer than you think!
Mailgun’s /bounces endpoint does support time-based filtering, even if it’s easy to miss机器人ata events 良**Sched,外ao events供电, Gsl收到. The key parameter you need is created_after, which lets you specify a cutoff time; only bounces created after that time will be returned.
Here’s how to adjust your Laravel code to make this work:
First, we’ll calculate the timestamp for one hour ago using Laravel’s built-in Carbon library (super handy for date handling):
use Carbon\Carbon; // Generate an ISO 8601 formatted timestamp for 1 hour prior to now $oneHourAgo = Carbon::now()->subHour()->toIso8601String(); // Update your GET request to include the filter parameter $response = Http::get( 'https://api:' . env('MAILGUN_SECRET') . '@api.mailgun.net/v3/' . env('MAILGUN_DOMAIN') . '/bounces', [ 'created_after' => $oneHourAgo ] );
A quick note on the parameter:
- Mailgun accepts both ISO 8601 strings (like
2024-05-20T14:30:00Z) and Unix timestamps (like1716234600) forcreated_after—either will work, but ISO 8601 is more readable for debugging. - If you ever need to filter older ranges later, there’s also a
created_beforeparameter you can use alongside this one.
If you end up with a lot of bounces in an hour, you can add limit and skip parameters to handle pagination, but for most use cases, the above should get you exactly the last hour’s bounces you need.
内容的提问来源于stack exchange,提问作者Senthurkumaran

