Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The settings are saved on the form itself, so they are version controlled and de

## How it works

When a form submission is created, a listener checks the form's MailerLite settings. If syncing is enabled and a field mapping is present, a queued job sends the subscriber — with the mapped fields and selected group — to MailerLite. Make sure a queue worker is running (or use the `sync` queue driver) for subscribers to be created.
When a form submission is created, a listener checks the form's MailerLite settings. If syncing is enabled and a field mapping is present, a queued job sends the subscriber — with the mapped fields, the selected group, and the submitter's IP address (which MailerLite uses as a consent and spam signal) — to MailerLite. Make sure a queue worker is running (or use the `sync` queue driver) for subscribers to be created.

## Testing

Expand Down
5 changes: 5 additions & 0 deletions src/Jobs/CreateSubscriberJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(
private array $fieldMapping,
private array $subscriberGroups,
private string $apiKey,
private ?string $ipAddress = null,
) {}

public function handle(): void
Expand Down Expand Up @@ -49,6 +50,10 @@ public function handle(): void

$data = ['email' => $email];

if (filled($this->ipAddress)) {
$data['ip_address'] = $this->ipAddress;
}

if (! empty($fields)) {
$data['fields'] = $fields;
}
Expand Down
1 change: 1 addition & 0 deletions src/Listeners/SubmissionCreatedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function handle(SubmissionCreated $event): void
fieldMapping: array_values($fieldMapping),
subscriberGroups: filled($group) ? Arr::wrap($group) : [],
apiKey: $apiKey,
ipAddress: request()->ip(),
));
}
}
18 changes: 18 additions & 0 deletions tests/Feature/SubmissionCreatedListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ function jobProperty(CreateSubscriberJob $job, string $name): mixed
});
});

it('passes the submitter ip address to the job', function () {
fakeApiKey('key-123');

request()->server->set('REMOTE_ADDR', '203.0.113.7');

$event = submissionEvent([
'mailerlite_enabled' => true,
'mailerlite_field_mapping' => [['form_field' => 'email', 'mailerlite_field' => 'email']],
], ['email' => 'a@b.com']);

(new SubmissionCreatedListener)->handle($event);

Queue::assertPushed(
CreateSubscriberJob::class,
fn (CreateSubscriberJob $job) => jobProperty($job, 'ipAddress') === '203.0.113.7',
);
});

it('wraps a single selected group into an array', function () {
fakeApiKey('key-123');

Expand Down