{
const LIST_ID = '10296433';
+ const ENABLED = false;
+
+ const BATCH = 500;
+
/**
* @throws \Exception
*/
public function handle()
{
+ if (!self::ENABLED) {
+ $this->deleteContacts();
+ return;
+ }
+
$this->syncStatusFromMailjet();
dump('Synced status from mailjet');
}
+ protected function deleteContacts()
+ {
+ $mj = static::_api();
+ $i = 0;
+ $remove = $this->getRecipientsList();
+ $emails=$this->getEmailsFromContactIDs($remove);
+ $contacts = [];
+ foreach ($emails as $email) {
+ $contacts[] = ['Email' => $email];
+ }
+ $this->addContactsToList($contacts, 'remove');
+ }
+
+ protected function getRecipientsList($filter = null)
+ {
+ $i = 0;
+ $res = [];
+ $mj = static::_api();
+
+
+ $filters = ['ContactsList' => static::LIST_ID,
+ 'Limit' => self::BATCH,
+ 'Offset' => 0];
+ if (null !== $filter) {
+ $filters = array_merge($filters, $filter);
+ }
+
+ while (true) {
+ $filters['Offset'] = $i * self::BATCH;
+ $response = $mj->get(Resources::$Listrecipient, ['filters' => $filters]);
+ foreach ($response->getData() as $contact) {
+ $res[] = $contact['ContactID'];
+ }
+
+ if ($response->getCount() < self::BATCH) {
+ break;
+ }
+
+ $i++;
+ }
+ return $res;
+ }
+
+ protected function getEmailsFromContactIDs($contactIDs)
+ {
+ $i = 0;
+ $res = [];
+ $mj = static::_api();
+
+ while (true) {
+ $response = $mj->get(Resources::$Contact, ['filters' => ['ContactsList' => static::LIST_ID,
+ 'Limit' => self::BATCH,
+ 'Offset' => $i * self::BATCH,
+ ]]);
+
+ foreach ($response->getData() as $contact) {
+ if (in_array($contact['ID'], $contactIDs)) {
+ $res[] = $contact['Email'];
+ }
+ }
+ if ($response->getCount() < self::BATCH) {
+ break;
+ }
+ $i++;
+ }
+ return $res;
+ }
+
protected function addContactsToList($contacts, $action = 'addnoforce')
{
if (empty($contacts)) {
{
$mj = static::_api();
$i = 0;
- $batchs = 500;
$unsub = [];
- $unsubEmails = [];
- while (true) {
- $response = $mj->get(Resources::$Listrecipient, ['filters' => ['ContactsList' => static::LIST_ID,
- 'Limit' => $batchs,
- 'Offset' => $i * $batchs,
- 'Unsub' => "true"]]);
+ $timestamp = time() - (3600 * 24 * 90); // 90 days in the past
- foreach ($response->getData() as $contact) {
- $unsub[] = $contact['ContactID'];
- }
-
- if ($response->getCount() < $batchs) {
- break;
- }
- $i++;
- }
+ // Check bounces
while (true) {
- $response = $mj->get(Resources::$Contact, ['filters' => ['ContactsList' => static::LIST_ID,
- 'Limit' => $batchs,
- 'Offset' => $i * $batchs,
+ $response = $mj->get(Resources::$Bouncestatistics, ['filters' => [
+ 'Limit' => self::BATCH,
+ 'Offset' => $i * self::BATCH,
+ 'FromTS' => $timestamp,
]]);
foreach ($response->getData() as $contact) {
- if (in_array($contact['ID'], $unsub)) {
- $unsubEmails[] = $contact['Email'];
+ if ($contact['IsBlocked'] || $contact['IsStatePermanent']) {
+ $unsub[] = $contact['ContactID'];
}
}
- if ($response->getCount() < $batchs) {
+
+ if ($response->getCount() < self::BATCH) {
break;
}
$i++;
}
+
+ // Check unsubscribed users
+ $filters = ['Unsub' => 'true', 'IsExcludedFromCampaigns' => 'true'];
+ foreach ($filters as $filter => $value) {
+ $unsub = array_merge($unsub, $this->getRecipientsList([$filter => $value]));
+ }
+
+ $unsubEmails = $this->getEmailsFromContactIDs($unsub);
DB::table('extranet_users.user')->whereIn('email', $unsubEmails)->update(['marketing' => 0]);
}