PHP code example of mydnic / laravel-subscribers

1. Go to this page and download the library: Download mydnic/laravel-subscribers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mydnic / laravel-subscribers example snippets


return [
    // Enable email verification (double opt-in)
    'verify' => env('KANPEN_VERIFY', false),

    // Named route to redirect to after web form submission
    'redirect_url' => 'home',

    // Rate limiting on the two public subscribe endpoints
    'throttle' => [
        'enabled'   => true,
        'per_ip'    => ['attempts' => 10, 'minutes' => 1],
        'per_email' => ['attempts' => 3, 'minutes' => 60],
    ],

    // Route registration
    'routes' => [
        'web'                 => true,  // POST subscribe, unsubscribe, verify, tracking
        'api'                 => true,  // POST /kanpen-api/subscriber
        // Routes resolved when building the links that go out in emails
        'unsubscribe_name'    => 'kanpen.unsubscribe',
        'verify_name'         => 'kanpen.verify',
        'tracking_open_name'  => 'kanpen.tracking.open',
        'tracking_click_name' => 'kanpen.tracking.click',
    ],

    // Verification email content
    'mail' => [
        'verify' => [
            'expiration' => 60, // minutes
            'subject'    => 'Verify Email Address',
            'greeting'   => 'Hello!',
            'content'    => ['Please click the button below to verify your email address.'],
            'action'     => 'Verify Email Address',
            'footer'     => ['If you did not sign up for our newsletter, no further action is 

use Mydnic\Kanpen\Traits\HasNewsletterSubscription;

class User extends Authenticatable
{
    use HasNewsletterSubscription;
}

$user->subscribe();       // adds the user's email to subscribers
$user->unsubscribe();     // soft-deletes the subscriber record
$user->isSubscribed();    // returns bool

// config/kanpen.php
'routes' => [
    'web'                 => false,
    'api'                 => false,
    'unsubscribe_name'    => 'app.unsubscribe',
    'verify_name'         => 'app.verify',
    'tracking_open_name'  => 'app.tracking.open',
    'tracking_click_name' => 'app.tracking.click',
],

// routes/web.php
use Mydnic\Kanpen\Http\Controllers\TrackingController;
use Mydnic\Kanpen\Http\Controllers\UnsubscribeController;

Route::get('newsletter/leave/{token}', [UnsubscribeController::class, 'show'])->name('app.unsubscribe');
Route::delete('newsletter/leave/{token}', [UnsubscribeController::class, 'destroy'])->name('app.unsubscribe.destroy');
Route::get('newsletter/open/{token}', [TrackingController::class, 'open'])->name('app.tracking.open');

use Mydnic\Kanpen\Models\Campaign;

$campaign = Campaign::create([
    'name'         => 'March Newsletter',
    'subject'      => 'What\'s new this month',
    'from_name'    => 'Acme Newsletter',     // optional, falls back to config
    'from_email'   => '[email protected]',       // optional, falls back to config
    'reply_to'     => '[email protected]',    // optional
    'content_html' => '<h1>Hello!</h1><p>Here is what\'s new...</p>',
]);

use Mydnic\Kanpen\Actions\SendCampaignAction;
use Mydnic\Kanpen\Models\Campaign;

$campaign = Campaign::find(1);

app(SendCampaignAction::class)->execute($campaign);

use Mydnic\Kanpen\Models\Campaign;

$campaign = Campaign::create([
    'name'         => 'March Newsletter',
    'subject'      => 'What\'s new this month',
    'content_html' => '<p>...</p>',
    'scheduled_at' => now()->addDays(3), // send in 3 days
]);

// config/kanpen.php
'campaigns' => [
    'schedule' => false,
],

// bootstrap/app.php (Laravel 11+)
->withSchedule(function (Schedule $schedule) {
    $schedule->command('kanpen:dispatch-scheduled')->everyFiveMinutes();
})

$campaign = Campaign::create([
    'name'    => 'Special Announcement',
    'subject' => 'Big news!',
    'view'    => 'emails.special-announcement', // your own Blade view
]);

'campaigns' => [
    'middleware' => ['api', 'auth:sanctum'],
],

// config/kanpen.php
'tracking' => [
    'enabled' => false, // disables all tracking
    'open'    => false, // disables open pixel only
    'click'   => false, // disables click rewriting only
],

'tracking' => [
    'allowed_domains' => ['mysite.com', 'blog.mysite.com'],
],

use Mydnic\Kanpen\Events\EmailOpened;
use Mydnic\Kanpen\Events\EmailLinkClicked;

protected $listen = [
    EmailOpened::class => [
        UpdateAnalyticsDashboardListener::class,
    ],
    EmailLinkClicked::class => [
        LogClickListener::class,
    ],
];

use Mydnic\Kanpen\Traits\HasNewsletterSubscription;

class User extends Authenticatable
{
    use HasNewsletterSubscription;

    /**
     * Define when this user should be a subscriber.
     * Any logic works — check a column, a role, a plan, a combination.
     */
    public function shouldBeSubscribed(): bool
    {
        return $this->subscribed_to_newsletter;
    }

    /**
     * Called automatically when the subscriber is removed from outside your app
     * (e.g. the user clicks the unsubscribe link in an email).
     * Use this to keep your own model in sync so the user isn't re-subscribed next time they save.
     */
    public function onUnsubscribed(): void
    {
        $this->updateQuietly(['subscribed_to_newsletter' => false]);
    }
}

$user->syncSubscriberRecord();

// Before
use Mydnic\Subscribers\Subscriber;

// After
use Mydnic\Kanpen\Models\Subscriber;
bash
php artisan vendor:publish --tag="kanpen-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="kanpen-config"
bash
php artisan queue:work
bash
php artisan kanpen:sync "App\Models\User"
bash
php artisan vendor:publish --tag="kanpen-views"
bash
php artisan vendor:publish --tag="kanpen-migrations"
php artisan migrate