diff --git a/.env.example b/.env.example index 963fddd..1597a5c 100644 --- a/.env.example +++ b/.env.example @@ -49,3 +49,7 @@ SAML2_SP_CERT_x509= SAML2_SP_CERT_PRIVATEKEY= SAML2_CONTACT_TECHNICAL_EMAIL= SAML2_CONTACT_SUPPORT_EMAIL= + +# MJ - API +MJ_APIKEY_PUBLIC= +MJ_APIKEY_PRIVATE= diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 7efb0d7..09c6420 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -28,6 +28,7 @@ class Kernel extends ConsoleKernel // @codeCoverageIgnoreStart $schedule->job(StartStatJobs::class)->dailyAt('03:30'); $schedule->job(SendTransferredToDigitalCountMail::class)->weekdays()->at('07:00'); + $schedule->job(SyncEmailOptOuts::class)->dailyAt('00:00'); $schedule->command(DeleteUnconfirmedCustomers::class)->dailyAt('05:30'); // @codeCoverageIgnoreEnd } diff --git a/app/Http/Requests/Bo/Brands/StoreBrandRequest.php b/app/Http/Requests/Bo/Brands/StoreBrandRequest.php index b1b81c0..eca4c1e 100644 --- a/app/Http/Requests/Bo/Brands/StoreBrandRequest.php +++ b/app/Http/Requests/Bo/Brands/StoreBrandRequest.php @@ -37,10 +37,11 @@ class StoreBrandRequest extends FormRequest public function rules(): array { $rules = [ - 'name' => 'required|unique:brands,name|string|min:5|max:255', - 'url' => 'required|url|min:5|max:255', - 'url_claim' => 'required|url|min:5|max:255', - 'domain' => 'required|unique:brands,domain|string|min:5|max:255', + 'name' => 'required|unique:brands,name|string|min:5|max:255', + 'url' => 'required|url|min:5|max:255', + 'url_claim' => 'required|url|min:5|max:255', + 'domain' => 'required|unique:brands,domain|string|min:5|max:255', + 'newsletter_email' => 'nullable|email:rfc,strict,dns,spoof,filter', ]; return \array_merge( $rules, @@ -56,11 +57,12 @@ class StoreBrandRequest extends FormRequest public function attributes(): array { return [ - 'name' => trans('validation.attributes.name'), - 'logo' => trans('validation.custom.logo'), - 'url' => trans('validation.custom.url'), - 'url_claim' => trans('validation.custom.url_claim'), - 'domain' => trans('validation.custom.domain'), + 'name' => trans('validation.attributes.name'), + 'logo' => trans('validation.custom.logo'), + 'url' => trans('validation.custom.url'), + 'url_claim' => trans('validation.custom.url_claim'), + 'domain' => trans('validation.custom.domain'), + 'newsletter_email' => trans('validation.custom.newsletter_email'), ]; } } diff --git a/app/Jobs/SyncEmailOptOuts.php b/app/Jobs/SyncEmailOptOuts.php new file mode 100644 index 0000000..6223d0f --- /dev/null +++ b/app/Jobs/SyncEmailOptOuts.php @@ -0,0 +1,58 @@ +where('newsletter_email', $campaign['FromEmail'])->first(); + $unsubscribedContacts = MailjetHelper::getUnsubscribedContacts($contactListId, Carbon::now()->subDay()); + + if (empty($unsubscribedContacts)) { + continue; + } + + foreach ($unsubscribedContacts as $unsubscribedContact) { + $contact = MailjetHelper::getContact($unsubscribedContact['ContactID']); + $crmCustomer = CustomerInfo::getFromEmail($brand, $contact[0]["Email"])->first(); + + if ($crmCustomer) { + $crmCustomer->opt_in = false; + + if (!$crmCustomer->saveOrFail()) { + throw new \Exception(sprintf('Optin status not changed for customer (%s) and brand (%d).', $crmCustomer->getKey())); + } + } else { + $singleOptin = SingleOptin::query()->where('email', $contact[0]['Email']) + ->whereHas('brand', function ($q) use ($brand) { + $q->whereId($brand->getKey()); + })->first(); + + if ($singleOptin) { + if (!$singleOptin->delete()) { + throw new \Exception(sprintf('SingleOptin (%s) not deleted for brand (%d).', $singleOptin->hashcode, $brand->name)); + } + } + } + } + } + } +} diff --git a/app/Lib/Helpers/MailjetHelper.php b/app/Lib/Helpers/MailjetHelper.php new file mode 100644 index 0000000..1c02351 --- /dev/null +++ b/app/Lib/Helpers/MailjetHelper.php @@ -0,0 +1,94 @@ + 1000 + ]; + + if (!is_null($fromTimestamp)) { + $filters = array_merge( + $filters, + [ + 'FromTS' => $fromTimestamp->toRfc3339String() + ] + ); + } + + $response = app('mailjetClient')->get(Resources::$Campaign, [ + '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 []; + } + } +} diff --git a/app/Providers/MailjetServiceProvider.php b/app/Providers/MailjetServiceProvider.php new file mode 100644 index 0000000..681d38f --- /dev/null +++ b/app/Providers/MailjetServiceProvider.php @@ -0,0 +1,34 @@ + 'v3', + ] + ); + + $mailjetClient->setConnectionTimeout(200); + $mailjetClient->setTimeout(200); + + return $mailjetClient; + }); + } +} diff --git a/composer.json b/composer.json index 4344564..71d0ad4 100644 --- a/composer.json +++ b/composer.json @@ -37,6 +37,7 @@ "laravel/tinker": "^2.8", "laravel/ui": "^4.2", "league/csv": "^9.7", + "mailjet/mailjet-apiv3-php": "^1.6", "mews/purifier": "^3.4", "propaganistas/laravel-phone": "^5.0", "sentry/sentry-laravel": "^4.10", diff --git a/composer.lock b/composer.lock index 16c22e5..7727b19 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1417f675b37867e72bdd34d5b0fcf629", + "content-hash": "70aad663cba0f4125a01bc8e53999abc", "packages": [ { "name": "24slides/laravel-saml2", @@ -3562,6 +3562,67 @@ ], "time": "2024-12-08T08:18:47+00:00" }, + { + "name": "mailjet/mailjet-apiv3-php", + "version": "v1.6.6", + "source": { + "type": "git", + "url": "https://github.com/mailjet/mailjet-apiv3-php.git", + "reference": "9d5cea25f347719d7df7909fb4a43a72bd4e5011" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mailjet/mailjet-apiv3-php/zipball/9d5cea25f347719d7df7909fb4a43a72bd4e5011", + "reference": "9d5cea25f347719d7df7909fb4a43a72bd4e5011", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/guzzle": "^7.4.4", + "php": "^8.1", + "psr/http-client": "^1.0", + "symfony/validator": "^6.3|^7.0" + }, + "require-dev": { + "mockery/mockery": "^1.4", + "php-coveralls/php-coveralls": "^2.0", + "phpcompatibility/php-compatibility": "*", + "phpstan/phpstan": "^2.0", + "phpunit/phpunit": "^11.4.0", + "squizlabs/php_codesniffer": "*" + }, + "type": "library", + "autoload": { + "psr-0": { + "Mailjet": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mailjet", + "email": "dev@mailjet.com", + "homepage": "https://dev.mailjet.com" + } + ], + "description": "PHP wrapper for the Mailjet API", + "homepage": "https://github.com/mailjet/mailjet-apiv3-php/", + "keywords": [ + "Mailjet", + "api", + "email", + "php", + "v3" + ], + "support": { + "issues": "https://github.com/mailjet/mailjet-apiv3-php/issues", + "source": "https://github.com/mailjet/mailjet-apiv3-php/tree/v1.6.6" + }, + "time": "2025-12-24T15:55:48+00:00" + }, { "name": "masterminds/html5", "version": "2.10.0", @@ -7953,6 +8014,110 @@ ], "time": "2024-09-25T14:21:43+00:00" }, + { + "name": "symfony/validator", + "version": "v7.4.8", + "source": { + "type": "git", + "url": "https://github.com/symfony/validator.git", + "reference": "8f73cbddae916756f319b3e195088da216f0f12f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/validator/zipball/8f73cbddae916756f319b3e195088da216f0f12f", + "reference": "8f73cbddae916756f319b3e195088da216f0f12f", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "symfony/deprecation-contracts": "^2.5|^3", + "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-mbstring": "~1.0", + "symfony/polyfill-php83": "^1.27", + "symfony/translation-contracts": "^2.5|^3" + }, + "conflict": { + "doctrine/lexer": "<1.1", + "symfony/dependency-injection": "<6.4", + "symfony/doctrine-bridge": "<7.0", + "symfony/expression-language": "<6.4", + "symfony/http-kernel": "<6.4", + "symfony/intl": "<6.4", + "symfony/property-info": "<6.4", + "symfony/translation": "<6.4.3|>=7.0,<7.0.3", + "symfony/var-exporter": "<6.4.25|>=7.0,<7.3.3", + "symfony/yaml": "<6.4" + }, + "require-dev": { + "egulias/email-validator": "^2.1.10|^3|^4", + "symfony/cache": "^6.4|^7.0|^8.0", + "symfony/config": "^6.4|^7.0|^8.0", + "symfony/console": "^6.4|^7.0|^8.0", + "symfony/dependency-injection": "^6.4|^7.0|^8.0", + "symfony/expression-language": "^6.4|^7.0|^8.0", + "symfony/finder": "^6.4|^7.0|^8.0", + "symfony/http-client": "^6.4|^7.0|^8.0", + "symfony/http-foundation": "^6.4|^7.0|^8.0", + "symfony/http-kernel": "^6.4|^7.0|^8.0", + "symfony/intl": "^6.4|^7.0|^8.0", + "symfony/mime": "^6.4|^7.0|^8.0", + "symfony/process": "^6.4|^7.0|^8.0", + "symfony/property-access": "^6.4|^7.0|^8.0", + "symfony/property-info": "^6.4|^7.0|^8.0", + "symfony/string": "^6.4|^7.0|^8.0", + "symfony/translation": "^6.4.3|^7.0.3|^8.0", + "symfony/type-info": "^7.1.8", + "symfony/yaml": "^6.4|^7.0|^8.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Validator\\": "" + }, + "exclude-from-classmap": [ + "/Tests/", + "/Resources/bin/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides tools to validate values", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/validator/tree/v7.4.8" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2026-03-30T12:55:43+00:00" + }, { "name": "symfony/var-dumper", "version": "v7.2.0", diff --git a/config/app.php b/config/app.php index 6072907..e48c6ca 100644 --- a/config/app.php +++ b/config/app.php @@ -196,6 +196,7 @@ return [ App\Providers\RouteServiceProvider::class, App\Providers\TranslationServiceProvider::class, App\Providers\ViewServiceProvider::class, + App\Providers\MailjetServiceProvider::class, ], /* diff --git a/config/mailjet.php b/config/mailjet.php new file mode 100644 index 0000000..a39ca6d --- /dev/null +++ b/config/mailjet.php @@ -0,0 +1,6 @@ + env('MJ_APIKEY_PUBLIC'), + 'secret' => env('MJ_APIKEY_PRIVATE'), +]; diff --git a/manifest.yaml b/manifest.yaml index f47b04e..0086d4c 100644 --- a/manifest.yaml +++ b/manifest.yaml @@ -32,6 +32,8 @@ data: SAML2_CONTACT_TECHNICAL_EMAIL: "${APP_SAML2_CONTACT_TECHNICAL_EMAIL}" SAML2_CONTACT_SUPPORT_EMAIL: "${APP_SAML2_CONTACT_SUPPORT_EMAIL}" UWIL_ADMIN_SECURE_CIDR_LIST: "${APP_GLOBAL_UWIL_ADMIN_SECURE_CIDR_LIST}" + MJ_APIKEY_PUBLIC: "${APP_MJ_APIKEY_PUBLIC}" + MJ_APIKEY_PRIVATE: "${APP_MJ_APIKEY_PRIVATE}" --- apiVersion: v1 @@ -82,7 +84,12 @@ spec: lifecycle: preStop: exec: - command: ["/bin/sh","-c","kill -s TERM 1; while test $(ps ax | grep -v grep | grep bash | wc -l) -gt 0; do sleep 2s; done"] + command: + [ + "/bin/sh", + "-c", + "kill -s TERM 1; while test $(ps ax | grep -v grep | grep bash | wc -l) -gt 0; do sleep 2s; done", + ] resources: limits: cpu: "${APP_CI_CPU_LIMIT}"