feat: create Mailjet lists for travel mailings

This commit is contained in:
fromme committed 2026-11-04 15:32:50 +01:00
1 parent d63fa10a56
commit c16f368293
15 files changed
+851 -1

No files matched your search

+147 -1
View File
@@ -12,6 +12,7 @@ class ApiClient
{
private const string DEFAULT_BASE_URL = 'https://api.mailjet.com/v3/REST';
private const int NAME_MAX_LENGTH = 255;
private const int PAGE_LIMIT = 1000;
/**
* @param array{firstName?: string, lastName?: string} $contactMetadataFields
@@ -141,6 +142,139 @@ class ApiClient
]);
}
public function createContactsList(string $name): int
{
$this->assertConfigured();
$response = $this->request('POST', 'contactslist', [
'json' => [
'Name' => $name,
],
]);
$listId = $response['Data'][0]['ID'] ?? null;
if (null === $listId) {
throw new NewsletterProviderException(sprintf('Mailjet did not return an id for the new contact list %s', $name));
}
return (int) $listId;
}
/**
* Adds the contacts as subscribed, including any who unsubscribed from this list before.
* Mailjet processes the request as an asynchronous job.
*
* @param list<array{email: string, firstName: ?string}> $contacts
*
* @return int|null the job id
*/
public function addContactsToList(int $listId, array $contacts): ?int
{
$this->assertConfigured();
$payload = [];
foreach ($contacts as $contact) {
$entry = ['Email' => $this->normalizeEmail($contact['email'])];
$firstName = $this->normalizeName($contact['firstName']);
if (null !== $firstName && true === isset($this->contactMetadataFields['firstName'])) {
$entry['Properties'] = [$this->contactMetadataFields['firstName'] => $firstName];
}
$payload[] = $entry;
}
$response = $this->request('POST', sprintf('contactslist/%d/managemanycontacts', $listId), [
'json' => [
'Action' => 'addforce',
'Contacts' => $payload,
],
]);
$jobId = $response['Data'][0]['JobID'] ?? null;
return null !== $jobId ? (int) $jobId : null;
}
/**
* @return list<int>
*/
public function getListContactIds(int $listId): array
{
$this->assertConfigured();
$contactIds = [];
$offset = 0;
do {
$response = $this->request('GET', 'listrecipient', [
'query' => [
'ContactsList' => $listId,
'Limit' => self::PAGE_LIMIT,
'Offset' => $offset,
],
]);
$entries = true === is_array($response['Data'] ?? null) ? $response['Data'] : [];
foreach ($entries as $entry) {
if (true === is_array($entry) && true === isset($entry['ContactID'])) {
$contactIds[] = (int) $entry['ContactID'];
}
}
$offset += self::PAGE_LIMIT;
} while (self::PAGE_LIMIT === count($entries));
return array_values(array_unique($contactIds));
}
/**
* Lists the contact is on, subscribed or not. Mailjet includes lists deleted within the last 60 days.
*
* @return list<int>
*/
public function getContactListIds(int $contactId): array
{
$this->assertConfigured();
$response = $this->request('GET', sprintf('contact/%d/getcontactslists', $contactId), [
'allow_404' => true,
]);
$listIds = [];
foreach ($response['Data'] ?? [] as $entry) {
if (true === is_array($entry) && true === isset($entry['ListID'])) {
$listIds[] = (int) $entry['ListID'];
}
}
return $listIds;
}
public function deleteContactsList(int $listId): void
{
$this->assertConfigured();
$this->request('DELETE', sprintf('contactslist/%d', $listId), [
'allow_404' => true,
'allow_empty' => true,
]);
}
/**
* GDPR deletion: Mailjet anonymises the contact immediately and purges it after 30 days.
* This cannot be undone.
*/
public function deleteContact(int $contactId): void
{
$this->assertConfigured();
$this->request('DELETE', sprintf('contacts/%d', $contactId), [
'allow_404' => true,
'allow_empty' => true,
'api_version' => 'v4',
]);
}
private function findOrCreateContactId(string $email): int
{
$contactId = $this->resolveContactId($email);
@@ -246,15 +380,17 @@ class ApiClient
{
$allow404 = true === ($options['allow_404'] ?? false);
$allowEmpty = true === ($options['allow_empty'] ?? false);
$baseUrl = 'v4' === ($options['api_version'] ?? null) ? $this->getV4BaseUrl() : $this->getBaseUrl();
unset($options['allow_404']);
unset($options['allow_empty']);
unset($options['api_version']);
$resourcePath = trim($resource, '/');
try {
$response = $this->httpClient->request(
$method,
sprintf('%s/%s', $this->getBaseUrl(), $resourcePath),
sprintf('%s/%s', $baseUrl, $resourcePath),
array_merge($options, [
'auth_basic' => sprintf('%s:%s', $this->apiKey, $this->apiSecret),
])
@@ -310,6 +446,16 @@ class ApiClient
return rtrim($baseUrl, '/');
}
/**
* The configured base URL points at v3 (`…/v3/REST`); the v4 API sits beside it on the same host.
*/
private function getV4BaseUrl(): string
{
$baseUrl = preg_replace('~/v3/REST$~i', '', $this->getBaseUrl());
return sprintf('%s/v4', $baseUrl);
}
private function assertConfigured(): void
{
if (true === empty($this->apiKey) || true === empty($this->apiSecret)) {