-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Display a console info message using |
||
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'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ enum Provider: string | |
{ | ||
case POSTMARK = 'postmark'; | ||
case MAILGUN = 'mailgun'; | ||
case RESEND = 'resend'; | ||
} |
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', | ||
]); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be removed.