/laravel-newsletter

Manage newsletters in Laravel 5

Primary LanguagePHPMIT LicenseMIT

Manage newsletters in Laravel 5

Latest Version Software License Build Status SensioLabsInsight Quality Score Total Downloads

This package provides an easy way to integrate email marketing services with Laravel 5. Currently the package only supports MailChimp. In the future more services may get added.

Installation

You can install this package via composer using:

composer require spatie/laravel-newsletter

You must also install this service provider.

// config/app.php
'providers' => [
    ...
    'Spatie\Newsletter\NewsletterServiceProvider',
    ...
];

If you want to make use of the facade you must install it as well.

// config/app.php
'aliases' => [
    ...
    'Newsletter' => 'Spatie\Newsletter\NewsletterFacade',
];

To publish the config file to app/config/laravel-newsletter.php run:

php artisan vendor:publish --provider="Spatie\Newsletter\NewsletterServiceProvider"

This wil publish a file laravel-newsletter.php in your config directory with the following contents:

return [

    'mailChimp' => [

        /*
         * The api key of a MailChimp account. You can find yours here:
         * https://us10.admin.mailchimp.com/account/api-key-popup/
         */
        'apiKey' => env('MAILCHIMP_APIKEY'),

        /*
         * Here you can define properties of the lists you want to
         * send campaigns.
         */
        'lists' => [

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

                /*
                 * A mail chimp list id. Check the mailchimp docs if you don't know
                 * how to get this value:
                 * http://kb.mailchimp.com/lists/managing-subscribers/find-your-list-id
                 */
                'id' => '',

                /*
                 * These values will be used when creating a new campaign.
                 */
                'createCampaign' => [
                    'fromEmail' => 'newsletter@coffeewithcats.com',
                    'fromName' => 'coffee with cats',
                    'toName' => ''
                ],
                /*
                 * These values will be used when subscribing to a list
                 */
                'subscribe' => [
                    'emailType' => 'html',
                    'requireDoubleOptin' => false,
                    'updateExistingUser' => false
                ],
                /*
                 * These values will be used when unsubscribing from a list
                 */
                'unsubscribe' => [
                    'deletePermanently' => false,
                    'sendGoodbyeEmail' => false,
                    'sendUnsubscribeEmail' => false
                ],
            ],
        ],
    ],
];

Usage

After you've installed the package and filled in the values in the config-file working with this package will be a breeze.

Subscribing an e-mailadress can be done like this:

Newsletter::subscribe('rincewind@discworld.com');

Let's unsubcribe someone:

Newsletter::unsubscribe('the.luggage@discworld.com');

This is how you create a campaign:

$subject = 'The Truth newsletter';
$contents = '<h1>Big news</h1>The world is carried by four elephants on a turtle!';

Newsletter::createCampaign($subject, $contents);

The method will create a campaign, but not send it.

If you have multiple lists defined in the config file you must pass the name of the list an extra parameter:

Newsletter::subscribe('havelock.vetinari@discworld.com', 'mySecondList');
Newsletter::unsubscribe('sam.vimes@discworld.com', 'mySecondList');

Newsletter::createCampaign($subject, $contents, 'mySecondList);

If you need more functionality you get an instance of the underlying service api with:

$api = Newsletter::getApi();

As this package currently only supports MailChimp this method will always return an instance of the MailChimp API.

Testing

Run the tests with:

vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Credits

This package was inspired by the Bulk Email Notifications series on Laracasts.

License

The MIT License (MIT). Please see License File for more information.