151 lines
3.5 KiB
PHP
151 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Lib\Helpers;
|
|
|
|
use Mailjet\Resources;
|
|
|
|
class MailjetHelper
|
|
{
|
|
/**
|
|
* Retrieves contact lists
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function getContactLists(): array
|
|
{
|
|
$filters = [
|
|
'Limit' => 1000,
|
|
'Sort' => 'CreatedAt DESC',
|
|
];
|
|
|
|
$response = app('mailjetClient')->get(Resources::$Contactslist, [
|
|
'filters' => $filters
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get unsubscribed contacts for a given contact list
|
|
*
|
|
* @param integer $contactListId
|
|
* @param \Carbon\Carbon|null $lastActivity
|
|
* @return array
|
|
*/
|
|
public static function getUnsubscribedContacts(int $contactListId, \Carbon\Carbon|null $lastActivity = null): array
|
|
{
|
|
$filters = [
|
|
'ContactsList' => $contactListId,
|
|
'Unsub' => true,
|
|
'Limit' => 1000,
|
|
];
|
|
|
|
if (!is_null($lastActivity)) {
|
|
$filters = array_merge(
|
|
$filters,
|
|
[
|
|
'LastActivityAt' => $lastActivity->toRfc3339String()
|
|
]
|
|
);
|
|
}
|
|
|
|
$response = app('mailjetClient')->get(Resources::$Listrecipient, [
|
|
'filters' => $filters
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get contact for a given contact id
|
|
*
|
|
* @param integer $contactId
|
|
* @return array
|
|
*/
|
|
public static function getContact(int $contactId): array
|
|
{
|
|
$response = app('mailjetClient')->get(Resources::$Contact, [
|
|
'id' => $contactId
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Creates a new Mailjet contact list
|
|
*
|
|
* @param string $listName
|
|
* @return array
|
|
*/
|
|
public static function createContactList(string $listName): array
|
|
{
|
|
$response = app('mailjetClient')->post(Resources::$Contactslist, [
|
|
'body' => [
|
|
'Name' => $listName,
|
|
]
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Uploads a CSV file
|
|
*
|
|
* @param integer $mailjetListId
|
|
* @param string $csvFile
|
|
* @return array
|
|
*/
|
|
public static function uploadCsvFile(int $mailjetListId, string $csvFile): array
|
|
{
|
|
$response = app('mailjetClient')->post(Resources::$ContactslistCsvdata, [
|
|
'id' => $mailjetListId,
|
|
'body' => $csvFile,
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Imports contacts into a Mailjet contact list
|
|
*
|
|
* @param integer $listId
|
|
* @param integer $dataId
|
|
* @return array
|
|
*/
|
|
public static function importContacts(int $listId, int $dataId): array
|
|
{
|
|
$response = app('mailjetClient')->post(Resources::$Csvimport, [
|
|
'body' => [
|
|
'ContactsListID' => $listId,
|
|
'DataID' => $dataId,
|
|
'Method' => "addnoforce"
|
|
]
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
}
|