Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add resend provider #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use Vormkracht10\Mails\Enums\Provider;
use Vormkracht10\Mails\Events\MailEvent;
use Vormkracht10\Mails\Facades\MailProvider;
Expand All @@ -20,6 +21,8 @@ public function __invoke(Request $request, string $driver): Response
return response('Invalid signature.', status: 400);
}

Log::channel('single')->log('info', 'Webhook received', $request->all());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed.


MailEvent::dispatch(
$driver,
$request->except('signature')
Expand Down
137 changes: 137 additions & 0 deletions src/Drivers/ResendDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace Vormkracht10\Mails\Drivers;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\URL;
use Vormkracht10\Mails\Contracts\MailDriverContract;
use Vormkracht10\Mails\Enums\EventType;

class ResendDriver extends MailDriver implements MailDriverContract
{
public function registerWebhooks($components): void
{
// TODO: verify if we can hack the user endpoint to create a webhook
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Display a console info message using $components with webhook URL and the link to add this to Resend (https://resend.com/webhooks)

return;

$trackingConfig = (array) config('mails.logging.tracking');

$triggers = [
'Open' => [
'Enabled' => (bool) $trackingConfig['opens'],
'PostFirstOpenOnly' => false,
],
'Click' => [
'Enabled' => (bool) $trackingConfig['clicks'],
],
'Delivery' => [
'Enabled' => (bool) $trackingConfig['deliveries'],
],
'Bounce' => [
'Enabled' => (bool) $trackingConfig['bounces'],
'IncludeContent' => (bool) $trackingConfig['bounces'],
],
'SpamComplaint' => [
'Enabled' => (bool) $trackingConfig['complaints'],
'IncludeContent' => (bool) $trackingConfig['complaints'],
],
'SubscriptionChange' => [
'Enabled' => (bool) $trackingConfig['unsubscribes'],
],
];

$webhookUrl = URL::signedRoute('mails.webhook', ['provider' => 'postmark']);

$token = (string) config('services.postmark.token');

$headers = [
'Accept' => 'application/json',
'X-Postmark-Server-Token' => $token,
];

$broadcastStream = collect(Http::withHeaders($headers)->get('https://api.postmarkapp.com/message-streams')['MessageStreams'] ?? []);

if ($broadcastStream->where('ID', 'broadcast')->count() === 0) {
Http::withHeaders($headers)->post('https://api.postmarkapp.com/message-streams', [
'ID' => 'broadcast',
'Name' => 'Broadcasts',
'Description' => 'Default Broadcast Stream',
]);
} else {
$components->info('Broadcast stream already exists');
}

$outboundWebhooks = collect(Http::withHeaders($headers)->get('https://api.postmarkapp.com/webhooks?MessageStream=outbound')['Webhooks'] ?? []);

if ($outboundWebhooks->where('Url', $webhookUrl)->count() === 0) {
$response = Http::withHeaders($headers)->post('https://api.postmarkapp.com/webhooks?MessageStream=outbound', [
'Url' => $webhookUrl,
'Triggers' => $triggers,
]);

if ($response->ok()) {
$components->info('Created Postmark webhook for outbound stream');
} else {
$components->error('Failed to create Postmark webhook for outbound stream');
}
} else {
$components->info('Outbound webhook already exists');
}

$broadcastWebhooks = collect(Http::withHeaders($headers)->get('https://api.postmarkapp.com/webhooks?MessageStream=broadcast')['Webhooks'] ?? []);

if ($broadcastWebhooks->where('Url', $webhookUrl)->count() === 0) {
$response = Http::withHeaders($headers)->post('https://api.postmarkapp.com/webhooks?MessageStream=broadcast', [
'Url' => $webhookUrl,
'MessageStream' => 'broadcast',
'Triggers' => $triggers,
]);

if ($response->ok()) {
$components->info('Created Postmark webhook for broadcast stream');
} else {
$components->error('Failed to create Postmark webhook for broadcast stream');
}
} else {
$components->info('Broadcast webhook already exists');
}
}

public function verifyWebhookSignature(array $payload): bool
{
return true;
}

public function getUuidFromPayload(array $payload): ?string
{
return $payload['data']['email_id'];
}

protected function getTimestampFromPayload(array $payload): string
{
return $payload['data']['created_at'] ?? now();
}

public function eventMapping(): array
{

return [
EventType::CLICKED->value => ['type' => 'email.clicked'],
EventType::COMPLAINED->value => ['type' => 'email.complained'],
EventType::DELIVERED->value => ['type' => 'email.delivered'],
EventType::HARD_BOUNCED->value => ['type' => 'email.bounced'],
EventType::OPENED->value => ['type' => 'email.opened'],
EventType::SOFT_BOUNCED->value => ['type' => 'email.delivery_delayed'],
EventType::UNSUBSCRIBED->value => ['type' => 'SubscriptionChange'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed if Resend does not have a subscribed event.

];
}

public function dataMapping(): array
{
return [
'ip_address' => 'data.click.ipAddress',
'link' => 'data.click.link',
'user_agent' => 'data.click.userAgent',
];
}
}
1 change: 1 addition & 0 deletions src/Enums/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ enum Provider: string
{
case POSTMARK = 'postmark';
case MAILGUN = 'mailgun';
case RESEND = 'resend';
}
6 changes: 6 additions & 0 deletions src/Managers/MailProviderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Manager;
use Vormkracht10\Mails\Drivers\MailgunDriver;
use Vormkracht10\Mails\Drivers\PostmarkDriver;
use Vormkracht10\Mails\Drivers\ResendDriver;

class MailProviderManager extends Manager
{
Expand All @@ -23,6 +24,11 @@ protected function createMailgunDriver(): MailgunDriver
return new MailgunDriver;
}

protected function createResendDriver(): ResendDriver
{
return new ResendDriver;
}

public function getDefaultDriver(): ?string
{
return null;
Expand Down
210 changes: 210 additions & 0 deletions tests/ResendTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
<?php

use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\URL;
use Vormkracht10\Mails\Enums\EventType;
use Vormkracht10\Mails\Models\Mail as MailModel;
use Vormkracht10\Mails\Models\MailEvent;

use function Pest\Laravel\assertDatabaseHas;
use function Pest\Laravel\post;

it('can receive incoming delivery webhook from resend', function () {
Mail::send([], [], function (Message $message) {
$message->to('[email protected]')
->from('[email protected]')
->cc('[email protected]')
->bcc('[email protected]')
->subject('Test')
->text('Text')
->html('<p>HTML</p>');
});

$mail = MailModel::latest()->first();

post(URL::signedRoute('mails.webhook', ['provider' => 'resend']), [
'created_at' => '2023-05-19T22:09:32Z',
'data' => [
'created_at' => '2025-01-09 14:17:29.059104+00',
'email_id' => $mail->uuid,
'from' => '[email protected]',
'subject' => 'Test',
'to' => ['[email protected]'],
'cc' => ['[email protected]'],
'bcc' => ['[email protected]'],
],
'type' => 'email.delivered',
])->assertAccepted();

assertDatabaseHas((new MailEvent)->getTable(), [
'type' => EventType::DELIVERED->value,
]);
});

it('can receive incoming hard bounce webhook from resend', function () {
Mail::send([], [], function (Message $message) {
$message->to('[email protected]')
->from('[email protected]')
->cc('[email protected]')
->bcc('[email protected]')
->subject('Test')
->text('Text')
->html('<p>HTML</p>');
});

$mail = MailModel::latest()->first();

post(URL::signedRoute('mails.webhook', ['provider' => 'resend']), [
'created_at' => '2023-05-19T22:09:32Z',
'data' => [
'created_at' => '2025-01-09 14:17:29.059104+00',
'email_id' => $mail->uuid,
'from' => '[email protected]',
'subject' => 'Test',
'to' => ['[email protected]'],
'cc' => ['[email protected]'],
'bcc' => ['[email protected]'],
],
'type' => 'email.bounced',
])->assertAccepted();

assertDatabaseHas((new MailEvent)->getTable(), [
'type' => EventType::HARD_BOUNCED->value,
]);
});

it('can receive incoming soft bounce webhook from resend', function () {
Mail::send([], [], function (Message $message) {
$message->to('[email protected]')
->from('[email protected]')
->cc('[email protected]')
->bcc('[email protected]')
->subject('Test')
->text('Text')
->html('<p>HTML</p>');
});

$mail = MailModel::latest()->first();

post(URL::signedRoute('mails.webhook', ['provider' => 'resend']), [
'created_at' => '2023-05-19T22:09:32Z',
'data' => [
'created_at' => '2025-01-09 14:17:29.059104+00',
'email_id' => $mail->uuid,
'from' => '[email protected]',
'subject' => 'Test',
'to' => ['[email protected]'],
'cc' => ['[email protected]'],
'bcc' => ['[email protected]'],
],
'type' => 'email.delivery_delayed',
])->assertAccepted();

assertDatabaseHas((new MailEvent)->getTable(), [
'type' => EventType::SOFT_BOUNCED->value,
]);
});

it('can receive incoming complaint webhook from resend', function () {
Mail::send([], [], function (Message $message) {
$message->to('[email protected]')
->from('[email protected]')
->cc('[email protected]')
->bcc('[email protected]')
->subject('Test')
->text('Text')
->html('<p>HTML</p>');
});

$mail = MailModel::latest()->first();

post(URL::signedRoute('mails.webhook', ['provider' => 'resend']), [
'created_at' => '2023-05-19T22:09:32Z',
'data' => [
'created_at' => '2025-01-09 14:17:29.059104+00',
'email_id' => $mail->uuid,
'from' => '[email protected]',
'subject' => 'Test',
'to' => ['[email protected]'],
'cc' => ['[email protected]'],
'bcc' => ['[email protected]'],
],
'type' => 'email.complained',
])->assertAccepted();

assertDatabaseHas((new MailEvent)->getTable(), [
'type' => EventType::COMPLAINED->value,
]);
});

it('can receive incoming open webhook from resend', function () {
Mail::send([], [], function (Message $message) {
$message->to('[email protected]')
->from('[email protected]')
->cc('[email protected]')
->bcc('[email protected]')
->subject('Test')
->text('Text')
->html('<p>HTML</p>');
});

$mail = MailModel::latest()->first();

post(URL::signedRoute('mails.webhook', ['provider' => 'resend']), [
'created_at' => '2023-05-19T22:09:32Z',
'data' => [
'created_at' => '2025-01-09 14:17:29.059104+00',
'email_id' => $mail->uuid,
'from' => '[email protected]',
'subject' => 'Test',
'to' => ['[email protected]'],
'cc' => ['[email protected]'],
'bcc' => ['[email protected]'],
],
'type' => 'email.opened',
])->assertAccepted();

assertDatabaseHas((new MailEvent)->getTable(), [
'type' => EventType::OPENED->value,
]);
});

it('can receive incoming click webhook from resend', function () {
Mail::send([], [], function (Message $message) {
$message->to('[email protected]')
->from('[email protected]')
->cc('[email protected]')
->bcc('[email protected]')
->subject('Test')
->text('Text')
->html('<p>HTML</p>');
});

$mail = MailModel::latest()->first();

post(URL::signedRoute('mails.webhook', ['provider' => 'resend']), [
'created_at' => '2023-05-19T22:09:32Z',
'data' => [
'created_at' => '2025-01-09 14:17:29.059104+00',
'email_id' => $mail->uuid,
'from' => '[email protected]',
'subject' => 'Test',
'to' => ['[email protected]'],
'cc' => ['[email protected]'],
'bcc' => ['[email protected]'],
'click' => [
'ipAddress' => '122.115.53.11',
'link' => 'https://resend.com',
'timestamp' => '2024-11-24T05:00:57.163Z',
'userAgent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15',
],
],
'type' => 'email.clicked',
])->assertAccepted();

assertDatabaseHas((new MailEvent)->getTable(), [
'type' => EventType::CLICKED->value,
'link' => 'https://resend.com',
]);
});