ankurk91/laravel-ses-webhooks 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ankurk91/laravel-ses-webhooks

最新稳定版本:4.4

Composer 安装命令:

composer require ankurk91/laravel-ses-webhooks

包简介

Handle AWS SES webhooks in Laravel php framework

README 文档

README

Packagist GitHub-tag License Downloads GH-Actions codecov

Handle AWS SES webhooks in Laravel php framework.

Installation

You can install the package via composer:

composer require "ankurk91/laravel-ses-webhooks"

The service provider will automatically register itself.

You must publish the config file with:

php artisan vendor:publish --provider="Ankurk91\SesWebhooks\SesWebhooksServiceProvider"

Next, you must publish the migration with:

php artisan vendor:publish --provider="Spatie\WebhookClient\WebhookClientServiceProvider" --tag="webhook-client-migrations"

After the migration has been published you can create the webhook_calls table by running the migrations:

php artisan migrate

Next, for routing, add this route (guest) to your routes/web.php

Route::sesWebhooks('/webhooks/ses');

Behind the scenes this will register a POST route to a controller provided by this package. Next, you must add that route to the except array of your VerifyCsrfToken middleware:

protected $except = [
    '/webhooks/*',
];

It is recommended to set up a queue worker to precess the incoming webhooks.

Prepare your AWS console for SES webhooks

  • Assuming that you have SES service up and running in your app already
  • Create a configuration set
  • Choose SNS as event destination
  • Choose HTTP as delivery method for that newly created SNS topic
  • Specify the webhook URL as ${APP_URL}/webhooks/ses
  • This package should auto confirm the subscription
  • You need to specify the configuration set name in your config/services.php
 'ses' => [
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
        'options' => [
            'ConfigurationSetName' => env('AWS_SES_CONFIGURATION_SET'),
        ],
    ],

Usage

There are 2 ways to handle incoming webhooks via this package.

1 - Handling webhook requests using jobs

If you want to do something when a specific event type comes in; you can define a job for that event. Here's an example of such job:

<?php

namespace App\Jobs\Webhooks\SES;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Spatie\WebhookClient\Models\WebhookCall;

class BounceHandlerJob implements ShouldQueue
{
    use SerializesModels;

    public function __construct(protected WebhookCall $webhookCall)
    {
        //
    }

    public function handle()
    {
        $message = $this->webhookCall->payload['Message'];
        
        if (Arr::get($message, 'bounce.bounceType') !== 'Permanent') return;

        foreach ($message['bounce']['bouncedRecipients'] as $recipient) {
            // todo do something with $recipient['emailAddress']
        }
    }
}

After having created your job you must register it at the jobs array in the config/ses-webhooks.php config file. The key should be lowercase and spaces should be replaced by _. The value should be a fully qualified classname.

<?php

return [
     'jobs' => [
          'bounce' => \App\Jobs\Webhooks\SES\BounceHandlerJob::class,
     ],
];

2 - Handling webhook requests using events and listeners

Instead of queueing jobs to perform some work when a webhook request comes in, you can opt to listen to the events this package will fire. Whenever a valid request hits your app, the package will fire a ses-webhooks::<name-of-the-event> event.

The payload of the events will be the instance of WebhookCall that was created for the incoming request.

You can listen for such event by registering the listener in your EventServiceProvider class.

protected $listen = [
    'ses-webhooks::complaint' => [
        App\Listeners\SES\ComplaintListener::class,
    ],
];

Here's an example of such listener class:

<?php

namespace App\Listeners\SES;

use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;

class ComplaintListener implements ShouldQueue
{
    public function handle(WebhookCall $webhookCall)
    {
        $message = $webhookCall->payload['Message'];
        
        foreach ($message['complaint']['complainedRecipients'] as $recipient) {
            // todo do something with $recipient['emailAddress']
        }
    }
}

Pruning old webhooks (opt-in but recommended)

Update your app/Console/Kernel.php file like:

use Illuminate\Database\Console\PruneCommand;
use Spatie\WebhookClient\Models\WebhookCall;

$schedule->command(PruneCommand::class, [
            '--model' => [WebhookCall::class]
        ])
        ->onOneServer()
        ->daily()
        ->description('Prune webhook_calls.');

This will delete records older than 30 days, you can modify this duration by publishing this config file.

php artisan vendor:publish --provider="Spatie\WebhookClient\WebhookClientServiceProvider" --tag="webhook-client-config"

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Security

If you discover any security issue, please email pro.ankurk1[at]gmail[dot]com instead of using the issue tracker.

Useful Links

Acknowledgment

This package is highly inspired by:

License

This package is licensed under MIT License.

统计信息

  • 总下载量: 27.45k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 25
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 25
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固