PHP code example of nextmigrant / laravel-plunk
1. Go to this page and download the library: Download nextmigrant/laravel-plunk 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/ */
nextmigrant / laravel-plunk example snippets
use NextMigrant\Plunk\Plunk;
// Send a transactional email
Plunk::transactional()->send(
to: '[email protected] ',
subject: 'Welcome aboard!',
body: '<h1>Welcome to our platform</h1>',
);
// Track an event
Plunk::events()->track(
email: '[email protected] ',
event: 'signed_up',
data: ['plan' => 'pro'],
);
// Verify an email
$result = Plunk::verifyEmail('[email protected] ');
// $result->valid, $result->isDisposable, $result->hasMxRecords, etc.
// Inline content
Plunk::transactional()->send(
to: '[email protected] ', // string, {name, email}, or array
subject: 'Your Invoice',
body: '<h1>Invoice #1234</h1>',
from: ['name' => 'Acme', 'email' => '[email protected] '], // verified domain
reply: '[email protected] ',
subscribed: true,
data: ['invoice_id' => '1234'], // contact data + template vars
headers: ['X-Priority' => '1'],
attachments: [
[
'filename' => 'invoice.pdf',
'content' => base64_encode($pdfContent),
'contentType' => 'application/pdf',
],
],
);
// Using a template (subject/body come from the template)
Plunk::transactional()->send(
to: '[email protected] ',
template: 'tpl_welcome',
data: ['firstName' => 'John', 'plan' => 'pro'],
);
Plunk::events()->track(
email: '[email protected] ',
event: 'plan_upgraded',
data: ['plan' => 'enterprise', 'seats' => 50],
subscribed: false, // Subscription state for auto-created contacts
);
// List contacts (cursor-based pagination)
$result = Plunk::contacts()->list(
search: 'john', // Filter by email substring
limit: 50, // Items per page (max 100)
cursor: $cursor, // Cursor from previous response
);
foreach ($result['data'] as $contact) {
echo $contact->email; // Contact DTO
echo $contact->subscribed;
}
// $result['cursor'], $result['hasMore'], $result['total']
// Get a single contact
$contact = Plunk::contacts()->get('contact_id');
// Create or upsert a contact
$result = Plunk::contacts()->create('[email protected] ',
subscribed: true,
data: ['source' => 'api', 'plan' => 'free'],
);
// $result['_meta']['isNew'], $result['_meta']['isUpdate']
// Update a contact (PATCH)
$result = Plunk::contacts()->update('contact_id',
subscribed: false,
data: ['plan' => 'pro'],
);
// Delete a contact
Plunk::contacts()->delete('contact_id');
// Bulk email-existence check (max 500 emails)
$result = Plunk::contacts()->lookup(['[email protected] ', '[email protected] ']);
// Subscribe/unsubscribe/delete (up to 1,000 IDs)
$result = Plunk::contacts()->bulkSubscribe(['id_1', 'id_2', 'id_3']);
$result = Plunk::contacts()->bulkUnsubscribe(['id_1', 'id_2']);
$result = Plunk::contacts()->bulkDelete(['id_1']);
// Poll job status
$status = Plunk::contacts()->bulkStatus($result['jobId']);
// Import from CSV (max 5MB, queued)
$result = Plunk::contacts()->import('/path/to/contacts.csv');
$status = Plunk::contacts()->importStatus($result['jobId']);
// List templates (with pagination and filtering)
$result = Plunk::templates()->list(
search: 'welcome',
type: 'TRANSACTIONAL', // or 'MARKETING'
limit: 50,
);
// Get a single template
$template = Plunk::templates()->get('template_id');
// Create a template
$template = Plunk::templates()->create(
name: 'Welcome Email',
subject: 'Welcome to {{company}}!',
body: '<h1>Hello {{firstName}}</h1>',
type: 'TRANSACTIONAL', // or 'MARKETING'
);
// Update a template (PATCH)
$template = Plunk::templates()->update('template_id',
subject: 'Updated Subject',
);
// Duplicate a template
$copy = Plunk::templates()->duplicate('template_id');
// Check what uses a template
$usage = Plunk::templates()->usage('template_id');
// Delete a template
Plunk::templates()->delete('template_id');
// List all campaigns
$campaigns = Plunk::campaigns()->list();
// Create a campaign (starts in DRAFT)
$result = Plunk::campaigns()->create(
name: 'Product Launch',
subject: 'Exciting news!',
body: '<h1>We launched!</h1>',
from: '[email protected] ',
audienceType: 'ALL', // 'ALL', 'SEGMENT', or 'FILTERED'
segmentId: 'seg_123', // , '[email protected] ');
// Get campaign stats
$stats = Plunk::campaigns()->stats('campaign_id');
// $stats['sent'], $stats['opened'], $stats['clicked'], $stats['bounced']
// Duplicate / Update / Delete
$copy = Plunk::campaigns()->duplicate('campaign_id');
Plunk::campaigns()->update('campaign_id', [...]);
Plunk::campaigns()->delete('campaign_id');
// List all segments
$segments = Plunk::segments()->list();
// Create a segment
$result = Plunk::segments()->create(
name: 'Pro Users',
filters: ['data.plan' => 'pro'],
trackMembership: true,
);
// Get segment members (page-based pagination)
$result = Plunk::segments()->contacts('segment_id', page: 1, pageSize: 100);
// Add/remove members (static segments)
Plunk::segments()->addMembers('segment_id',
emails: ['[email protected] ', '[email protected] '],
createMissing: true,
);
Plunk::segments()->removeMembers('segment_id', ['[email protected] ']);
// Recompute membership (fires entry/exit events)
Plunk::segments()->compute('segment_id');
// Cheap count refresh (no events)
Plunk::segments()->refresh('segment_id');
// Update / Delete
Plunk::segments()->update('segment_id', ['name' => 'Updated Name']);
Plunk::segments()->delete('segment_id');
$verification = Plunk::verifyEmail('[email protected] ');
$verification->valid; // bool — overall result
$verification->email; // string — the email checked
$verification->isDisposable; // bool — is a disposable domain
$verification->isAlias; // bool — is an alias address
$verification->isTypo; // bool — likely contains a typo
$verification->suggestedEmail; // string|null — correction if isTypo is true
$verification->isPlusAddressed; // bool — uses + addressing
$verification->isPersonalEmail; // bool — personal vs business
$verification->domainExists; // bool — domain resolves
$verification->hasWebsite; // bool — domain has a website
$verification->hasMxRecords; // bool — MX records exist
$verification->reasons; // array — human-readable explanations
return [
'secret_key' => env('PLUNK_SECRET_KEY'),
'public_key' => env('PLUNK_PUBLIC_KEY'),
'base_api_url' => env('PLUNK_BASE_API_URL', 'https://next-api.useplunk.com'),
'timeout' => env('PLUNK_TIMEOUT', 30),
'retry' => [
'times' => 3,
'sleep' => 100, // milliseconds
],
];
use NextMigrant\Plunk\Exceptions\AuthenticationException; // 401, 403
use NextMigrant\Plunk\Exceptions\BillingException; // 402
use NextMigrant\Plunk\Exceptions\ConflictException; // 409
use NextMigrant\Plunk\Exceptions\ValidationException; // 422
use NextMigrant\Plunk\Exceptions\RateLimitException; // 429
use NextMigrant\Plunk\Exceptions\PlunkException; // All others
try {
Plunk::transactional()->send(to: $email, subject: 'Hi', body: '<p>Hello</p>');
} catch (AuthenticationException $e) {
// 401/403 — Invalid or missing API key
} catch (BillingException $e) {
// 402 — Billing limit exceeded or upgrade
use Illuminate\Support\Facades\Http;
use NextMigrant\Plunk\Plunk;
Http::fake([
'*/v1/send' => Http::response(['success' => true]),
]);
Plunk::transactional()->send(
to: '[email protected] ',
subject: 'Test',
body: '<p>Hello</p>',
);
Http::assertSent(fn ($request) =>
str_contains($request->url(), '/v1/send')
&& $request['to'] === '[email protected] '
);
bash
php artisan vendor:publish --tag="plunk-config"