Skip to content

Commit

Permalink
Add unsuppress logic
Browse files Browse the repository at this point in the history
  • Loading branch information
arduinomaster22 committed Jan 11, 2025
1 parent 6a2d42b commit ef730a5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
"laravel-notification-channels/telegram": "^4.0|5.0",
"laravel/helpers": "^1.7.0",
"laravel/slack-notification-channel": "^2.5|^3.3.2",
"spatie/laravel-package-tools": "^1.14.0"
"mailgun/mailgun-php": "^4.3",
"nyholm/psr7": "^1.8",
"spatie/laravel-package-tools": "^1.14.0",
"symfony/http-client": "^7.2",
"symfony/mailgun-mailer": "^7.2",
"symfony/postmark-mailer": "^7.2"
},
"require-dev": {
"larastan/larastan": "^2.9.8",
Expand Down Expand Up @@ -74,4 +79,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
37 changes: 36 additions & 1 deletion src/Models/MailEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Mailgun\Mailgun;
use Vormkracht10\Mails\Database\Factories\MailEventFactory;
use Vormkracht10\Mails\Drivers\PostmarkDriver;
use Vormkracht10\Mails\Enums\EventType;
Expand Down Expand Up @@ -96,6 +98,39 @@ protected function getEventClassAttribute(): string

public function unSuppress()
{
$this->update(['unsuppressed_at' => now()]);
if (config('mail.default') === 'postmark') {
$client = Http::asJson()
->withHeaders([
'X-Postmark-Server-Token' => config('services.postmark.token')
])
->baseUrl('https://api.postmarkapp.com/');

$streamId = 'broadcast';
$response = $client->post("message-streams/{$streamId}/suppressions/delete", [
'Suppressions' => [
[
'emailAddress' => key($this->mail->to)
]
]
]);

if ($response->successful()) {
$this->update(['unsuppressed_at' => now()]);
} else {
throw new \Exception('Failed to unsuppress email address due to ' . $response);
}
}

if (config('mail.default') === 'mailgun') {
$mailgun = Mailgun::create(config('services.mailgun.secret'), 'https://api.mailgun.net/v3');

$response = $mailgun->suppressions()->unsubscribes()->delete(config('services.mailgun.domain'), key($this->mail->to));

if ($response ) {
$this->update(['unsuppressed_at' => now()]);
} else {
throw new \Exception('Failed to unsuppress email address due to ' . $response);
}
}
}
}

0 comments on commit ef730a5

Please sign in to comment.