PHP code example of spatie / laravel-newsletter

1. Go to this page and download the library: Download spatie/laravel-newsletter 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/ */

    

spatie / laravel-newsletter example snippets


return [

    /*
     * The driver to use to interact with MailChimp API.
     * You may use "log" or "null" to prevent calling the
     * API directly from your environment.
     */
    'driver' => env('NEWSLETTER_DRIVER', Spatie\Newsletter\Drivers\MailcoachDriver::class),

    /**
     * These arguments will be given to the driver.
     */
    'driver_arguments' => [
        'api_key' => env('NEWSLETTER_API_KEY'),

        'endpoint' => env('NEWSLETTER_ENDPOINT'),
    ],

    /*
     * The list name to use when no list name is specified in a method.
     */
    'default_list_name' => 'subscribers',

    'lists' => [

        /*
         * This key is used to identify this list. It can be used
         * as the listName parameter provided in the various methods.
         *
         * You can set it to any string you want and you can add
         * as many lists as you want.
         */
        'subscribers' => [

            /*
             * When using the Mailcoach driver, this should be the Email list UUID
             * which is displayed in the Mailcoach UI
             *
             * When using the MailChimp driver, this should be a MailChimp list id.
             * http://kb.mailchimp.com/lists/managing-subscribers/find-your-list-id.
             */
            'id' => env('NEWSLETTER_LIST_ID'),
        ],
    ],
];

use Spatie\Newsletter\Facades\Newsletter;

use Newsletter;

Newsletter::subscribe('[email protected]');

Newsletter::unsubscribe('[email protected]');

Newsletter::subscribe('[email protected]', ['first_name' => 'Rince', 'last_name' => 'Wind']);

Newsletter::subscribe('[email protected]', ['FNAME'=>'Rince', 'LNAME'=>'Wind']);

Newsletter::subscribe('[email protected]', listName: 'subscribers');

 Newsletter::subscribeOrUpdate('[email protected]', ['first_name' => 'Rince', 'last_name' => 'Wind']);
 

Newsletter::subscribeOrUpdate(
   '[email protected]', 
   ['FNAME'=>'Rince','LNAME'=>'Wind'], 
   'subscribers', 
   ['interests'=>['interestId'=>true, 'interestId'=>true]],
);

Newsletter::unsubscribe('[email protected]', 'subscribers');

Newsletter::delete('[email protected]');

Newsletter::getMember('[email protected]');

Newsletter::hasMember('[email protected]'); //returns a boolean

Newsletter::isSubscribed('[email protected]'); //returns a boolean

$api = Newsletter::getApi();

Newsletter::getApi()->getLastError();
bash
php artisan vendor:publish --tag="newsletter-config"
bash
composer