aleahy/mailgun-logger

一个用于接收Mailgun webhooks并记录事件的软件包

1.0 2023-02-26 02:11 UTC

This package is auto-updated.

Last update: 2024-09-26 05:22:56 UTC


README

此软件包允许Laravel应用程序接收Mailgun webhooks。它使用Spatie的Webhook Client软件包来捕获webhooks。然后您可以按需将作业排队以处理webhooks。

它还有一个服务,可以通过API检索存储的消息。

安装

使用composer安装此软件包

composer require aleahy/mailgun-logger

服务提供者应自动注册。

您需要在.env文件中添加您的mailgun签名密钥。

MAILGUN_SIGNING_SECRET=ABC123

如果您想定义响应webhooks的作业,您需要发布配置文件。

使用以下方法发布配置文件:

php artisan vendor:publish --provider="Aleahy\MailgunLogger\MailgunLoggerServiceProvider" --tag="config"

然后您可以在mailgun-logger配置文件的作业部分定义您的作业。

/**
 * If you want to process a webhook, create a job to handle the type of event.
 * Keys must be one of the event types found at
 * https://documentation.mailgun.com/en/latest/api-events.html#event-types
 */
'jobs' => [
    'failed' => HandleFailedJob::class
],

请确保您定义的每个作业的关键字都是Mailgun API中定义的事件类型之一。

您需要来自Spatie\WebhookClient软件包的迁移来存储webhook调用。

如果您还没有这样做,请使用以下方法发布它们:

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

然后运行迁移

php artisan migrate

路由

在您的routes/web.php文件中使用Route宏mailgunWebhooks定义一个用于接收mailgun事件的路线

# routes/web.php
Route::mailgunWebhooks('webhooks/mailgun');

这将为该软件包中的控制器注册一个POST路由。

您还需要将此路由添加到VerifyCsrfToken中间件中,以绕过CSRF令牌检查。

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

使用

响应Webhooks

创建一个继承自Spatie\WebhookClient\Jobs\ProcessWebhookJob的laravel作业类,当接收到webhook时运行。

由于父类在构造函数中接收webhook,因此可以通过$this->webhookCall->payload访问负载。

如果您决定不扩展Spatie\WebhookClient\Jobs\ProcessWebhookJob,则确保它可以在构造函数中接收webhook。这也非常重要,因为作业必须是可排队的,这样Mailgun就可以立即接收响应。

以下是一个示例

use Spatie\WebhookClient\Jobs\ProcessWebhookJob;

class HandleFailedJob extends ProcessWebhookJob
{
    public function handle()
    {
        // The payload of the webhook call with `$this->webhookCall->payload`
    }
}

一旦创建了作业类,只需将其添加到mailgun-logger配置文件的jobs数组中。键是事件类型,如Mailgun API文档中所述,值是作业类名。

'jobs' => [
    'failed' => HandleFailedJob::class
],

获取存储的消息

一些mailgun webhooks包含指向存储消息的URL。您可以使用MailgunService类访问消息的内容。

$message = MailgunService::get($url);

这将返回一个包含各种消息形式的关联数组,例如stripped-textbody-htmlstripped-html等。

您还可以获取原始MIME消息。

$mimeMessage = MailgunService::getMime($url);

这也返回一个包含body-mime键下mime数据的关联数组。

从0.3升级到1

版本1.0需要PHP 8.0,并且已将Spatie\WebhookClient升级到版本3.1.7。
此软件包的迁移已更改,因此要升级,您必须添加以下迁移

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddColumnsToWebhookCalls extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::table('webhook_calls', function (Blueprint $table) {
            $table->string('url')->nullable();
            $table->json('headers')->nullable();
            $table->json('payload')->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(): void
    {
        Schema::table('webhook_calls', function (Blueprint $table) {
            $table->dropColumn('url');
            $table->dropColumn('headers');
            $table->text('payload')->change();
        });
    }
}

致谢

此软件包的大部分内容都受到binary-cats/laravel-mailgun-webhooks的影响。

还要感谢Spatie为我们创建了如此出色的包,包括他们的Webhook客户端包,这个包就是基于该包实现的。