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.
Spatie is webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
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' => '',
'fromName' => '',
'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
],
],
],
],
];
If you specified a listName on a subscription you need to modify that call from
Newsletter::subscribe($email, $listName)
to
Newsletter::subscribe($email, [], $listName)
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 email address can be done like this:
Newsletter::subscribe('[email protected]');
Let's unsubcribe someone:
Newsletter::unsubscribe('[email protected]');
You can pass some merge variables as the second argument:
Newsletter::subscribe('[email protected]', ['firstName'=>'Rince', 'lastName'=>'Wind']);
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('[email protected]', ['firstName'=>'Havelock', 'lastName'=>'Vetinari'], 'mySecondList');
Newsletter::unsubscribe('[email protected]', ['firstName'=>'Sam', 'lastName'=>'Vines'], '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.
Run the tests with:
vendor/bin/phpunit
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
This package was inspired by the Bulk Email Notifications series on Laracasts.
Spatie is webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
The MIT License (MIT). Please see License File for more information.