├── .github ├── FUNDING.yml └── dependabot.yml ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Contracts ├── CampaignContract.php ├── CampaignDraftContract.php ├── ContactMetadataContract.php ├── ContactsListContract.php ├── EventCallbackUrlContract.php ├── MailjetServiceContract.php └── TemplateServiceContract.php ├── Exception └── MailjetException.php ├── Facades └── Mailjet.php ├── MailjetServiceProvider.php ├── Model ├── Campaign.php ├── CampaignDraft.php ├── Contact.php ├── ContactMetadata.php ├── ContactsList.php ├── EventCallbackUrl.php ├── Model.php ├── Requestable.php └── Template.php ├── Providers ├── CampaignDraftServiceProvider.php ├── CampaignServiceProvider.php ├── ContactMetadataServiceProvider.php ├── ContactsListServiceProvider.php ├── EventCallbackUrlServiceProvider.php ├── MailjetClientServiceProvider.php └── TemplateServiceProvider.php └── Services ├── CampaignDraftService.php ├── CampaignService.php ├── ContactMetadataService.php ├── ContactsListService.php ├── EventCallbackUrlService.php ├── MailjetService.php └── TemplateService.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [thedoctor0] 2 | custom: ["https://www.paypal.me/thedoctor0"] 3 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: composer 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Mailjet 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Mailjet Driver 2 | 3 | [![Build Status](https://travis-ci.org/TheDoctor0/laravel-mailjet-driver.svg?branch=master)](https://travis-ci.org/TheDoctor0/laravel-mailjet-driver) 4 | [![Packagist](https://img.shields.io/packagist/v/TheDoctor0/laravel-mailjet-driver.svg)](https://packagist.org/packages/TheDoctor0/laravel-mailjet-driver) 5 | [![Packagist](https://img.shields.io/packagist/dt/TheDoctor0/laravel-mailjet-driver.svg)](https://packagist.org/packages/TheDoctor0/laravel-mailjet-driver) 6 | [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/TheDoctor0/laravel-mailjet-driver/blob/master/LICENSE.md) 7 | 8 | Laravel mail driver package for [Mailjet](https://www.mailjet.com/). It also serves as a wrapper for [Mailjet API v3](https://github.com/mailjet/mailjet-apiv3-php). 9 | 10 | ## Installation 11 | 12 | For Laravel 9.x and 10.x which also requires Symfony Mailer: 13 | ``` 14 | composer require thedoctor0/laravel-mailjet-driver symfony/http-client 15 | ``` 16 | 17 | In other cases: 18 | ``` 19 | composer require thedoctor0/laravel-mailjet-driver:1.0.4 20 | ``` 21 | 22 | ## Configuration 23 | 24 | You can find your Mailjet API key / secret [here](https://app.mailjet.com/account/api_keys). 25 | 26 | Change default mail driver and add new variables to your **.env** file: 27 | 28 | ```php 29 | MAIL_DRIVER=mailjet 30 | 31 | MAILJET_APIKEY=YOUR_APIKEY 32 | MAILJET_APISECRET=YOUR_APISECRET 33 | ``` 34 | 35 | Add section to the **config/services.php** file: 36 | 37 | ```php 38 | 'mailjet' => [ 39 | 'key' => env('MAILJET_APIKEY'), 40 | 'secret' => env('MAILJET_APISECRET'), 41 | ], 42 | ``` 43 | 44 | Make sure that in **config/mail.php** as mail sender address you are using an authorised email address configured on your Mailjet account. 45 | 46 | Your available Mailjet email addresses and domains can be managed [here](https://app.mailjet.com/account/sender). 47 | 48 | For Laravel 7+ you also need to specify new available mail driver in **config/mail.php**: 49 | 50 | ```php 51 | 'mailers' => [ 52 | ... 53 | 54 | 'mailjet' => [ 55 | 'transport' => 'mailjet', 56 | ], 57 | ], 58 | ``` 59 | 60 | ### Optional configuration 61 | 62 | You can add full configuration for [MailjetClient](https://github.com/mailjet/mailjet-apiv3-php) to the **config/services.php** file. 63 | 64 | * `transactional`: settings to sendAPI client 65 | * `common`: setting to MailjetClient accessible through the Facade Mailjet 66 | * `v4`: setting used for some DataProvider`s 67 | 68 | ```php 69 | 'mailjet' => [ 70 | 'key' => env('MAILJET_APIKEY'), 71 | 'secret' => env('MAILJET_APISECRET'), 72 | 'transactional' => [ 73 | 'call' => true, 74 | 'options' => [ 75 | 'url' => 'api.mailjet.com', 76 | 'version' => 'v3.1', 77 | 'call' => true, 78 | 'secured' => true 79 | ], 80 | ], 81 | 'common' => [ 82 | 'call' => true, 83 | 'options' => [ 84 | 'url' => 'api.mailjet.com', 85 | 'version' => 'v3', 86 | 'call' => true, 87 | 'secured' => true 88 | ], 89 | ], 90 | 'v4' => [ 91 | 'call' => true, 92 | 'options' => [ 93 | 'url' => 'api.mailjet.com', 94 | 'version' => 'v4', 95 | 'call' => true, 96 | 'secured' => true 97 | ] 98 | ], 99 | ], 100 | ``` 101 | 102 | ## API Wrapper usage 103 | 104 | In order to API wrapper from this package, you first need to import Mailjet Facade in your code: 105 | ``` 106 | use Mailjet\LaravelMailjet\Facades\Mailjet; 107 | ``` 108 | 109 | Then you can use one of the methods available in the **MailjetServices** class. 110 | 111 | #### Low level API methods: 112 | 113 | * `Mailjet::get($resource, $args, $options)` 114 | * `Mailjet::post($resource, $args, $options)` 115 | * `Mailjet::put($resource, $args, $options)` 116 | * `Mailjet::delete($resource, $args, $options)` 117 | 118 | #### High level API methods: 119 | 120 | * `Mailjet::getAllLists($filters)` 121 | * `Mailjet::createList($body)` 122 | * `Mailjet::getListRecipients($filters)` 123 | * `Mailjet::getSingleContact($id)` 124 | * `Mailjet::createContact($body)` 125 | * `Mailjet::createListRecipient($body)` 126 | * `Mailjet::editListrecipient($id, $body)` 127 | 128 | All method return `Mailjet\Response` or throw a `MailjetException` in case of any API error. 129 | 130 | You can also get the Mailjet API client with the method `getClient()` and make your own custom request to Mailjet API. 131 | 132 | For more information please refer to the official [Mailjet API documentation](https://dev.mailjet.com/email/reference/). 133 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "thedoctor0/laravel-mailjet-driver", 3 | "description": "Laravel mail driver package for Mailjet and wrapper for its API", 4 | "keywords": [ 5 | "framework", 6 | "laravel", 7 | "mailjet", 8 | "wrapper", 9 | "transport", 10 | "mailjet API", 11 | "driver" 12 | ], 13 | "license": "MIT", 14 | "type": "library", 15 | "homepage": "https://github.com/TheDoctor0/laravel-mailjet-driver", 16 | "authors": [ 17 | { 18 | "name": "Dawid Janik", 19 | "role": "Maintainer", 20 | "email": "dawid.janik95@gmail.com", 21 | "homepage": "https://github.com/TheDoctor0" 22 | }, 23 | { 24 | "name": "Gaetan Delbart", 25 | "email": "gaetan@moltencore.io", 26 | "homepage": "https://github.com/gagaXD" 27 | }, 28 | { 29 | "name": "Titouan Benoit", 30 | "email": "titouan.benoit@gmx.fr", 31 | "homepage": "https://github.com/Nightbr" 32 | }, 33 | { 34 | "name": "Mailjet API", 35 | "email": "api@mailjet.com", 36 | "homepage": "https://dev.mailjet.com/" 37 | } 38 | ], 39 | "require": { 40 | "laravel/framework": "~5.5|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0", 41 | "mailjet/mailjet-apiv3-php": "^1.5.6|^1.5", 42 | "symfony/mailjet-mailer": "^6.0|^7.0" 43 | }, 44 | "require-dev": { 45 | "fzaninotto/faker": "~1.9|^1.5", 46 | "mockery/mockery": "~1.3", 47 | "phpunit/phpunit": "~6.5|^7.0|^8.0|^9.0|^10.5|^11.0", 48 | "orchestra/testbench": "~3.5|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0|^10.0" 49 | }, 50 | "autoload": { 51 | "psr-4": { 52 | "Mailjet\\LaravelMailjet\\": "src" 53 | } 54 | }, 55 | "autoload-dev": { 56 | "psr-4": { 57 | "Mailjet\\LaravelMailjet\\Tests\\": "tests" 58 | } 59 | }, 60 | "extra": { 61 | "laravel": { 62 | "providers": [ 63 | "Mailjet\\LaravelMailjet\\MailjetServiceProvider" 64 | ], 65 | "aliases": { 66 | "Mailjet": "Mailjet\\LaravelMailjet\\Facades\\Mailjet" 67 | } 68 | } 69 | }, 70 | "config": { 71 | "sort-packages": true, 72 | "bin-dir": "bin" 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/Contracts/CampaignContract.php: -------------------------------------------------------------------------------- 1 | getStatus(); 37 | $message = "{$message}: {$response->getReasonPhrase()}"; 38 | 39 | $this->setErrorFromResponse($response); 40 | } 41 | 42 | parent::__construct($message, $statusCode, $previous); 43 | } 44 | /** 45 | * Configure MailjetException from Mailjet\Response. 46 | * 47 | * @param Response $response 48 | */ 49 | private function setErrorFromResponse(Response $response): void 50 | { 51 | $body = $response->getBody(); 52 | 53 | if (isset($body['ErrorInfo'])) { 54 | $this->errorInfo = $body['ErrorInfo']; 55 | } 56 | if (isset($body['ErrorMessage'])) { 57 | $this->errorMessage = $body['ErrorMessage']; 58 | } 59 | if (isset($body['ErrorIdentifier'])) { 60 | $this->errorIdentifier = $body['ErrorIdentifier']; 61 | } 62 | } 63 | 64 | /** 65 | * @return string 66 | */ 67 | public function getErrorInfo(): string 68 | { 69 | return $this->errorInfo; 70 | } 71 | 72 | /** 73 | * @return string 74 | */ 75 | public function getErrorMessage(): string 76 | { 77 | return $this->errorMessage; 78 | } 79 | 80 | /** 81 | * @return string 82 | */ 83 | public function getErrorIdentifier(): string 84 | { 85 | return $this->errorIdentifier; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/Facades/Mailjet.php: -------------------------------------------------------------------------------- 1 | create( 24 | new Dsn( 25 | 'mailjet+api', 26 | 'default', 27 | config('services.mailjet.key'), 28 | config('services.mailjet.secret') 29 | ) 30 | ); 31 | }); 32 | } 33 | 34 | /** 35 | * Register the application services. 36 | * 37 | * @return void 38 | */ 39 | public function register(): void 40 | { 41 | $this->app->singleton('Mailjet', function () { 42 | $config = $this->app['config']->get('services.mailjet', []); 43 | $call = $this->app['config']->get('services.mailjet.common.call', true); 44 | $options = $this->app['config']->get('services.mailjet.common.options', []); 45 | 46 | return new MailjetService($config['key'], $config['secret'], $call, $options); 47 | }); 48 | } 49 | 50 | /** 51 | * @return array 52 | */ 53 | public function provides(): array 54 | { 55 | return ['mailjet']; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Model/Campaign.php: -------------------------------------------------------------------------------- 1 | fromEmail = $fromEmail; 26 | $this->optionalProperties = $optionalProperties; 27 | } 28 | 29 | /** 30 | * Format Campaign for MailJet API request. 31 | * 32 | * @return array 33 | */ 34 | public function format(): array 35 | { 36 | $result[self::FROM_EMAIL_KEY] = $this->fromEmail; 37 | 38 | return array_merge($result, $this->optionalProperties); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Model/CampaignDraft.php: -------------------------------------------------------------------------------- 1 | locale = $locale; 61 | $this->sender = $sender; 62 | $this->senderEmail = $senderEmail; 63 | $this->subject = $subject; 64 | $this->contactsListId = $contactsListId; 65 | $this->optionalProperties = $optionalProperties; 66 | } 67 | 68 | /** 69 | * Format CampaignDraft for MailJet API request. 70 | * 71 | * @return array 72 | */ 73 | public function format(): array 74 | { 75 | $result = [ 76 | self::LOCALE_KEY => $this->locale, 77 | self::SENDER_KEY => $this->sender, 78 | self::SENDER_EMAIL_KEY => $this->senderEmail, 79 | self::SUBJECT_KEY => $this->subject, 80 | self::CONTACT_LIST_ID_KEY => $this->contactsListId 81 | ]; 82 | 83 | return array_merge($result, $this->optionalProperties); 84 | } 85 | 86 | /** 87 | * Get CampaignDraft content. 88 | * 89 | * @return array|null 90 | */ 91 | public function getContent(): ?array 92 | { 93 | return $this->content; 94 | } 95 | 96 | /** 97 | * Set CampaignDraft content. 98 | * 99 | * @param $content 100 | * 101 | * @return \Mailjet\LaravelMailjet\Model\CampaignDraft 102 | */ 103 | public function setContent($content): CampaignDraft 104 | { 105 | $this->content = $content; 106 | 107 | return $this; 108 | } 109 | 110 | /** 111 | * Get CampaignDraft id. 112 | * 113 | * @return string|null 114 | */ 115 | public function getId(): ?string 116 | { 117 | return $this->id; 118 | } 119 | 120 | /** 121 | * Set CampaignDraft id. 122 | * 123 | * @param string $id 124 | * 125 | * @return \Mailjet\LaravelMailjet\Model\CampaignDraft 126 | */ 127 | public function setId(string $id): CampaignDraft 128 | { 129 | $this->id = $id; 130 | 131 | return $this; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/Model/Contact.php: -------------------------------------------------------------------------------- 1 | email = $email; 42 | $this->optionalProperties = $optionalProperties; 43 | } 44 | 45 | /** 46 | * Format Contact for MailJet API request. 47 | * 48 | * @return array 49 | */ 50 | public function format(): array 51 | { 52 | $result = [ 53 | self::EMAIL_KEY => $this->email, 54 | self::PROPERTIES_KEY => array_filter($this->optionalProperties) 55 | ]; 56 | 57 | if ($this->action !== null) { 58 | $result[self::ACTION_KEY] = $this->action; 59 | } 60 | 61 | return $result; 62 | } 63 | 64 | /** 65 | * Correspond to Email in Mailjet request. 66 | */ 67 | public function getEmail(): string 68 | { 69 | return $this->email; 70 | } 71 | 72 | /** 73 | * Set contact email. 74 | * 75 | * @param string $email 76 | * 77 | * @return Contact 78 | */ 79 | public function setEmail($email): Contact 80 | { 81 | $this->email = $email; 82 | 83 | return $this; 84 | } 85 | 86 | /** 87 | * Correspond to Name in MailJet request. 88 | */ 89 | public function getName(): ?string 90 | { 91 | return $this->optionalProperties[self::NAME_KEY] ?? null; 92 | } 93 | 94 | /** 95 | * Set contact name. 96 | * 97 | * @param string $name 98 | * 99 | * @return Contact 100 | */ 101 | public function setName(string $name): Contact 102 | { 103 | $this->optionalProperties[self::NAME_KEY] = $name; 104 | 105 | return $this; 106 | } 107 | 108 | /** 109 | * Action to the contact for Synchronization. 110 | * 111 | * @return string|null 112 | */ 113 | public function getAction(): ?string 114 | { 115 | return $this->action; 116 | } 117 | 118 | /** 119 | * Action to the contact for Synchronization. 120 | * 121 | * @param string $action (ACTION_* const) 122 | * 123 | * @return Contact 124 | */ 125 | public function setAction($action): Contact 126 | { 127 | if (! $this->validateAction($action)) { 128 | throw new RuntimeException("$action: is not a valid Action."); 129 | } 130 | 131 | $this->action = $action; 132 | 133 | return $this; 134 | } 135 | 136 | /** 137 | * Validate action. 138 | * 139 | * @param string $action 140 | * 141 | * @return bool 142 | */ 143 | protected function validateAction(string $action): bool 144 | { 145 | $available = [self::ACTION_ADDFORCE, self::ACTION_ADDNOFORCE, self::ACTION_REMOVE, self::ACTION_UNSUB]; 146 | 147 | return in_array($action, $available); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/Model/ContactMetadata.php: -------------------------------------------------------------------------------- 1 | validateDatatype($datatype)) { 33 | throw new RuntimeException("$datatype: is not a valid Datatype."); 34 | } 35 | 36 | $this->name = $name; 37 | $this->datatype = $datatype; 38 | } 39 | 40 | /** 41 | * Format contact for Mailjet API request. 42 | * 43 | * @return array 44 | */ 45 | public function format(): array 46 | { 47 | return [ 48 | 'Name' => $this->name, 49 | 'Datatype' => $this->datatype, 50 | ]; 51 | } 52 | 53 | /** 54 | * Validate datatype. 55 | * 56 | * @param string $datatype 57 | * 58 | * @return bool 59 | */ 60 | protected function validateDatatype(string $datatype): bool 61 | { 62 | $available = [ 63 | self::DATATYPE_STR, 64 | self::DATATYPE_INT, 65 | self::DATATYPE_FLOAT, 66 | self::DATATYPE_BOOL, 67 | self::DATATYPE_DATETIME, 68 | ]; 69 | 70 | return in_array($datatype, $available); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/Model/ContactsList.php: -------------------------------------------------------------------------------- 1 | validateAction($action)) { 37 | throw new RuntimeException("$action: is not a valid Action."); 38 | } 39 | 40 | $this->listId = $listId; 41 | $this->action = $action; 42 | $this->contacts = $contacts; 43 | } 44 | 45 | /** 46 | * Format contactList for MailJet API request. 47 | * 48 | * @return array 49 | */ 50 | public function format(): array 51 | { 52 | $result = [ 53 | 'Action' => $this->action, 54 | ]; 55 | 56 | $result['Contacts'] = array_map(static function (Contact $contact) { 57 | return $contact->format(); 58 | }, $this->contacts); 59 | 60 | return $result; 61 | } 62 | 63 | /** 64 | * Get list id 65 | */ 66 | public function getListId(): string 67 | { 68 | return $this->listId; 69 | } 70 | 71 | /** 72 | * Get action. 73 | */ 74 | public function getAction(): string 75 | { 76 | return $this->action; 77 | } 78 | 79 | /** 80 | * Set action. 81 | * 82 | * @param string $action 83 | */ 84 | public function setAction($action): ContactsList 85 | { 86 | if (! $this->validateAction($action)) { 87 | throw new RuntimeException("$action: is not a valid Action."); 88 | } 89 | 90 | $this->action = $action; 91 | 92 | return $this; 93 | } 94 | 95 | /** 96 | * Get contacts. 97 | */ 98 | public function getContacts(): array 99 | { 100 | return $this->contacts; 101 | } 102 | 103 | /** 104 | * Validate action name. 105 | * 106 | * @param string $action 107 | * 108 | * @return bool 109 | */ 110 | protected function validateAction($action): bool 111 | { 112 | $actionsAvailable = [self::ACTION_ADDFORCE, self::ACTION_ADDNOFORCE, self::ACTION_REMOVE, self::ACTION_UNSUB]; 113 | 114 | return in_array($action, $actionsAvailable); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/Model/EventCallbackUrl.php: -------------------------------------------------------------------------------- 1 | validateType($type)) { 69 | throw new RuntimeException("$type: is not a valid event type."); 70 | } 71 | 72 | if (! $this->validateStatus($status)) { 73 | throw new RuntimeException("$status: is not a valid event status."); 74 | } 75 | 76 | $this->url = $url; 77 | $this->type = $type; 78 | $this->isBackup = $isBackup; 79 | $this->status = $status; 80 | $this->version = $version; 81 | $this->apiKeyId = $apiKeyId; 82 | $this->groupEvent = $groupEvent; 83 | } 84 | 85 | /** 86 | * Format contactList for MailJet API request. 87 | * 88 | * @return array 89 | */ 90 | public function format(): array 91 | { 92 | if ($this->groupEvent) { 93 | // Events are grouped only in API version 2. 94 | $this->version = 2; 95 | } 96 | 97 | $result = [ 98 | 'Url' => $this->url, 99 | 'EventType' => $this->type, 100 | 'IsBackup' => $this->isBackup, 101 | 'Status' => $this->status, 102 | 'Version' => $this->version, 103 | ]; 104 | 105 | if ($this->apiKeyId) { 106 | $result['APIKeyID'] = $this->apiKeyId; 107 | } 108 | 109 | return $result; 110 | } 111 | 112 | /** 113 | * Validate event type. 114 | * 115 | * @param string $type 116 | * 117 | * @return bool 118 | */ 119 | protected function validateType(string $type): bool 120 | { 121 | $available = [ 122 | self::EVENT_TYPE_OPEN, 123 | self::EVENT_TYPE_CLICK, 124 | self::EVENT_TYPE_BOUNCE, 125 | self::EVENT_TYPE_SPAM, 126 | self::EVENT_TYPE_BLOCKED, 127 | self::EVENT_TYPE_UNSUB, 128 | self::EVENT_TYPE_SENT, 129 | ]; 130 | 131 | return in_array($type, $available); 132 | } 133 | 134 | /** 135 | * Validate event status. 136 | * 137 | * @param string $status 138 | * 139 | * @return bool 140 | */ 141 | protected function validateStatus(string $status): bool 142 | { 143 | $available = [self::EVENT_STATUS_ALIVE, self::EVENT_STATUS_DEAD]; 144 | 145 | return in_array($status, $available); 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/Model/Model.php: -------------------------------------------------------------------------------- 1 | value, ...] 24 | * 25 | * @return array 26 | */ 27 | public function getOptionalProperties(): array 28 | { 29 | return $this->optionalProperties; 30 | } 31 | 32 | /** 33 | * Set array of optional properties. 34 | * 35 | * @param array $properties 36 | * 37 | * @return array 38 | */ 39 | public function setOptionalProperties(array $properties): array 40 | { 41 | $this->optionalProperties = $properties; 42 | 43 | return $this->optionalProperties; 44 | } 45 | 46 | /** 47 | * Add a new optional property. 48 | * 49 | * @param array $property 50 | * 51 | * @return array 52 | */ 53 | public function addOptionalProperty(array $property): array 54 | { 55 | $this->optionalProperties[] = $property; 56 | 57 | return $this->optionalProperties; 58 | } 59 | 60 | /** 61 | * Remove a optional property. 62 | * 63 | * @param array $property 64 | * 65 | * @return array 66 | */ 67 | public function removeOptionalProperty(array $property): array 68 | { 69 | foreach (array_keys($this->optionalProperties, $property) as $key) { 70 | unset($this->optionalProperties[$key]); 71 | } 72 | 73 | return $this->optionalProperties; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/Model/Requestable.php: -------------------------------------------------------------------------------- 1 | name = $name; 32 | $this->optionalProperties = $optionalProperties; 33 | } 34 | 35 | /** 36 | * Format Template for MailJet API request. 37 | * 38 | * @return array 39 | */ 40 | public function format(): array 41 | { 42 | $result[self::NAME_KEY] = $this->name; 43 | 44 | return array_merge($result, $this->optionalProperties); 45 | } 46 | 47 | /** 48 | * Get Template content 49 | * 50 | * @return array|null $content 51 | */ 52 | public function getContent(): ?array 53 | { 54 | return $this->content; 55 | } 56 | 57 | /** 58 | * Set Template content. 59 | * 60 | * @param array $content 61 | * 62 | * @return \Mailjet\LaravelMailjet\Model\Template 63 | */ 64 | public function setContent(array $content): Template 65 | { 66 | $this->content = $content; 67 | 68 | return $this; 69 | } 70 | 71 | /** 72 | * Get id. 73 | * 74 | * @return string|null 75 | */ 76 | public function getId(): ?string 77 | { 78 | return $this->id; 79 | } 80 | 81 | /** 82 | * Set id. 83 | * 84 | * @param string $id 85 | * 86 | * @return \Mailjet\LaravelMailjet\Model\Template 87 | */ 88 | public function setId(string $id): Template 89 | { 90 | $this->id = $id; 91 | 92 | return $this; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/Providers/CampaignDraftServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind(CampaignDraftContract::class, function () { 38 | $config = $this->app['config']->get('services.mailjet', []); 39 | $call = $this->app['config']->get('services.mailjet.common.call', true); 40 | $options = $this->app['config']->get('services.mailjet.common.options', []); 41 | 42 | $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); 43 | 44 | return new CampaignDraftService($mailjetService); 45 | }); 46 | } 47 | 48 | /** 49 | * Get the services provided by the provider. 50 | * 51 | * @return array 52 | */ 53 | public function provides(): array 54 | { 55 | return [CampaignDraftContract::class]; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Providers/CampaignServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind(CampaignContract::class, function () { 38 | $config = $this->app['config']->get('services.mailjet', []); 39 | $call = $this->app['config']->get('services.mailjet.common.call', true); 40 | $options = $this->app['config']->get('services.mailjet.common.options', []); 41 | 42 | $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); 43 | 44 | return new CampaignService($mailjetService); 45 | }); 46 | } 47 | 48 | /** 49 | * Get the services provided by the provider. 50 | * 51 | * @return array 52 | */ 53 | public function provides(): array 54 | { 55 | return [CampaignContract::class]; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Providers/ContactMetadataServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind(ContactMetadataContract::class, function () { 38 | $config = $this->app['config']->get('services.mailjet', []); 39 | $call = $this->app['config']->get('services.mailjet.common.call', true); 40 | $options = $this->app['config']->get('services.mailjet.common.options', []); 41 | 42 | $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); 43 | 44 | return new ContactMetadataService($mailjetService); 45 | }); 46 | } 47 | 48 | /** 49 | * Get the services provided by the provider. 50 | * 51 | * @return array 52 | */ 53 | public function provides(): array 54 | { 55 | return [ContactMetadataContract::class]; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Providers/ContactsListServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind(ContactsListContract::class, function () { 38 | $config = $this->app['config']->get('services.mailjet', []); 39 | $call = $this->app['config']->get('services.mailjet.common.call', true); 40 | $options = $this->app['config']->get('services.mailjet.common.options', []); 41 | 42 | $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); 43 | 44 | return new ContactsListService($mailjetService); 45 | }); 46 | } 47 | 48 | /** 49 | * Get the services provided by the provider. 50 | * 51 | * @return array 52 | */ 53 | public function provides(): array 54 | { 55 | return [ContactsListContract::class]; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Providers/EventCallbackUrlServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind(EventCallbackUrlContract::class, function () { 38 | $config = $this->app['config']->get('services.mailjet', []); 39 | $call = $this->app['config']->get('services.mailjet.common.call', true); 40 | $options = $this->app['config']->get('services.mailjet.common.options', []); 41 | 42 | $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); 43 | 44 | return new EventCallbackUrlService($mailjetService); 45 | }); 46 | } 47 | 48 | /** 49 | * Get the services provided by the provider. 50 | * 51 | * @return array 52 | */ 53 | public function provides(): array 54 | { 55 | return [EventCallbackUrlContract::class]; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Providers/MailjetClientServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind(MailjetServiceContract::class, function () { 37 | $config = $this->app['config']->get('services.mailjet', []); 38 | $call = $this->app['config']->get('services.mailjet.common.call', true); 39 | $options = $this->app['config']->get('services.mailjet.common.options', []); 40 | 41 | return new MailjetService($config['key'], $config['secret'], $call, $options); 42 | }); 43 | } 44 | 45 | /** 46 | * Get the services provided by the provider. 47 | * 48 | * @return array 49 | */ 50 | public function provides(): array 51 | { 52 | return [MailjetServiceContract::class]; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Providers/TemplateServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind(TemplateServiceContract::class, function () { 38 | $config = $this->app['config']->get('services.mailjet', []); 39 | $call = $this->app['config']->get('services.mailjet.common.call', true); 40 | $options = $this->app['config']->get('services.mailjet.common.options', []); 41 | 42 | $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); 43 | 44 | return new TemplateService($mailjetService); 45 | }); 46 | } 47 | 48 | /** 49 | * Get the services provided by the provider. 50 | * 51 | * @return array 52 | */ 53 | public function provides(): array 54 | { 55 | return [TemplateServiceContract::class]; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Services/CampaignDraftService.php: -------------------------------------------------------------------------------- 1 | mailjet = $mailjet; 26 | } 27 | 28 | /** 29 | * List campaign draft resources available for this apikey. 30 | * 31 | * @return array 32 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 33 | */ 34 | public function getAllCampaignDrafts(array $filters = null): array 35 | { 36 | $response = $this->mailjet->get(Resources::$Campaigndraft, ['filters' => $filters]); 37 | 38 | if (! $response->success()) { 39 | throw new MailjetException(0, 'CampaignDraftService :getAllCampaignDrafts() failed', $response); 40 | } 41 | 42 | return $response->getData(); 43 | } 44 | 45 | /** 46 | * Access a given CampaignDraft resource. 47 | * 48 | * @param string $id 49 | * 50 | * @return array 51 | */ 52 | public function findByCampaignDraftId(string $id) 53 | { 54 | $response = $this->mailjet->get(Resources::$Campaigndraft, 55 | ['id' => $id]); 56 | if (! $response->success()) { 57 | $this->throwError('CampaignDraftService:findByCampaignDraftId() failed', 58 | $response); 59 | } 60 | 61 | return $response->getData(); 62 | } 63 | 64 | /** 65 | * create a new fresh CampaignDraft 66 | * 67 | * @param Campaigndraft $campaignDraft 68 | */ 69 | public function create(CampaignDraft $campaignDraft) 70 | { 71 | $response = $this->mailjet->post(Resources::$Campaigndraft, 72 | ['body' => $campaignDraft->format()]); 73 | if (! $response->success()) { 74 | $this->throwError('CampaignDraftService:create() failed', $response); 75 | } 76 | 77 | return $response->getData(); 78 | } 79 | 80 | /** 81 | * Update one specific campaigndraft resource 82 | * 83 | * @param int $id 84 | * @param Campaigndraft $campaignDraft 85 | */ 86 | public function update($id, CampaignDraft $campaignDraft) 87 | { 88 | $response = $this->mailjet->put(Resources::$Campaigndraft, 89 | ['id' => $id, 'body' => $campaignDraft->format()]); 90 | if (! $response->success()) { 91 | $this->throwError('CampaignDraftService:update() failed', $response); 92 | } 93 | 94 | return $response->getData(); 95 | } 96 | 97 | /** 98 | * Return the text and html contents of the campaigndraft 99 | * 100 | * @param string $id 101 | * 102 | * @return array 103 | */ 104 | public function getDetailContent(string $id) 105 | { 106 | $response = $this->mailjet->get(Resources::$CampaigndraftDetailcontent, 107 | ['id' => $id]); 108 | if (! $response->success()) { 109 | $this->throwError('CampaignDraftService:getDetailContent failed', 110 | $response); 111 | } 112 | 113 | return $response->getData(); 114 | } 115 | 116 | /** 117 | * Creates the content of a campaigndraft 118 | * 119 | * @param string $id 120 | * 121 | * @return array 122 | */ 123 | public function createDetailContent($id, $contentData) 124 | { 125 | $response = $this->mailjet->post(Resources::$CampaigndraftDetailcontent, 126 | ['id' => $id, 'body' => $contentData]); 127 | if (! $response->success()) { 128 | $this->throwError('CampaignDraftService:createDetailContent failed', 129 | $response); 130 | } 131 | 132 | return $response->getData(); 133 | } 134 | 135 | /** 136 | * Return the date of the scheduled sending of the campaigndraft 137 | * 138 | * @param string Campaign $id 139 | * 140 | * @return array 141 | */ 142 | public function getSchedule(string $id) 143 | { 144 | $response = $this->mailjet->get(Resources::$CampaigndraftSchedule, 145 | ['id' => $id]); 146 | if (! $response->success()) { 147 | $this->throwError('CampaignDraftService:getSchedule failed', 148 | $response); 149 | } 150 | 151 | return $response->getData(); 152 | } 153 | 154 | /** 155 | * Schedule when the campaigndraft will be sent 156 | * 157 | * @param string $id 158 | * @param string $date (RFC3339 format "Y-m-d\TH:i:sP") 159 | * 160 | * @return array 161 | */ 162 | public function scheduleCampaign(string $id, string $date) 163 | { 164 | $response = $this->mailjet->post(Resources::$CampaigndraftSchedule, 165 | ['id' => $id, 'body' => $date]); 166 | if (! $response->success()) { 167 | $this->throwError('CampaignDraftService:scheduleCampaign failed', 168 | $response); 169 | } 170 | 171 | return $response->getData(); 172 | } 173 | 174 | /** 175 | * Update the date when the campaigndraft will be sent 176 | * 177 | * @param string Campaign $id 178 | * @param string Schedule $date 179 | * 180 | * @return array 181 | */ 182 | public function updateCampaignSchedule($id, $date) 183 | { 184 | $response = $this->mailjet->put(Resources::$CampaigndraftSchedule, 185 | ['id' => $id, 'body' => $date]); 186 | if (! $response->success()) { 187 | $this->throwError('CampaignDraftService:updateCampaignSchedule failed', 188 | $response); 189 | } 190 | 191 | return $response->getData(); 192 | } 193 | 194 | /** 195 | * Cancel a future sending 196 | * 197 | * @param string Campaign $id 198 | * 199 | * @return array 200 | */ 201 | public function removeSchedule(string $id) 202 | { 203 | $response = $this->mailjet->delete(Resources::$CampaigndraftSchedule, 204 | ['id' => $id]); 205 | if (! $response->success()) { 206 | $this->throwError('CampaignDraftService:removeSchedule failed', 207 | $response); 208 | } 209 | 210 | return $response->getData(); 211 | } 212 | 213 | /** 214 | * Send the campaign immediately 215 | * 216 | * @param string Campaign $id 217 | * 218 | * @return array 219 | */ 220 | public function sendCampaign(string $id) 221 | { 222 | $response = $this->mailjet->post(Resources::$CampaigndraftSend, 223 | ['id' => $id]); 224 | if (! $response->success()) { 225 | $this->throwError('CampaignDraftService:sendCampaign failed', 226 | $response); 227 | } 228 | 229 | return $response->getData(); 230 | } 231 | 232 | /** 233 | * Return the status of a CampaignDraft 234 | * 235 | * @param string Campaign $id 236 | * 237 | * @return array 238 | */ 239 | public function getCampaignStatus(string $id) 240 | { 241 | $response = $this->mailjet->get(Resources::$CampaigndraftStatus, 242 | ['id' => $id]); 243 | if (! $response->success()) { 244 | $this->throwError('CampaignDraftService:getCampaignStatus failed', 245 | $response); 246 | } 247 | 248 | return $response->getData(); 249 | } 250 | 251 | /** 252 | * An action to test a CampaignDraft. 253 | * 254 | * @param string Campaign $id 255 | * @param array of $recipients 256 | * 257 | * @return array 258 | */ 259 | public function testCampaign($id, $recipients) 260 | { 261 | $response = $this->mailjet->post(Resources::$CampaigndraftTest, 262 | ['id' => $id, 'body' => $recipients]); 263 | if (! $response->success()) { 264 | $this->throwError('CampaignDraftService:testCampaign failed', 265 | $response); 266 | } 267 | 268 | return $response->getData(); 269 | } 270 | 271 | private function throwError($title, Response $response) 272 | { 273 | throw new MailjetException(0, $title, $response); 274 | } 275 | } 276 | -------------------------------------------------------------------------------- /src/Services/CampaignService.php: -------------------------------------------------------------------------------- 1 | mailjet = $mailjet; 25 | } 26 | 27 | /** 28 | * List campaigns resources available for this apikey 29 | * 30 | * @return array 31 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 32 | */ 33 | public function getAllCampaigns(array $filters = null): array 34 | { 35 | $response = $this->mailjet->get(Resources::$Campaign, ['filters' => $filters]); 36 | 37 | if (! $response->success()) { 38 | throw new MailjetException(0, 'CampaignService:getAllCampaigns() failed', $response); 39 | } 40 | 41 | return $response->getData(); 42 | } 43 | 44 | /** 45 | * Access a given campaign resource. 46 | * 47 | * @param string $id 48 | * 49 | * @return array 50 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 51 | */ 52 | public function findByCampaignId(string $id): array 53 | { 54 | $response = $this->mailjet->get(Resources::$Campaign, ['id' => $id]); 55 | 56 | if (! $response->success()) { 57 | throw new MailjetException(0, 'CampaignService:findByCampaignId() failed', $response); 58 | } 59 | 60 | return $response->getData(); 61 | } 62 | 63 | /** 64 | * Find a given campaign by Newsletter / CampaignDraft id. 65 | * 66 | * @param string $id 67 | * 68 | * @return array 69 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 70 | */ 71 | public function findByNewsletterId(string $id): array 72 | { 73 | $response = $this->mailjet->get(Resources::$Campaign, ['mj.nl' => $id]); 74 | 75 | if (! $response->success()) { 76 | throw new MailjetException(0, 'CampaignService:findByNewsletterId() failed', $response); 77 | } 78 | 79 | return $response->getData(); 80 | } 81 | 82 | /** 83 | * Update one specific campaign resource with a PUT request. 84 | * 85 | * @param string $id 86 | * 87 | * @return array 88 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 89 | */ 90 | public function updateCampaign(string $id, Campaign $campaign): array 91 | { 92 | $response = $this->mailjet->put(Resources::$Campaign, ['id' => $id, 'body' => $campaign->format()]); 93 | 94 | if (! $response->success()) { 95 | throw new MailjetException(0, 'CampaignService:updateCampaign() failed', $response); 96 | } 97 | 98 | return $response->getData(); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/Services/ContactMetadataService.php: -------------------------------------------------------------------------------- 1 | mailjet = $mailjet; 25 | } 26 | 27 | /** 28 | * Retrieve all ContactMetadata. 29 | * 30 | * @return array 31 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 32 | */ 33 | public function getAll(): array 34 | { 35 | $response = $this->mailjet->get(Resources::$Contactmetadata); 36 | 37 | if (! $response->success()) { 38 | throw new MailjetException(0, 'ContactMetadataService:getAll() failed', $response); 39 | } 40 | 41 | return $response->getData(); 42 | } 43 | 44 | /** 45 | * Retrieve one ContactMetadata. 46 | * 47 | * @param string $id 48 | * 49 | * @return array 50 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 51 | */ 52 | public function get(string $id): array 53 | { 54 | $response = $this->mailjet->get(Resources::$Contactmetadata, ['id' => $id]); 55 | 56 | if (! $response->success()) { 57 | throw new MailjetException(0, 'ContactMetadataService:get() failed', $response); 58 | } 59 | 60 | return $response->getData(); 61 | } 62 | 63 | /** 64 | * create a new fresh ContactMetadata 65 | * 66 | * @param ContactMetadata $metadata 67 | * 68 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 69 | */ 70 | public function create(ContactMetadata $metadata): array 71 | { 72 | $response = $this->mailjet->post(Resources::$Contactmetadata, ['body' => $metadata->format()]); 73 | 74 | if (! $response->success()) { 75 | throw new MailjetException(0, 'ContactMetadataService:create() failed', $response); 76 | } 77 | 78 | return $response->getData(); 79 | } 80 | 81 | /** 82 | * Update one ContactMetadata 83 | * 84 | * @param string $id 85 | * @param ContactMetadata $metadata 86 | * 87 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 88 | */ 89 | public function update(string $id, ContactMetadata $metadata): array 90 | { 91 | $response = $this->mailjet->put(Resources::$Contactmetadata, ['id' => $id, 'body' => $metadata->format()]); 92 | 93 | if (! $response->success()) { 94 | throw new MailjetException(0, 'ContactMetadataService:update() failed', $response); 95 | } 96 | 97 | return $response->getData(); 98 | } 99 | 100 | /** 101 | * Delete one ContactMetadata 102 | * 103 | * @param string $id 104 | * 105 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 106 | */ 107 | public function delete(string $id): array 108 | { 109 | $response = $this->mailjet->delete(Resources::$Contactmetadata, ['id' => $id]); 110 | 111 | if (! $response->success()) { 112 | throw new MailjetException(0, 'ContactMetadataService:delete() failed', $response); 113 | } 114 | 115 | return $response->getData(); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/Services/ContactsListService.php: -------------------------------------------------------------------------------- 1 | mailjet = $mailjet; 32 | } 33 | 34 | /** 35 | * Create a new fresh Contact to listId. 36 | * 37 | * @param string $id 38 | * @param Contact $contact 39 | * @param string $action 40 | * 41 | * @return array 42 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 43 | */ 44 | public function create(string $id, Contact $contact, $action = Contact::ACTION_ADDFORCE): array 45 | { 46 | $contact->setAction($action); 47 | 48 | $response = $this->_exec($id, $contact); 49 | 50 | if (! $response->success()) { 51 | throw new MailjetException(0, 'ContactsListService:create() failed', $response); 52 | } 53 | 54 | return $response->getData(); 55 | } 56 | 57 | /** 58 | * Update a Contact to listId. 59 | * 60 | * @param string $id 61 | * @param Contact $contact 62 | * @param string $action 63 | * 64 | * @return array 65 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 66 | */ 67 | public function update(string $id, Contact $contact, $action = Contact::ACTION_ADDNOFORCE): array 68 | { 69 | $contact->setAction($action); 70 | 71 | $response = $this->_exec($id, $contact); 72 | 73 | if (! $response->success()) { 74 | throw new MailjetException(0, 'ContactsListService:update() failed', $response); 75 | } 76 | 77 | return $response->getData(); 78 | } 79 | 80 | /** 81 | * Re/subscribe a Contact to listId. 82 | * 83 | * @param string $id 84 | * @param Contact $contact 85 | * @param bool $force 86 | * 87 | * @return array 88 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 89 | */ 90 | public function subscribe(string $id, Contact $contact, bool $force = true): array 91 | { 92 | $contact->setAction($force ? Contact::ACTION_ADDFORCE : Contact::ACTION_ADDNOFORCE); 93 | 94 | $response = $this->_exec($id, $contact); 95 | 96 | if (! $response->success()) { 97 | throw new MailjetException(0, 'ContactsListService:subscribe() failed', $response); 98 | } 99 | 100 | return $response->getData(); 101 | } 102 | 103 | /** 104 | * Unsubscribe a Contact from listId. 105 | * 106 | * @param string $id 107 | * @param Contact $contact 108 | * 109 | * @return array 110 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 111 | */ 112 | public function unsubscribe(string $id, Contact $contact): array 113 | { 114 | $contact->setAction(Contact::ACTION_UNSUB); 115 | 116 | $response = $this->_exec($id, $contact); 117 | 118 | if (! $response->success()) { 119 | throw new MailjetException(0, 'ContactsListService:unsubscribe() failed', $response); 120 | } 121 | 122 | return $response->getData(); 123 | } 124 | 125 | /** 126 | * Delete a Contact from listId 127 | * 128 | * @param string $id 129 | * @param Contact $contact 130 | * 131 | * @return array 132 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 133 | */ 134 | public function delete(string $id, Contact $contact): array 135 | { 136 | $contact->setAction(Contact::ACTION_REMOVE); 137 | 138 | $response = $this->_exec($id, $contact); 139 | 140 | if (! $response->success()) { 141 | throw new MailjetException(0, 'ContactsListService:delete() failed', $response); 142 | } 143 | 144 | return $response->getData(); 145 | } 146 | 147 | /** 148 | * Change email a Contact. 149 | * 150 | * @param string $id 151 | * @param Contact $contact 152 | * @param string $oldEmail 153 | * 154 | * @return array 155 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 156 | */ 157 | public function updateEmail(string $id, Contact $contact, string $oldEmail): array 158 | { 159 | $response = $this->mailjet->get(Resources::$Contactdata, ['id' => $oldEmail]); 160 | 161 | if (! $response->success()) { 162 | throw new MailjetException(0, 'ContactsListService:changeEmail() failed', $response); 163 | } 164 | 165 | $oldContactData = $response->getData(); 166 | 167 | if (isset($oldContactData[0])) { 168 | $contact->setOptionalProperties($oldContactData[0]['Data']); 169 | } 170 | 171 | $contact->setAction(Contact::ACTION_ADDFORCE); 172 | $response = $this->_exec($id, $contact); 173 | 174 | if (! $response->success()) { 175 | throw new MailjetException(0, 'ContactsListService:changeEmail() failed', $response); 176 | } 177 | 178 | $oldContact = new Contact($oldEmail); 179 | $oldContact->setAction(Contact::ACTION_REMOVE); 180 | $response = $this->_exec($id, $oldContact); 181 | 182 | if (! $response->success()) { 183 | throw new MailjetException(0, 'ContactsListService:changeEmail() failed', $response); 184 | } 185 | 186 | return $response->getData(); 187 | } 188 | 189 | /** 190 | * Import many contacts to a list. 191 | * https://dev.mailjet.com/email-api/v3/contactslist-managemanycontacts/ 192 | * 193 | * @param ContactsList $list 194 | * 195 | * @return array 196 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 197 | */ 198 | public function uploadManyContactsList(ContactsList $list): array 199 | { 200 | $batchResults = []; 201 | $contactChunks = array_chunk($list->getContacts(), self::CONTACT_BATCH_SIZE); 202 | 203 | foreach ($contactChunks as $contactChunk) { 204 | $subContactsList = new ContactsList($list->getListId(), $list->getAction(), $contactChunk); 205 | $currentBatch = $this->mailjet->post(Resources::$ContactslistManagemanycontacts, 206 | ['id' => $subContactsList->getListId(), 'body' => $subContactsList->format()] 207 | ); 208 | 209 | if ($currentBatch->success()) { 210 | $batchResults[] = $currentBatch->getData()[0]; 211 | } else { 212 | throw new MailjetException(0, 'ContactsListService:manageManyContactsList() failed', $currentBatch); 213 | } 214 | } 215 | 216 | return $batchResults; 217 | } 218 | 219 | /** 220 | * An action for adding a contact to a contact list. Only POST is supported. 221 | * The API will internally create the new contact if it does not exist, 222 | * add or update the name and properties. 223 | * The properties have to be defined before they can be used. 224 | * The API then adds the contact to the contact list with active=true 225 | * and unsub=specified value if it is not already in the list, 226 | * or updates the entry with these values. 227 | * On success, the API returns a packet with the same format 228 | * but with all properties available for that contact. 229 | * 230 | * @param string $id 231 | * @param Contact $contact 232 | * 233 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 234 | */ 235 | private function _exec(string $id, Contact $contact): Response 236 | { 237 | return $this->mailjet->post(Resources::$ContactslistManagecontact, 238 | ['id' => $id, 'body' => $contact->format()] 239 | ); 240 | } 241 | } 242 | -------------------------------------------------------------------------------- /src/Services/EventCallbackUrlService.php: -------------------------------------------------------------------------------- 1 | mailjet = $mailjet; 25 | } 26 | 27 | /** 28 | * Retrieve all EventCallbackUrl. 29 | * 30 | * @return array 31 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 32 | */ 33 | public function getAll(): array 34 | { 35 | $response = $this->mailjet->get(Resources::$Eventcallbackurl); 36 | 37 | if (! $response->success()) { 38 | throw new MailjetException(0, 'EventCallbackUrlService:getAll() failed', $response); 39 | } 40 | 41 | return $response->getData(); 42 | } 43 | 44 | /** 45 | * Retrieve one EventCallbackUrl. 46 | * 47 | * @param string $id 48 | * 49 | * @return array 50 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 51 | */ 52 | public function get(string $id): array 53 | { 54 | $response = $this->mailjet->get(Resources::$Eventcallbackurl, ['id' => $id]); 55 | 56 | if (! $response->success()) { 57 | throw new MailjetException(0, 'EventCallbackUrlService:get() failed', $response); 58 | } 59 | 60 | return $response->getData(); 61 | } 62 | 63 | /** 64 | * Create one EventCallbackUrl. 65 | * 66 | * @param EventCallbackUrl $url 67 | * 68 | * @return array 69 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 70 | */ 71 | public function create(EventCallbackUrl $url): array 72 | { 73 | $response = $this->mailjet->post(Resources::$Eventcallbackurl, ['body' => $url->format()]); 74 | 75 | if (! $response->success()) { 76 | throw new MailjetException(0, 'EventCallbackUrlService:create() failed', $response); 77 | } 78 | 79 | return $response->getData(); 80 | } 81 | 82 | /** 83 | * Update one EventCallbackUrl. 84 | * 85 | * @param string $id 86 | * @param EventCallbackUrl $url 87 | * 88 | * @return array 89 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 90 | */ 91 | public function update(string $id, EventCallbackUrl $url): array 92 | { 93 | $response = $this->mailjet->put(Resources::$Eventcallbackurl, ['id' => $id, 'body' => $url->format()]); 94 | 95 | if (! $response->success()) { 96 | throw new MailjetException(0, 'EventCallbackUrlService:update() failed', $response); 97 | } 98 | 99 | return $response->getData(); 100 | } 101 | 102 | /** 103 | * Delete one EventCallbackUrl. 104 | * 105 | * @param string $id 106 | * 107 | * @return array 108 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 109 | */ 110 | public function delete(string $id): array 111 | { 112 | $response = $this->mailjet->delete(Resources::$Eventcallbackurl, ['id' => $id]); 113 | 114 | if (! $response->success()) { 115 | throw new MailjetException(0, 'EventCallbackUrlService:delete() failed', $response); 116 | } 117 | 118 | return $response->getData(); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/Services/MailjetService.php: -------------------------------------------------------------------------------- 1 | client = new Client($key, $secret, $call, $settings); 23 | } 24 | 25 | /** 26 | * Trigger a POST request. 27 | * 28 | * @param array $resource Mailjet Resource/Action pair 29 | * @param array $args Request arguments 30 | * @param array $options 31 | * 32 | * @return Response 33 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 34 | */ 35 | public function post(array $resource, array $args = [], array $options = []): Response 36 | { 37 | $response = $this->client->post($resource, $args, $options); 38 | 39 | if (! $response->success()) { 40 | throw new MailjetException(0, 'MailjetService:post() failed', $response); 41 | } 42 | 43 | return $response; 44 | } 45 | 46 | /** 47 | * Trigger a GET request. 48 | * 49 | * @param array $resource Mailjet Resource/Action pair 50 | * @param array $args Request arguments 51 | * @param array $options 52 | * 53 | * @return Response 54 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 55 | */ 56 | public function get(array $resource, array $args = [], array $options = []): Response 57 | { 58 | $response = $this->client->get($resource, $args, $options); 59 | 60 | if (! $response->success()) { 61 | throw new MailjetException(0, 'MailjetService:get() failed', $response); 62 | } 63 | 64 | return $response; 65 | } 66 | 67 | /** 68 | * Trigger a PUT request. 69 | * 70 | * @param array $resource Mailjet Resource/Action pair 71 | * @param array $args Request arguments 72 | * @param array $options 73 | * 74 | * @return Response 75 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 76 | */ 77 | public function put(array $resource, array $args = [], array $options = []): Response 78 | { 79 | $response = $this->client->put($resource, $args, $options); 80 | 81 | if (! $response->success()) { 82 | throw new MailjetException(0, 'MailjetService:put() failed', $response); 83 | } 84 | 85 | return $response; 86 | } 87 | 88 | /** 89 | * Trigger a DELETE request. 90 | * 91 | * @param array $resource Mailjet Resource/Action pair 92 | * @param array $args Request arguments 93 | * @param array $options 94 | * 95 | * @return Response 96 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 97 | */ 98 | public function delete(array $resource, array $args = [], array $options = []): Response 99 | { 100 | $response = $this->client->delete($resource, $args, $options); 101 | 102 | if (! $response->success()) { 103 | throw new MailjetException(0, 'MailjetService:delete() failed', $response); 104 | } 105 | 106 | return $response; 107 | } 108 | 109 | /** 110 | * Get all list on your Mailjet account. 111 | * TODO: Exclude HIGH Level API methods into managers. 112 | * 113 | * @param array $filters Filters that will be use to filter the request 114 | * 115 | * @return \Mailjet\Response 116 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 117 | */ 118 | public function getAllLists(array $filters = null): Response 119 | { 120 | $response = $this->client->get(Resources::$Contactslist, ['filters' => $filters]); 121 | 122 | if (! $response->success()) { 123 | throw new MailjetException(0, 'MailjetService:getAllLists() failed', $response); 124 | } 125 | 126 | return $response; 127 | } 128 | 129 | /** 130 | * Create a new list. 131 | * 132 | * @param array $body Information list - the 'Name' field is mandatory. 133 | * 134 | * @return \Mailjet\Response 135 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 136 | */ 137 | public function createList(array $body): Response 138 | { 139 | $response = $this->client->post(Resources::$Contactslist, ['body' => $body]); 140 | 141 | if (! $response->success()) { 142 | throw new MailjetException(0, 'MailjetService:createList() failed', $response); 143 | } 144 | 145 | return $response; 146 | } 147 | 148 | /** 149 | * Get all list recipient on your Mailjet account. 150 | * 151 | * @param array $filters Filters that will be use to filter the request. 152 | * 153 | * @return \Mailjet\Response 154 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 155 | */ 156 | public function getListRecipients(array $filters = null): Response 157 | { 158 | $response = $this->client->get(Resources::$Listrecipient, ['filters' => $filters]); 159 | 160 | if (! $response->success()) { 161 | throw new MailjetException(0, 'MailjetService:getListRecipients() failed', $response); 162 | } 163 | 164 | return $response; 165 | } 166 | 167 | /** 168 | * Get single contact information. 169 | * 170 | * @param string $id 171 | * 172 | * @return \Mailjet\Response 173 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 174 | */ 175 | public function getSingleContact(string $id): Response 176 | { 177 | $response = $this->client->get(Resources::$Contact, ['id' => $id]); 178 | 179 | if (! $response->success()) { 180 | throw new MailjetException(0, 'MailjetService:getSingleContact() failed', $response); 181 | } 182 | 183 | return $response; 184 | } 185 | 186 | /** 187 | * Create a contact. 188 | * 189 | * @param array $body Information list - the 'Email' field is mandatory. 190 | * 191 | * @return \Mailjet\Response 192 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 193 | */ 194 | public function createContact(array $body): Response 195 | { 196 | $response = $this->client->post(Resources::$Contact, ['body' => $body]); 197 | 198 | if (! $response->success()) { 199 | throw new MailjetException(0, 'MailjetService:createContact() failed', $response); 200 | } 201 | 202 | return $response; 203 | } 204 | 205 | /** 206 | * Create a list recipient (relationship between contact and list). 207 | * 208 | * @param array $body Information list - the 'ContactID' and 'ListID' parameters are mandatory. 209 | * 210 | * @return \Mailjet\Response 211 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 212 | */ 213 | public function createListRecipient(array $body): Response 214 | { 215 | $response = $this->client->post(Resources::$Listrecipient, ['body' => $body]); 216 | 217 | if (! $response->success()) { 218 | throw new MailjetException(0, 'MailjetService:createListRecipient() failed', $response); 219 | } 220 | 221 | return $response; 222 | } 223 | 224 | /** 225 | * Edit a list recipient. 226 | * 227 | * @param string $id 228 | * @param array $body Information list - the 'ContactID' and 'ListID' parameters are mandatory. 229 | * 230 | * @return \Mailjet\Response 231 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 232 | */ 233 | public function editListRecipient(string $id, array $body): Response 234 | { 235 | $response = $this->client->put(Resources::$Listrecipient, ['id' => $id, 'body' => $body]); 236 | 237 | if (! $response->success()) { 238 | throw new MailjetException(0, 'MailjetService:editListrecipient() failed', $response); 239 | } 240 | 241 | return $response; 242 | } 243 | 244 | /** 245 | * Retrieve Mailjet client. 246 | * 247 | * @return \Mailjet\Client 248 | */ 249 | public function getClient(): Client 250 | { 251 | return $this->client; 252 | } 253 | } 254 | -------------------------------------------------------------------------------- /src/Services/TemplateService.php: -------------------------------------------------------------------------------- 1 | mailjet = $mailjet; 25 | } 26 | 27 | /** 28 | * List template resources available for this apikey, use a GET request. 29 | * Alternatively, you may want to add one or more filters. 30 | * 31 | * @param array|null $filters 32 | * 33 | * @return array 34 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 35 | */ 36 | public function getAll(array $filters = null): array 37 | { 38 | $response = $this->mailjet->get(Resources::$Template, ['filters' => $filters]); 39 | 40 | if (! $response->success()) { 41 | throw new MailjetException(0, 'TemplateService:getAll() failed', $response); 42 | } 43 | 44 | return $response->getData(); 45 | } 46 | 47 | /** 48 | * Access a given template resource, use a GET request, providing the template's ID value. 49 | * 50 | * @param string $id 51 | * 52 | * @return array 53 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 54 | */ 55 | public function get(string $id): array 56 | { 57 | $response = $this->mailjet->get(Resources::$Template, ['id' => $id]); 58 | 59 | if (! $response->success()) { 60 | throw new MailjetException(0, 'TemplateService:get() failed', $response); 61 | } 62 | 63 | return $response->getData(); 64 | } 65 | 66 | /** 67 | * Add a new template resource with a POST request. 68 | * 69 | * @param Template $template 70 | * 71 | * @return array 72 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 73 | */ 74 | public function create(Template $template): array 75 | { 76 | $response = $this->mailjet->post(Resources::$Template, ['body' => $template->format()]); 77 | 78 | if (! $response->success()) { 79 | throw new MailjetException(0, 'TemplateService:create() failed', $response); 80 | } 81 | 82 | return $response->getData(); 83 | } 84 | 85 | /** 86 | * Update one specific template resource with a PUT request, providing the template's ID value. 87 | * 88 | * @param string $id 89 | * @param Template $template 90 | * 91 | * @return array 92 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 93 | */ 94 | public function update(string $id, Template $template): array 95 | { 96 | $response = $this->mailjet->put(Resources::$Template, ['id' => $id, 'body' => $template->format()]); 97 | 98 | if (! $response->success()) { 99 | throw new MailjetException(0, 'TemplateService:update() failed', $response); 100 | } 101 | 102 | return $response->getData(); 103 | } 104 | 105 | /** 106 | * Delete a given template. 107 | * 108 | * @param string $id 109 | * 110 | * @return array 111 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 112 | */ 113 | public function delete(string $id): array 114 | { 115 | $response = $this->mailjet->delete(Resources::$Template, ['id' => $id]); 116 | 117 | if (! $response->success()) { 118 | throw new MailjetException(0, 'TemplateService:delete() failed', $response); 119 | } 120 | 121 | return $response->getData(); 122 | } 123 | 124 | /** 125 | * Return the text and html contents of the Template. 126 | * 127 | * @param string $id 128 | * 129 | * @return array 130 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 131 | */ 132 | public function getDetailContent(string $id): array 133 | { 134 | $response = $this->mailjet->get(Resources::$TemplateDetailcontent, ['id' => $id]); 135 | 136 | if (! $response->success()) { 137 | throw new MailjetException(0, 'TemplateService:getDetailContent failed', $response); 138 | } 139 | 140 | return $response->getData(); 141 | } 142 | 143 | /** 144 | * Creates the content of a Template. 145 | * 146 | * @param string $id 147 | * @param array $content 148 | * 149 | * @return array 150 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 151 | */ 152 | public function createDetailContent(string $id, array $content): array 153 | { 154 | $response = $this->mailjet->post(Resources::$TemplateDetailcontent, ['id' => $id, 'body' => $content]); 155 | 156 | if (! $response->success()) { 157 | throw new MailjetException(0, 'TemplateService:createDetailContent failed', $response); 158 | } 159 | 160 | return $response->getData(); 161 | } 162 | 163 | /** 164 | * Deletes the content of a Template. 165 | * 166 | * @param string $id 167 | * 168 | * @return array 169 | * @throws \Mailjet\LaravelMailjet\Exception\MailjetException 170 | */ 171 | public function deleteDetailContent(string $id): array 172 | { 173 | $response = $this->mailjet->post(Resources::$TemplateDetailcontent, ['id' => $id, 'body' => null]); 174 | 175 | if (! $response->success()) { 176 | throw new MailjetException(0, 'TemplateService:createDetailContent failed', $response); 177 | } 178 | 179 | return $response->getData(); 180 | } 181 | } 182 | --------------------------------------------------------------------------------