Skip to content

Commit 04dcace

Browse files
committed
Add MailerLite driver with configuration and README documentation
1 parent 521ee85 commit 04dcace

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Currently this package support:
2222

2323
- [Mailcoach](https://mailcoach.app) (built by us :-))
2424
- [MailChimp](https://mailchimp.com)
25+
- [MailerLite](https://mailerlite.com)
2526

2627
## Support us
2728

@@ -88,6 +89,8 @@ return [
8889
*
8990
* When using the MailChimp driver, this should be a MailChimp list id.
9091
* http://kb.mailchimp.com/lists/managing-subscribers/find-your-list-id.
92+
*
93+
* When using MailerLite Driver, this should be a MailerLite group ID
9194
*/
9295
'id' => env('NEWSLETTER_LIST_ID'),
9396
],
@@ -119,6 +122,20 @@ Next, you must provide values for the API key and `list.subscribers.id`. You'll
119122

120123
The `endpoint` config value must be set to null.
121124

125+
### Using MailerLite
126+
127+
To use MailerLite, install this extra package.
128+
129+
```bash
130+
composer require mailerlite/mailerlite-php
131+
```
132+
133+
The `driver` key of the `newsletter` config file must be set to `Spatie\Newsletter\Drivers\MailerLiteDriver::class`.
134+
135+
You need to provide the API key and the `group.id`. These can be found in your MailerLite Dashboard under Integrations > API.
136+
137+
The `endpoint` config value must be set to null.
138+
122139
## Usage
123140

124141
After you've installed the package and filled in the values in the config-file working with this package will be a breeze. All the following examples use the facade. Don't forget to import it at the top of your file.
@@ -154,6 +171,11 @@ For MailChimp you can pass merge variables as the second argument:
154171
Newsletter::subscribe('rincewind@discworld.com', ['FNAME'=>'Rince', 'LNAME'=>'Wind']);
155172
```
156173

174+
For MailerLite you can pass subscriber fields as the second argument:
175+
```php
176+
Newsletter::subscribe('rincewind@discworld.com', ['name'=>'Rince', 'last_name'=>'Wind']);
177+
```
178+
157179
You can subscribe someone to a specific list by passing a list name:
158180
```php
159181
Newsletter::subscribe('rincewind@discworld.com', listName: 'subscribers');
@@ -186,6 +208,12 @@ Here's how to unsubscribe someone from a specific list:
186208
Newsletter::unsubscribe('rincewind@discworld.com', 'subscribers');
187209
```
188210

211+
For MailerLite, if you provide the group ID as the second argument, the subscriber will be removed from that group. Otherwise, they will be marked as unsubscribed.
212+
213+
```php
214+
Newsletter::unsubscribe('rincewind@discworld.com', 'subscribers');
215+
```
216+
189217
### Deleting subscribers
190218

191219
Deleting is not the same as unsubscribing. Unlike unsubscribing, deleting a member will result in the loss of all history (add/opt-in/edits) as well as removing them from the list. In most cases, you want to use `unsubscribe` instead of `delete`.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"require-dev": {
3030
"drewm/mailchimp-api": "^2.5",
3131
"guzzlehttp/guzzle": "^7.5|^7.2",
32+
"mailerlite/mailerlite-php": "^1.0",
3233
"mockery/mockery": "^1.4",
3334
"orchestra/testbench": "^7.11|^8.0|^9.0|^10.0",
3435
"pestphp/pest": "^1.20|^2.0|^3.0",

config/newsletter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
*
4141
* When using the MailChimp driver, this should be a MailChimp list id.
4242
* http://kb.mailchimp.com/lists/managing-subscribers/find-your-list-id.
43+
*
44+
* When using MailerLite Driver, this should be a MailerLite group ID
4345
*/
4446
'id' => env('NEWSLETTER_LIST_ID'),
4547
],

src/Drivers/MailerLiteDriver.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace Spatie\Newsletter\Drivers;
4+
5+
use MailerLite\MailerLite;
6+
use Spatie\Newsletter\Support\Lists;
7+
8+
class MailerLiteDriver implements Driver
9+
{
10+
protected MailerLite $mailerLite;
11+
protected Lists $lists;
12+
13+
public static function make(array $arguments, Lists $lists): self
14+
{
15+
return new self($arguments, $lists);
16+
}
17+
18+
public function __construct(array $arguments, Lists $lists)
19+
{
20+
$this->mailerLite = new MailerLite([
21+
'api_key' => $arguments['api_key'] ?? ''
22+
]);
23+
24+
$this->lists = $lists;
25+
}
26+
27+
public function getApi(): MailerLite
28+
{
29+
return $this->mailerLite;
30+
}
31+
32+
public function subscribe(
33+
string $email,
34+
array $properties = [],
35+
string $listName = '',
36+
array $options = [],
37+
bool $unsubscribe = false
38+
): array|false {
39+
$groupId = $this->lists->findByName($listName)->getId();
40+
41+
try {
42+
$response = $this->mailerLite->subscribers->create([
43+
'email' => $email,
44+
'groups' => [$groupId],
45+
'fields' => $properties,
46+
'status' => $unsubscribe ? 'unsubscribed' : null,
47+
]);
48+
49+
return $response['body']['data'] ?? false;
50+
} catch (\Exception $e) {
51+
return false;
52+
}
53+
}
54+
55+
public function subscribeOrUpdate(
56+
string $email,
57+
array $properties = [],
58+
string $listName = '',
59+
array $options = []
60+
): array|false {
61+
return $this->subscribe($email, $properties, $listName, $options);
62+
}
63+
64+
public function unsubscribe(string $email, string $listName = ''): array|false
65+
{
66+
if ($listName) {
67+
$subscriber = $this->getMember($email, $listName);
68+
69+
if ($subscriber) {
70+
$groupId = $this->lists->findByName($listName)->getId();
71+
$this->mailerLite->groups->unAssignSubscriber($groupId, $subscriber['id']);
72+
73+
return $this->getMember($email);
74+
}
75+
76+
return false;
77+
}
78+
79+
return $this->subscribe($email, listName: $listName, unsubscribe: true);
80+
}
81+
82+
public function delete(string $email, string $listName = ''): bool
83+
{
84+
$subscriber = $this->getMember($email, $listName);
85+
86+
if ($subscriber) {
87+
try {
88+
$this->mailerLite->subscribers->delete($subscriber['id']);
89+
return true;
90+
} catch (\Exception $e) {
91+
return false;
92+
}
93+
}
94+
95+
return false;
96+
}
97+
98+
public function getMember(string $email, string $listName = ''): array|false
99+
{
100+
try {
101+
$response = $this->mailerLite->subscribers->find($email);
102+
103+
$groupId = $this->lists->findByName($listName)->getId();
104+
105+
return $this->matchGroup($response, $groupId);
106+
} catch (\Exception $e) {
107+
return false;
108+
}
109+
}
110+
111+
public function hasMember(string $email, string $listName = ''): bool
112+
{
113+
return $this->getMember($email, $listName) !== false;
114+
}
115+
116+
public function isSubscribed(string $email, string $listName = ''): bool
117+
{
118+
$subscriber = $this->getMember($email, $listName);
119+
120+
return $subscriber && ($subscriber['status'] ?? null) === 'active';
121+
}
122+
123+
protected function matchGroup(array $subscriber, string|int $groupId): array|false
124+
{
125+
$groups = $subscriber['body']['data']['groups'] ?? [];
126+
127+
$groupIds = array_column($groups, 'id');
128+
129+
if (in_array($groupId, $groupIds, true)) {
130+
return $subscriber['body']['data'];
131+
}
132+
133+
return false;
134+
}
135+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use MailerLite\MailerLite;
4+
use Spatie\Newsletter\Drivers\MailerLiteDriver;
5+
use Spatie\Newsletter\Facades\Newsletter;
6+
7+
it('can get the MailerLite API', function () {
8+
config()->set('newsletter.driver', MailerLiteDriver::class);
9+
10+
expect(Newsletter::getApi())->toBeInstanceOf(MailerLite::class);
11+
});

0 commit comments

Comments
 (0)