PHP code example of ucscode / php-listmonk

1. Go to this page and download the library: Download ucscode/php-listmonk 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/ */

    

ucscode / php-listmonk example snippets


use Junisan\ListmonkApi\API\ListmonkPHP;

$api = new ListmonkPHP('https://listmonk-domain.com/api', [
    'username' => 'api-username',
    'password' => 'api-token',
]);

$subscriberApi = $api->getSubscribersApi();
$listApi       = $api->getListsApi();
$campaignApi   = $api->getCampaignsApi();

use Junisan\ListmonkApi\Models\SubscriberModel;
use Junisan\ListmonkApi\Models\SubscriberAttributesModel;

$model = (new SubscriberModel())
    ->setName('Example')
    ->setEmail('[email protected]')
    ->setStatus('enabled') // enabled, disabled, etc.
    ->setLists([]) // optional: list IDs
    ->setAttributes(new SubscriberAttributesModel()); // optional

$subscriber = $subscriberApi->createSubscriber($model);

$subscriberApi->createSubscriber($model, true); // preconfirmed subscriptions

$subscriber = $subscriberApi->getSubscriberByEmail('[email protected]');

$subscriber = $subscriberApi->getSubscriberById(1);

$paginator = $subscriberApi->getAllSubscriber(1, 100);

$subscribers = $paginator->getResults();
$total       = $paginator->getTotal();

use Junisan\ListmonkApi\Models\ListModel;

$list = (new ListModel())
    ->setName('Newsletter')
    ->setDescription('Main newsletter list')
    ->setIsPublic(true)
    ->setOptinSingle(true)
    ->setIsActive(true)
    ->setTags(['marketing', 'weekly']);

$createdList = $listApi->createList($list);

$paginator = $listApi->getAllLists(1, 100);

$list = $listApi->getListById(1);

use Junisan\ListmonkApi\Models\CampaignModel;

$campaign = (new CampaignModel())
    ->setName('January Campaign')
    ->setSubject('Welcome to our newsletter')
    ->setBody('<h1>Hello World</h1>')
    ->setType('regular')
    ->setContentType('html')
    ->setStatus('draft')
    ->setListIds([1, 2])
    ->setTags(['monthly']);

$createdCampaign = $campaignApi->createCampaign($campaign);

$paginator = $campaignApi->getAllCampaigns(1, 100);

$campaign = $campaignApi->getCampaignById(1);

$updated = $campaignApi->changeCampaignStatus(1, 'running');

$htmlPreview = $campaignApi->previewCampaign(1);
bash
composer