├── .gitattributes ├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── mkdocs.yml ├── phpunit.xml ├── src ├── Contracts │ ├── CampaignContract.php │ ├── CampaignDraftContract.php │ ├── ContactMetadataContract.php │ ├── ContactsListContract.php │ ├── ContactsV4Contract.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 │ ├── ContactsServiceProvider.php │ ├── EventCallbackUrlServiceProvider.php │ ├── MailjetClientServiceProvider.php │ └── TemplateServiceProvider.php └── Services │ ├── CampaignDraftService.php │ ├── CampaignService.php │ ├── ContactMetadataService.php │ ├── ContactsListService.php │ ├── ContactsV4Service.php │ ├── EventCallbackUrlService.php │ ├── MailjetService.php │ └── TemplateService.php └── tests ├── Providers ├── CampaignDraftServiceProviderTest.php ├── CampaignServiceProviderTest.php ├── ContactMetadataServiceProviderTest.php ├── ContactsListServiceProviderTest.php ├── EventCallbackUrlServiceProviderTest.php ├── MailjetClientServiceProviderTest.php └── TemplateServiceProviderTest.php ├── Services └── MailjetServiceTest.php └── bootstrap.php /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.css linguist-vendored 3 | *.scss linguist-vendored 4 | *.js linguist-vendored 5 | CHANGELOG.md export-ignore 6 | /docs export-ignore 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /public/hot 3 | /public/storage 4 | /storage/*.key 5 | /vendor 6 | /.idea 7 | /.vagrant 8 | Homestead.json 9 | Homestead.yaml 10 | npm-debug.log 11 | .env 12 | composer.lock 13 | /bin/ 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - '7.1' 4 | - '7.2' 5 | - '8.0' 6 | before_install: 7 | - composer install --dev -v --prefer-source 8 | script: 9 | - bin/phpunit -c . 10 | notifications: 11 | slack: 12 | secure: YpECkeOQvtc5iVqVzBiV2XzOXEpL2s5Nyp82waOwRyegNY8y3QWP4h0UhbXJmUlcM6E+NMZgNBmQKrtkoM8u1H0K5Q1Qun/0Ca0qbku5eLrXbxS1PlYDwnTpgv6yAfJsqjkEVturV5QYDHVG5O9VJtKCzUXTC5ZZDl4AS7tKScrXwfZxcM82OWeksokAsMRjVTKamqWvs95Pr35rhUpggwqS7tHF9Kf9ynkwL6PC2UMglcqPKbd+eUohaxWu4uJ6+9jAYesjEmqzBEEiMdAXmDPKhJrjHnzP8pozYacpasBjT29KCNIn3ny2aN+REui74m6vpzHOohNvu46IAfNIN3r4PhVhC/szwgguaSJBd9YvyZO4RXNXn9dZRX7BBZ20Z2Q/CMvqJCHYk00X9W1TIziIa1Va1U7zmhTJYjCCcsKN0e60VqrVsbIyM6tv8ym5t2zYHUa+E9IsHYl9T8u2itBPgzbbndD8r+hch3vQ32DUCepBNirNyAZriV++piMuj00COBa1RAjgYTwOsXN1vDQi84RfxVRtwIsF3v2fanq19SJhBuYzpbWFCVeGkhnrDDJTANbHBvSDajDyVtOe6j9NM9Uzo1jCyp/LRZdKAuQLk0+u4AZBAoLy/DOEOcxphH9IHw6FUTBq9eoYB2wnET4u+POeDBfVgpQwKyXbC0E= 13 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # CONTRIBUTING 2 | 3 | * [Contributing to Open Source guide](https://guides.github.com/activities/contributing-to-open-source/) 4 | 5 | ## Steps 6 | 7 | 1. Create an Issue 8 | 2. Pull Request 9 | 3. Merge \o/ 10 | 11 | ## Contributors 12 | 13 | * Original author: [Gaetan DELBART](https://github.com/gagaXD), [Titouan BENOIT](https://github.com/Nightbr) 14 | * Mailjet API 15 | -------------------------------------------------------------------------------- /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 2 | 3 | [![Build Status](https://travis-ci.org/mailjet/laravel-mailjet.svg?branch=master)](https://travis-ci.org/mailjet/laravel-mailjet) 4 | [![Packagist](https://img.shields.io/packagist/v/mailjet/laravel-mailjet.svg)](https://packagist.org/packages/mailjet/laravel-mailjet) 5 | [![Packagist](https://img.shields.io/packagist/dt/mailjet/laravel-mailjet.svg)](https://packagist.org/packages/mailjet/laravel-mailjet) 6 | [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/mailjet/laravel-mailjet/blob/master/LICENSE.md) 7 | [![Documentation](https://img.shields.io/badge/documentation-gh--pages-blue.svg)](https://mailjet.github.io/laravel-mailjet/) 8 | 9 | Laravel package for handling Mailjet API v3 using this wrapper: 10 | 11 | It also provides a mailjetTransport for [Laravel mail feature](https://laravel.com/docs/master/mail) 12 | 13 | ## Installation 14 | 15 | First, include the package in your dependencies: 16 | 17 | composer require mailjet/laravel-mailjet 18 | 19 | Then, you need to add some informations in your configuration files. You can find your Mailjet API key/secret [here](https://app.mailjet.com/account/api_keys). 20 | Please also set your email from address and name. 21 | 22 | * In the providers array: 23 | 24 | ```php 25 | 'providers' => [ 26 | ... 27 | Mailjet\LaravelMailjet\MailjetServiceProvider::class, 28 | ... 29 | ] 30 | ``` 31 | 32 | ## Laravel 11.0+ 33 | In the file `example-app/bootstrap/providers.php` 34 | ```php 35 | use Mailjet\LaravelMailjet\MailjetServiceProvider; 36 | 37 | return [ 38 | App\Providers\AppServiceProvider::class, 39 | MailjetServiceProvider::class, 40 | ]; 41 | ```` 42 | 43 | * In the aliases array: 44 | 45 | ```php 46 | 'aliases' => [ 47 | ... 48 | 'Mailjet' => Mailjet\LaravelMailjet\Facades\Mailjet::class, 49 | ... 50 | ] 51 | ``` 52 | 53 | * In the services.php file: 54 | 55 | ```php 56 | 'mailjet' => [ 57 | 'key' => env('MAILJET_APIKEY'), 58 | 'secret' => env('MAILJET_APISECRET'), 59 | ] 60 | ``` 61 | 62 | * In your .env file: 63 | 64 | ```php 65 | MAILJET_APIKEY=YOUR_APIKEY 66 | MAILJET_APISECRET=YOUR_APISECRET 67 | MAIL_FROM_ADDRESS=YOUR_EMAIL_FROM_ADDRESS 68 | MAIL_FROM_NAME=YOU_FROM_NAME 69 | ``` 70 | 71 | ## Full configuration 72 | 73 | For details head to [configuration doc](docs/configuration.md). 74 | 75 | ## Mail driver configuration 76 | 77 | In order to use Mailjet as your Mail driver, you need to update the mail driver in your `config/mail.php` or your `.env` file to `MAIL_MAILER=mailjet` (for Laravel 6 and older use MAIL_DRIVER constant instead), and make sure you are using a valid and authorised from email address configured on your Mailjet account. The sending email addresses and domain can be managed [here](https://app.mailjet.com/account/sender) 78 | 79 | For Laravel 7+ you also need to specify new available mail driver in config/mail.php: 80 | ``` 81 | 'mailers' => [ 82 | ... 83 | 84 | 'mailjet' => [ 85 | 'transport' => 'mailjet', 86 | ], 87 | ], 88 | ``` 89 | For usage, please check the [Laravel mail documentation](https://laravel.com/docs/master/mail) 90 | 91 | ## Usage 92 | 93 | In order to usage this package, you first need to import Mailjet Facade in your code: 94 | 95 | use Mailjet\LaravelMailjet\Facades\Mailjet; 96 | 97 | 98 | Then, in your code you can use one of the methods available in the MailjetServices. 99 | 100 | Low level API methods: 101 | 102 | * `Mailjet::get($resource, $args, $options)` 103 | * `Mailjet::post($resource, $args, $options)` 104 | * `Mailjet::put($resource, $args, $options)` 105 | * `Mailjet::delete($resource, $args, $options)` 106 | 107 | High level API methods: 108 | 109 | * `Mailjet::getAllLists($filters)` 110 | * `Mailjet::createList($body)` 111 | * `Mailjet::getListRecipients($filters)` 112 | * `Mailjet::getSingleContact($id)` 113 | * `Mailjet::createContact($body)` 114 | * `Mailjet::createListRecipient($body)` 115 | * `Mailjet::editListrecipient($id, $body)` 116 | 117 | For more informations about the filters you can use in each methods, refer to the [Mailjet API documentation](https://dev.mailjet.com/email-api/v3/apikey/) 118 | 119 | All method return `Mailjet\Response` or throw a `MailjetException` in case of API error. 120 | 121 | You can also get the Mailjet API client with the method `getClient()` and make your own custom request to Mailjet API. 122 | 123 | If you need to delete a contact, you need to register ContactsServiceProvider: 124 | * In the providers array: 125 | 126 | ```php 127 | 'providers' => [ 128 | ... 129 | \Mailjet\LaravelMailjet\Providers\ContactsServiceProvider::class, 130 | ... 131 | ] 132 | ``` 133 | 134 | and use it: 135 | ```php 136 | public function handle(ContactsV4Service $contactsV4Service) 137 | { 138 | $response = $contactsV4Service->delete(351406781); 139 | ... 140 | } 141 | ``` 142 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mailjet/laravel-mailjet", 3 | "description": "Laravel package for Mailjet API V3 and Laravel Mailjet Mail Transport", 4 | "keywords": [ 5 | "framework", 6 | "laravel", 7 | "mailjet", 8 | "mailjet wrapper", 9 | "transport", 10 | "mailjet API" 11 | ], 12 | "license": "MIT", 13 | "type": "library", 14 | "homepage": "https://github.com/mailjet/laravel-mailjet", 15 | "authors": [ 16 | { 17 | "name": "Gaetan DELBART", 18 | "email": "gaetan@moltencore.io", 19 | "homepage": "https://github.com/gagaXD" 20 | }, 21 | { 22 | "name": "Titouan BENOIT", 23 | "email": "titouan.benoit@gmx.fr", 24 | "homepage": "https://github.com/Nightbr" 25 | }, 26 | { 27 | "name": "Mailjet API", 28 | "email": "api@mailjet.com", 29 | "homepage": "https://dev.mailjet.com/" 30 | } 31 | ], 32 | "require": { 33 | "php": "^7.4|^8.0", 34 | "laravel/framework": "^9.0|^10.0|^11.0|^12.0", 35 | "mailjet/mailjet-apiv3-php": "^1.5.6|^1.5", 36 | "symfony/http-client": "^7.1", 37 | "symfony/mailjet-mailer": "^6.0" 38 | }, 39 | "require-dev": { 40 | "fakerphp/faker": "~1", 41 | "mockery/mockery": "0.9.*|^1.0", 42 | "orchestra/testbench": "3.6|^4.0|^5.0|^6.0|^8.0|^9.0", 43 | "phpcompatibility/php-compatibility": "^9.3", 44 | "phpunit/phpunit": "~7.0|^8.0|^9|^10.0" 45 | }, 46 | "autoload": { 47 | "psr-4": { 48 | "Mailjet\\LaravelMailjet\\": "src" 49 | } 50 | }, 51 | "autoload-dev": { 52 | "psr-4": { 53 | "Mailjet\\LaravelMailjet\\Tests\\": "tests" 54 | } 55 | }, 56 | "extra": { 57 | "laravel": { 58 | "providers": [ 59 | "Mailjet\\LaravelMailjet\\MailjetServiceProvider" 60 | ], 61 | "aliases": { 62 | "Mailjet": "Mailjet\\LaravelMailjet\\Facades\\Mailjet" 63 | } 64 | } 65 | }, 66 | "config": { 67 | "sort-packages": true, 68 | "bin-dir": "bin" 69 | }, 70 | "scripts": { 71 | "post-install-cmd": "\"bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility", 72 | "post-update-cmd" : "\"bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility" 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name: Laravel Mailjet 2 | site_description: Laravel Mailjet API V3. 3 | site_author: Titouan BENOIT 4 | copyright: MIT 5 | repo_url: https://github.com/mailjet/laravel-mailjet 6 | edit_uri: edit/master/docs/ 7 | docs_dir: 'docs' 8 | site_dir: "doc" 9 | pages: 10 | - Home: index.md 11 | - Configuration: configuration.md 12 | - Examples: usage.md 13 | - Mailjet Doc: mailjet-doc.md 14 | - Contributing: CONTRIBUTING.md 15 | theme_dir: 'docs/custom_theme/material' 16 | extra_css: 17 | - https://dev.mailjet.com/static/stylesheets/styles-redesign.css 18 | - stylesheets/extra.css 19 | - https://cdn.rawgit.com/noelboss/featherlight/1.5.1/release/featherlight.min.css 20 | extra_javascript: 21 | - https://dev.mailjet.com/static/javascripts/jquery-2.1.1.min.js 22 | - https://cdn.jsdelivr.net/ace/1.1.3/min/ace.js 23 | - https://cdn.jsdelivr.net/clipboard.js/1.5.10/clipboard.min.js 24 | - https://dev.mailjet.com/static/javascripts/my/ace-editor-redesign.js 25 | - https://cdn.rawgit.com/noelboss/featherlight/1.5.1/release/featherlight.min.js 26 | markdown_extensions: 27 | - pymdownx.github -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | 13 | ./tests 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/Contracts/CampaignContract.php: -------------------------------------------------------------------------------- 1 | getStatus() ?? 0; 38 | $message = "{$message}: {$response->getReasonPhrase()}"; 39 | 40 | $this->setErrorFromResponse($response); 41 | } 42 | 43 | parent::__construct($message, $statusCode, $previous); 44 | } 45 | /** 46 | * Configure MailjetException from Mailjet\Response. 47 | * 48 | * @param Response $response 49 | */ 50 | private function setErrorFromResponse(Response $response): void 51 | { 52 | $body = $response->getBody(); 53 | 54 | if (isset($body['ErrorInfo'])) { 55 | $this->errorInfo = $body['ErrorInfo']; 56 | } 57 | if (isset($body['ErrorMessage'])) { 58 | $this->errorMessage = $body['ErrorMessage']; 59 | } 60 | if (isset($body['ErrorIdentifier'])) { 61 | $this->errorIdentifier = $body['ErrorIdentifier']; 62 | } 63 | } 64 | 65 | /** 66 | * @return string 67 | */ 68 | public function getErrorInfo(): string 69 | { 70 | return $this->errorInfo; 71 | } 72 | 73 | /** 74 | * @return string 75 | */ 76 | public function getErrorMessage(): string 77 | { 78 | return $this->errorMessage; 79 | } 80 | 81 | /** 82 | * @return string 83 | */ 84 | public function getErrorIdentifier(): string 85 | { 86 | return $this->errorIdentifier; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Facades/Mailjet.php: -------------------------------------------------------------------------------- 1 | create( 25 | new Dsn( 26 | 'mailjet+api', 27 | 'default', 28 | config('services.mailjet.key'), 29 | config('services.mailjet.secret') 30 | ) 31 | ); 32 | }); 33 | } 34 | 35 | /** 36 | * Register the application services. 37 | * 38 | * @return void 39 | */ 40 | public function register(): void 41 | { 42 | $this->app->singleton('Mailjet', function () { 43 | $config = $this->app['config']->get('services.mailjet', []); 44 | $call = $this->app['config']->get('services.mailjet.common.call', true); 45 | $options = $this->app['config']->get('services.mailjet.common.options', []); 46 | 47 | if (!isset($config['key'], $config['secret'])) { 48 | throw new InvalidArgumentException('The Mailjet service is not configured.'); 49 | } 50 | 51 | return new MailjetService($config['key'], $config['secret'], $call, $options); 52 | }); 53 | } 54 | 55 | /** 56 | * @return array 57 | */ 58 | public function provides(): array 59 | { 60 | return ['mailjet']; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /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; 70 | $this->sender = $sender; 71 | $this->senderEmail = $senderEmail; 72 | $this->subject = $subject; 73 | $this->contactsListId = $contactsListId; 74 | $this->optionalProperties = $optionalProperties; 75 | } 76 | 77 | /** 78 | * Format CampaignDraft for MailJet API request. 79 | * 80 | * @return array 81 | */ 82 | public function format(): array 83 | { 84 | $result = [ 85 | self::LOCALE_KEY => $this->locale, 86 | self::SENDER_KEY => $this->sender, 87 | self::SENDER_EMAIL_KEY => $this->senderEmail, 88 | self::SUBJECT_KEY => $this->subject, 89 | self::CONTACT_LIST_ID_KEY => $this->contactsListId 90 | ]; 91 | 92 | return array_merge($result, $this->optionalProperties); 93 | } 94 | 95 | /** 96 | * Get CampaignDraft content. 97 | * 98 | * @return array|null 99 | */ 100 | public function getContent(): ?array 101 | { 102 | return $this->content; 103 | } 104 | 105 | /** 106 | * Set CampaignDraft content. 107 | * 108 | * @param $content 109 | * 110 | * @return \Mailjet\LaravelMailjet\Model\CampaignDraft 111 | */ 112 | public function setContent($content): CampaignDraft 113 | { 114 | $this->content = $content; 115 | 116 | return $this; 117 | } 118 | 119 | /** 120 | * Get CampaignDraft id. 121 | * 122 | * @return string|null 123 | */ 124 | public function getId(): ?string 125 | { 126 | return $this->id; 127 | } 128 | 129 | /** 130 | * Set CampaignDraft id. 131 | * 132 | * @param string $id 133 | * 134 | * @return \Mailjet\LaravelMailjet\Model\CampaignDraft 135 | */ 136 | public function setId(string $id): CampaignDraft 137 | { 138 | $this->id = $id; 139 | 140 | return $this; 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /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 | * @return string 67 | */ 68 | public function getEmail(): string 69 | { 70 | return $this->email; 71 | } 72 | 73 | /** 74 | * Set contact email. 75 | * 76 | * @param string $email 77 | * 78 | * @return Contact 79 | */ 80 | public function setEmail(string $email): Contact 81 | { 82 | $this->email = $email; 83 | 84 | return $this; 85 | } 86 | 87 | /** 88 | * Correspond to Name in MailJet request. 89 | * @return string|null 90 | */ 91 | public function getName(): ?string 92 | { 93 | return $this->optionalProperties[self::NAME_KEY] ?? null; 94 | } 95 | 96 | /** 97 | * Set contact name. 98 | * 99 | * @param string $name 100 | * 101 | * @return Contact 102 | */ 103 | public function setName(string $name): Contact 104 | { 105 | $this->optionalProperties[self::NAME_KEY] = $name; 106 | 107 | return $this; 108 | } 109 | 110 | /** 111 | * Action to the contact for Synchronization. 112 | * 113 | * @return string|null 114 | */ 115 | public function getAction(): ?string 116 | { 117 | return $this->action; 118 | } 119 | 120 | /** 121 | * Action to the contact for Synchronization. 122 | * 123 | * @param string $action (ACTION_* const) 124 | * 125 | * @return Contact 126 | */ 127 | public function setAction(string $action): Contact 128 | { 129 | if (! $this->validateAction($action)) { 130 | throw new RuntimeException("$action: is not a valid Action."); 131 | } 132 | 133 | $this->action = $action; 134 | 135 | return $this; 136 | } 137 | 138 | /** 139 | * Validate action. 140 | * 141 | * @param string $action 142 | * 143 | * @return bool 144 | */ 145 | protected function validateAction(string $action): bool 146 | { 147 | $available = [self::ACTION_ADDFORCE, self::ACTION_ADDNOFORCE, self::ACTION_REMOVE, self::ACTION_UNSUB]; 148 | 149 | return in_array($action, $available); 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /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)) { 42 | throw new RuntimeException("$action: is not a valid Action."); 43 | } 44 | 45 | $this->listId = $listId; 46 | $this->action = $action; 47 | $this->contacts = $contacts; 48 | } 49 | 50 | /** 51 | * Format contactList for MailJet API request. 52 | * 53 | * @return array 54 | */ 55 | public function format(): array 56 | { 57 | $result = [ 58 | 'Action' => $this->action, 59 | ]; 60 | 61 | $result['Contacts'] = array_map(static function (Contact $contact) { 62 | return $contact->format(); 63 | }, $this->contacts); 64 | 65 | return $result; 66 | } 67 | 68 | /** 69 | * Get list id 70 | * @return string 71 | */ 72 | public function getListId(): string 73 | { 74 | return $this->listId; 75 | } 76 | 77 | /** 78 | * Get action. 79 | * @return string 80 | */ 81 | public function getAction(): string 82 | { 83 | return $this->action; 84 | } 85 | 86 | /** 87 | * Set action. 88 | * @param string $action 89 | * @return ContactsList 90 | */ 91 | public function setAction(string $action): ContactsList 92 | { 93 | if (! $this->validateAction($action)) { 94 | throw new RuntimeException("$action: is not a valid Action."); 95 | } 96 | 97 | $this->action = $action; 98 | 99 | return $this; 100 | } 101 | 102 | /** 103 | * Get contacts. 104 | * @return array 105 | */ 106 | public function getContacts(): array 107 | { 108 | return $this->contacts; 109 | } 110 | 111 | /** 112 | * Validate action name. 113 | * 114 | * @param string $action 115 | * 116 | * @return bool 117 | */ 118 | protected function validateAction(string $action): bool 119 | { 120 | $actionsAvailable = [self::ACTION_ADDFORCE, self::ACTION_ADDNOFORCE, self::ACTION_REMOVE, self::ACTION_UNSUB]; 121 | 122 | return in_array($action, $actionsAvailable); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/Model/EventCallbackUrl.php: -------------------------------------------------------------------------------- 1 | validateType($type)) { 79 | throw new RuntimeException("$type: is not a valid event type."); 80 | } 81 | 82 | if (!$this->validateStatus($status)) { 83 | throw new RuntimeException("$status: is not a valid event status."); 84 | } 85 | 86 | $this->url = $url; 87 | $this->type = $type; 88 | $this->isBackup = $isBackup; 89 | $this->status = $status; 90 | $this->version = $version; 91 | $this->apiKeyId = $apiKeyId; 92 | $this->groupEvent = $groupEvent; 93 | } 94 | 95 | /** 96 | * Format contactList for MailJet API request. 97 | * 98 | * @return array 99 | */ 100 | public function format(): array 101 | { 102 | if ($this->groupEvent) { 103 | // Events are grouped only in API version 2. 104 | $this->version = 2; 105 | } 106 | 107 | $result = [ 108 | 'Url' => $this->url, 109 | 'EventType' => $this->type, 110 | 'IsBackup' => $this->isBackup, 111 | 'Status' => $this->status, 112 | 'Version' => $this->version, 113 | ]; 114 | 115 | if ($this->apiKeyId) { 116 | $result['APIKeyID'] = $this->apiKeyId; 117 | } 118 | 119 | return $result; 120 | } 121 | 122 | /** 123 | * Validate event type. 124 | * 125 | * @param string $type 126 | * 127 | * @return bool 128 | */ 129 | protected function validateType(string $type): bool 130 | { 131 | $available = [ 132 | self::EVENT_TYPE_OPEN, 133 | self::EVENT_TYPE_CLICK, 134 | self::EVENT_TYPE_BOUNCE, 135 | self::EVENT_TYPE_SPAM, 136 | self::EVENT_TYPE_BLOCKED, 137 | self::EVENT_TYPE_UNSUB, 138 | self::EVENT_TYPE_SENT, 139 | ]; 140 | 141 | return in_array($type, $available); 142 | } 143 | 144 | /** 145 | * Validate event status. 146 | * 147 | * @param string $status 148 | * 149 | * @return bool 150 | */ 151 | protected function validateStatus(string $status): bool 152 | { 153 | $available = [self::EVENT_STATUS_ALIVE, self::EVENT_STATUS_DEAD]; 154 | 155 | return in_array($status, $available); 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /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; 36 | $this->optionalProperties = $optionalProperties; 37 | } 38 | 39 | /** 40 | * Format Template for MailJet API request. 41 | * 42 | * @return array 43 | */ 44 | public function format(): array 45 | { 46 | $result[self::NAME_KEY] = $this->name; 47 | 48 | return array_merge($result, $this->optionalProperties); 49 | } 50 | 51 | /** 52 | * Get Template content 53 | * 54 | * @return array|null $content 55 | */ 56 | public function getContent(): ?array 57 | { 58 | return $this->content; 59 | } 60 | 61 | /** 62 | * Set Template content. 63 | * 64 | * @param array $content 65 | * 66 | * @return \Mailjet\LaravelMailjet\Model\Template 67 | */ 68 | public function setContent(array $content): Template 69 | { 70 | $this->content = $content; 71 | 72 | return $this; 73 | } 74 | 75 | /** 76 | * Get id. 77 | * 78 | * @return string|null 79 | */ 80 | public function getId(): ?string 81 | { 82 | return $this->id; 83 | } 84 | 85 | /** 86 | * Set id. 87 | * 88 | * @param string $id 89 | * 90 | * @return \Mailjet\LaravelMailjet\Model\Template 91 | */ 92 | public function setId(string $id): Template 93 | { 94 | $this->id = $id; 95 | 96 | return $this; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /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/ContactsServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind(ContactsV4Service::class, function($app) { 37 | $config = $this->app['config']->get('services.mailjet', []); 38 | $call = $this->app['config']->get('services.mailjet.v4.call', true); 39 | $options = $this->app['config']->get('services.mailjet.v4.options', []); 40 | 41 | $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); 42 | 43 | return new ContactsV4Service($mailjetService); 44 | }); 45 | } 46 | 47 | /** 48 | * Get the services provided by the provider. 49 | * 50 | * @return array 51 | */ 52 | public function provides() 53 | { 54 | return [ContactsV4Service::class]; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /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 | * @param array|null $filters 31 | * @return array 32 | * @throws 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 | * @param string $id 48 | * @return array 49 | * @throws MailjetException 50 | */ 51 | public function findByCampaignDraftId(string $id): array 52 | { 53 | $response = $this->mailjet->get(Resources::$Campaigndraft, 54 | ['id' => $id]); 55 | if (! $response->success()) { 56 | $this->throwError('CampaignDraftService:findByCampaignDraftId() failed', 57 | $response); 58 | } 59 | 60 | return $response->getData(); 61 | } 62 | 63 | /** 64 | * create a new fresh CampaignDraft 65 | * @param Campaigndraft $campaignDraft 66 | * @return array 67 | * @throws MailjetException 68 | */ 69 | public function create(CampaignDraft $campaignDraft): array 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 | * @param string $id 83 | * @param Campaigndraft $campaignDraft 84 | * @return array 85 | * @throws MailjetException 86 | */ 87 | public function update(string $id, CampaignDraft $campaignDraft): array 88 | { 89 | $response = $this->mailjet->put(Resources::$Campaigndraft, 90 | ['id' => $id, 'body' => $campaignDraft->format()]); 91 | if (! $response->success()) { 92 | $this->throwError('CampaignDraftService:update() failed', $response); 93 | } 94 | 95 | return $response->getData(); 96 | } 97 | 98 | /** 99 | * Return the text and html contents of the campaigndraft 100 | * @param string $id 101 | * @return array 102 | * @throws MailjetException 103 | */ 104 | public function getDetailContent(string $id): array 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 | * @param string $id 119 | * @param array $contentData 120 | * @return array 121 | * @throws MailjetException 122 | */ 123 | public function createDetailContent(string $id, array $contentData): array 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 | * @param string $id 138 | * @return array 139 | * @throws MailjetException 140 | */ 141 | public function getSchedule(string $id): array 142 | { 143 | $response = $this->mailjet->get(Resources::$CampaigndraftSchedule, 144 | ['id' => $id]); 145 | if (! $response->success()) { 146 | $this->throwError('CampaignDraftService:getSchedule failed', 147 | $response); 148 | } 149 | 150 | return $response->getData(); 151 | } 152 | 153 | /** 154 | * Schedule when the campaigndraft will be sent 155 | * @param string $id 156 | * @param string $date (RFC3339 format "Y-m-d\TH:i:sP") 157 | * @return array 158 | * @throws MailjetException 159 | */ 160 | public function scheduleCampaign(string $id, string $date): array 161 | { 162 | $response = $this->mailjet->post(Resources::$CampaigndraftSchedule, 163 | ['id' => $id, 'body' => $date]); 164 | if (! $response->success()) { 165 | $this->throwError('CampaignDraftService:scheduleCampaign failed', 166 | $response); 167 | } 168 | 169 | return $response->getData(); 170 | } 171 | 172 | /** 173 | * Update the date when the campaigndraft will be sent 174 | * @param string $id 175 | * @param string $date 176 | * @return array 177 | * @throws MailjetException 178 | */ 179 | public function updateCampaignSchedule(string $id, string $date): array 180 | { 181 | $response = $this->mailjet->put(Resources::$CampaigndraftSchedule, 182 | ['id' => $id, 'body' => $date]); 183 | if (! $response->success()) { 184 | $this->throwError('CampaignDraftService:updateCampaignSchedule failed', 185 | $response); 186 | } 187 | 188 | return $response->getData(); 189 | } 190 | 191 | /** 192 | * Cancel a future sending 193 | * @param string $id 194 | * @return array 195 | * @throws MailjetException 196 | */ 197 | public function removeSchedule(string $id): array 198 | { 199 | $response = $this->mailjet->delete(Resources::$CampaigndraftSchedule, 200 | ['id' => $id]); 201 | if (! $response->success()) { 202 | $this->throwError('CampaignDraftService:removeSchedule failed', 203 | $response); 204 | } 205 | 206 | return $response->getData(); 207 | } 208 | 209 | /** 210 | * Send the campaign immediately 211 | * @param string $id 212 | * @return array 213 | * @throws MailjetException 214 | */ 215 | public function sendCampaign(string $id): array 216 | { 217 | $response = $this->mailjet->post(Resources::$CampaigndraftSend, 218 | ['id' => $id]); 219 | if (! $response->success()) { 220 | $this->throwError('CampaignDraftService:sendCampaign failed', 221 | $response); 222 | } 223 | 224 | return $response->getData(); 225 | } 226 | 227 | /** 228 | * Return the status of a CampaignDraft 229 | * @param string $id 230 | * @return array 231 | * @throws MailjetException 232 | */ 233 | public function getCampaignStatus(string $id): array 234 | { 235 | $response = $this->mailjet->get(Resources::$CampaigndraftStatus, 236 | ['id' => $id]); 237 | if (! $response->success()) { 238 | $this->throwError('CampaignDraftService:getCampaignStatus failed', 239 | $response); 240 | } 241 | 242 | return $response->getData(); 243 | } 244 | 245 | /** 246 | * An action to test a CampaignDraft. 247 | * @param string $id 248 | * @param array $recipients 249 | * @return array 250 | * @throws MailjetException 251 | */ 252 | public function testCampaign(string $id, array $recipients): array 253 | { 254 | $response = $this->mailjet->post(Resources::$CampaigndraftTest, 255 | ['id' => $id, 'body' => $recipients]); 256 | if (! $response->success()) { 257 | $this->throwError('CampaignDraftService:testCampaign failed', 258 | $response); 259 | } 260 | 261 | return $response->getData(); 262 | } 263 | 264 | /** 265 | * @param $title 266 | * @param Response $response 267 | * @throws MailjetException 268 | */ 269 | private function throwError($title, Response $response): void 270 | { 271 | throw new MailjetException(0, $title, $response); 272 | } 273 | } 274 | -------------------------------------------------------------------------------- /src/Services/CampaignService.php: -------------------------------------------------------------------------------- 1 | mailjet = $mailjet; 25 | } 26 | 27 | /** 28 | * List campaigns resources available for this apikey 29 | * @param array|null $filters 30 | * @return array 31 | * @throws 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 | * @param string $id 47 | * @return array 48 | * @throws MailjetException 49 | */ 50 | public function findByCampaignId(string $id): array 51 | { 52 | $response = $this->mailjet->get(Resources::$Campaign, ['id' => $id]); 53 | 54 | if (! $response->success()) { 55 | throw new MailjetException(0, 'CampaignService:findByCampaignId() failed', $response); 56 | } 57 | 58 | return $response->getData(); 59 | } 60 | 61 | /** 62 | * Find a given campaign by Newsletter / CampaignDraft id. 63 | * 64 | * @param string $id 65 | * 66 | * @return array 67 | * @throws MailjetException 68 | */ 69 | public function findByNewsletterId(string $id): array 70 | { 71 | $response = $this->mailjet->get(Resources::$Campaign, ['mj.nl' => $id]); 72 | 73 | if (! $response->success()) { 74 | throw new MailjetException(0, 'CampaignService:findByNewsletterId() failed', $response); 75 | } 76 | 77 | return $response->getData(); 78 | } 79 | 80 | /** 81 | * Update one specific campaign resource with a PUT request. 82 | * @param string $id 83 | * @param Campaign $campaign 84 | * @return array 85 | * @throws MailjetException 86 | */ 87 | public function updateCampaign(string $id, Campaign $campaign): array 88 | { 89 | $response = $this->mailjet->put(Resources::$Campaign, ['id' => $id, 'body' => $campaign->format()]); 90 | 91 | if (! $response->success()) { 92 | throw new MailjetException(0, 'CampaignService:updateCampaign() failed', $response); 93 | } 94 | 95 | return $response->getData(); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/Services/ContactMetadataService.php: -------------------------------------------------------------------------------- 1 | mailjet = $mailjet; 25 | } 26 | 27 | /** 28 | * Retrieve all ContactMetadata. 29 | * @return array 30 | * @throws MailjetException 31 | */ 32 | public function getAll(): array 33 | { 34 | $response = $this->mailjet->get(Resources::$Contactmetadata); 35 | 36 | if (! $response->success()) { 37 | throw new MailjetException(0, 'ContactMetadataService:getAll() failed', $response); 38 | } 39 | 40 | return $response->getData(); 41 | } 42 | 43 | /** 44 | * Retrieve one ContactMetadata. 45 | * @param string $id 46 | * @return array 47 | * @throws MailjetException 48 | */ 49 | public function get(string $id): array 50 | { 51 | $response = $this->mailjet->get(Resources::$Contactmetadata, ['id' => $id]); 52 | 53 | if (! $response->success()) { 54 | throw new MailjetException(0, 'ContactMetadataService:get() failed', $response); 55 | } 56 | 57 | return $response->getData(); 58 | } 59 | 60 | /** 61 | * create a new fresh ContactMetadata 62 | * @param ContactMetadata $metadata 63 | * @return array 64 | * @throws MailjetException 65 | */ 66 | public function create(ContactMetadata $metadata): array 67 | { 68 | $response = $this->mailjet->post(Resources::$Contactmetadata, ['body' => $metadata->format()]); 69 | 70 | if (! $response->success()) { 71 | throw new MailjetException(0, 'ContactMetadataService:create() failed', $response); 72 | } 73 | 74 | return $response->getData(); 75 | } 76 | 77 | /** 78 | * Update one ContactMetadata 79 | * @param string $id 80 | * @param ContactMetadata $metadata 81 | * @return array 82 | * @throws MailjetException 83 | */ 84 | public function update(string $id, ContactMetadata $metadata): array 85 | { 86 | $response = $this->mailjet->put(Resources::$Contactmetadata, ['id' => $id, 'body' => $metadata->format()]); 87 | 88 | if (! $response->success()) { 89 | throw new MailjetException(0, 'ContactMetadataService:update() failed', $response); 90 | } 91 | 92 | return $response->getData(); 93 | } 94 | 95 | /** 96 | * Delete one ContactMetadata 97 | * @param string $id 98 | * @return array 99 | * @throws MailjetException 100 | */ 101 | public function delete(string $id): array 102 | { 103 | $response = $this->mailjet->delete(Resources::$Contactmetadata, ['id' => $id]); 104 | 105 | if (! $response->success()) { 106 | throw new MailjetException(0, 'ContactMetadataService:delete() failed', $response); 107 | } 108 | 109 | return $response->getData(); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/Services/ContactsListService.php: -------------------------------------------------------------------------------- 1 | mailjet = $mailjet; 32 | } 33 | 34 | /** 35 | * Create a new fresh Contact to listId. 36 | * @param string $id 37 | * @param Contact $contact 38 | * @param string $action 39 | * @return array 40 | * @throws MailjetException 41 | */ 42 | public function create(string $id, Contact $contact, string $action = Contact::ACTION_ADDFORCE): array 43 | { 44 | $contact->setAction($action); 45 | 46 | $response = $this->_exec($id, $contact); 47 | 48 | if (! $response->success()) { 49 | throw new MailjetException(0, 'ContactsListService:create() failed', $response); 50 | } 51 | 52 | return $response->getData(); 53 | } 54 | 55 | /** 56 | * Update a Contact to listId. 57 | * @param string $id 58 | * @param Contact $contact 59 | * @param string $action 60 | * @return array 61 | * @throws MailjetException 62 | */ 63 | public function update(string $id, Contact $contact, string $action = Contact::ACTION_ADDNOFORCE): array 64 | { 65 | $contact->setAction($action); 66 | 67 | $response = $this->_exec($id, $contact); 68 | 69 | if (! $response->success()) { 70 | throw new MailjetException(0, 'ContactsListService:update() failed', $response); 71 | } 72 | 73 | return $response->getData(); 74 | } 75 | 76 | /** 77 | * Re/subscribe a Contact to listId. 78 | * 79 | * @param string $id 80 | * @param Contact $contact 81 | * @param bool $force 82 | * 83 | * @return array 84 | * @throws MailjetException 85 | */ 86 | public function subscribe(string $id, Contact $contact, bool $force = true): array 87 | { 88 | $contact->setAction($force ? Contact::ACTION_ADDFORCE : Contact::ACTION_ADDNOFORCE); 89 | 90 | $response = $this->_exec($id, $contact); 91 | 92 | if (! $response->success()) { 93 | throw new MailjetException(0, 'ContactsListService:subscribe() failed', $response); 94 | } 95 | 96 | return $response->getData(); 97 | } 98 | 99 | /** 100 | * Unsubscribe a Contact from listId. 101 | * 102 | * @param string $id 103 | * @param Contact $contact 104 | * 105 | * @return array 106 | * @throws MailjetException 107 | */ 108 | public function unsubscribe(string $id, Contact $contact): array 109 | { 110 | $contact->setAction(Contact::ACTION_UNSUB); 111 | 112 | $response = $this->_exec($id, $contact); 113 | 114 | if (! $response->success()) { 115 | throw new MailjetException(0, 'ContactsListService:unsubscribe() failed', $response); 116 | } 117 | 118 | return $response->getData(); 119 | } 120 | 121 | /** 122 | * Delete a Contact from listId 123 | * 124 | * @param string $id 125 | * @param Contact $contact 126 | * 127 | * @return array 128 | * @throws MailjetException 129 | */ 130 | public function delete(string $id, Contact $contact): array 131 | { 132 | $contact->setAction(Contact::ACTION_REMOVE); 133 | 134 | $response = $this->_exec($id, $contact); 135 | 136 | if (! $response->success()) { 137 | throw new MailjetException(0, 'ContactsListService:delete() failed', $response); 138 | } 139 | 140 | return $response->getData(); 141 | } 142 | 143 | /** 144 | * Change email a Contact. 145 | * 146 | * @param string $id 147 | * @param Contact $contact 148 | * @param string $oldEmail 149 | * 150 | * @return array 151 | * @throws MailjetException 152 | */ 153 | public function updateEmail(string $id, Contact $contact, string $oldEmail): array 154 | { 155 | $response = $this->mailjet->get(Resources::$Contactdata, ['id' => $oldEmail]); 156 | 157 | if (! $response->success()) { 158 | throw new MailjetException(0, 'ContactsListService:changeEmail() failed', $response); 159 | } 160 | 161 | $oldContactData = $response->getData(); 162 | 163 | if (isset($oldContactData[0])) { 164 | $contact->setOptionalProperties($oldContactData[0]['Data']); 165 | } 166 | 167 | $contact->setAction(Contact::ACTION_ADDFORCE); 168 | $response = $this->_exec($id, $contact); 169 | 170 | if (! $response->success()) { 171 | throw new MailjetException(0, 'ContactsListService:changeEmail() failed', $response); 172 | } 173 | 174 | $oldContact = new Contact($oldEmail); 175 | $oldContact->setAction(Contact::ACTION_REMOVE); 176 | $response = $this->_exec($id, $oldContact); 177 | 178 | if (! $response->success()) { 179 | throw new MailjetException(0, 'ContactsListService:changeEmail() failed', $response); 180 | } 181 | 182 | return $response->getData(); 183 | } 184 | 185 | /** 186 | * Import many contacts to a list. 187 | * https://dev.mailjet.com/email-api/v3/contactslist-managemanycontacts/ 188 | * 189 | * @param ContactsList $list 190 | * 191 | * @return array 192 | * @throws MailjetException 193 | */ 194 | public function uploadManyContactsList(ContactsList $list): array 195 | { 196 | $batchResults = []; 197 | $contactChunks = array_chunk($list->getContacts(), self::CONTACT_BATCH_SIZE); 198 | 199 | foreach ($contactChunks as $contactChunk) { 200 | $subContactsList = new ContactsList($list->getListId(), $list->getAction(), $contactChunk); 201 | $currentBatch = $this->mailjet->post(Resources::$ContactslistManagemanycontacts, 202 | ['id' => $subContactsList->getListId(), 'body' => $subContactsList->format()] 203 | ); 204 | 205 | if ($currentBatch->success()) { 206 | $batchResults[] = $currentBatch->getData()[0]; 207 | } else { 208 | throw new MailjetException(0, 'ContactsListService:manageManyContactsList() failed', $currentBatch); 209 | } 210 | } 211 | 212 | return $batchResults; 213 | } 214 | 215 | /** 216 | * An action for adding a contact to a contact list. Only POST is supported. 217 | * The API will internally create the new contact if it does not exist, 218 | * add or update the name and properties. 219 | * The properties have to be defined before they can be used. 220 | * The API then adds the contact to the contact list with active=true 221 | * and unsub=specified value if it is not already in the list, 222 | * or updates the entry with these values. 223 | * On success, the API returns a packet with the same format 224 | * but with all properties available for that contact. 225 | * @param string $id 226 | * @param Contact $contact 227 | * @return Response 228 | * @throws MailjetException 229 | */ 230 | private function _exec(string $id, Contact $contact): Response 231 | { 232 | return $this->mailjet->post(Resources::$ContactslistManagecontact, 233 | ['id' => $id, 'body' => $contact->format()] 234 | ); 235 | } 236 | } 237 | -------------------------------------------------------------------------------- /src/Services/ContactsV4Service.php: -------------------------------------------------------------------------------- 1 | mailjet = $mailjet; 26 | } 27 | 28 | /** 29 | * Delete a Contact 30 | * @param int $id 31 | * @return bool 32 | * @throws MailjetException 33 | */ 34 | public function delete(int $id): bool 35 | { 36 | $response = $this->mailjet->delete(['contacts', ''], ['id' => $id]); 37 | 38 | if (!$response->success()) { 39 | throw new MailjetException(0, "ContactsV4Service:delete() failed", $response); 40 | } 41 | 42 | return 200 === $response->getStatus(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Services/EventCallbackUrlService.php: -------------------------------------------------------------------------------- 1 | mailjet = $mailjet; 25 | } 26 | 27 | /** 28 | * Retrieve all EventCallbackUrl. 29 | * @return array 30 | * @throws MailjetException 31 | */ 32 | public function getAll(): array 33 | { 34 | $response = $this->mailjet->get(Resources::$Eventcallbackurl); 35 | 36 | if (! $response->success()) { 37 | throw new MailjetException(0, 'EventCallbackUrlService:getAll() failed', $response); 38 | } 39 | 40 | return $response->getData(); 41 | } 42 | 43 | /** 44 | * Retrieve one EventCallbackUrl. 45 | * @param string $id 46 | * @return array 47 | * @throws MailjetException 48 | */ 49 | public function get(string $id): array 50 | { 51 | $response = $this->mailjet->get(Resources::$Eventcallbackurl, ['id' => $id]); 52 | 53 | if (! $response->success()) { 54 | throw new MailjetException(0, 'EventCallbackUrlService:get() failed', $response); 55 | } 56 | 57 | return $response->getData(); 58 | } 59 | 60 | /** 61 | * Create one EventCallbackUrl. 62 | * 63 | * @param EventCallbackUrl $url 64 | * 65 | * @return array 66 | * @throws MailjetException 67 | */ 68 | public function create(EventCallbackUrl $url): array 69 | { 70 | $response = $this->mailjet->post(Resources::$Eventcallbackurl, ['body' => $url->format()]); 71 | 72 | if (! $response->success()) { 73 | throw new MailjetException(0, 'EventCallbackUrlService:create() failed', $response); 74 | } 75 | 76 | return $response->getData(); 77 | } 78 | 79 | /** 80 | * Update one EventCallbackUrl. 81 | * 82 | * @param string $id 83 | * @param EventCallbackUrl $url 84 | * 85 | * @return array 86 | * @throws MailjetException 87 | */ 88 | public function update(string $id, EventCallbackUrl $url): array 89 | { 90 | $response = $this->mailjet->put(Resources::$Eventcallbackurl, ['id' => $id, 'body' => $url->format()]); 91 | 92 | if (! $response->success()) { 93 | throw new MailjetException(0, 'EventCallbackUrlService:update() failed', $response); 94 | } 95 | 96 | return $response->getData(); 97 | } 98 | 99 | /** 100 | * Delete one EventCallbackUrl. 101 | * 102 | * @param string $id 103 | * 104 | * @return array 105 | * @throws MailjetException 106 | */ 107 | public function delete(string $id): array 108 | { 109 | $response = $this->mailjet->delete(Resources::$Eventcallbackurl, ['id' => $id]); 110 | 111 | if (! $response->success()) { 112 | throw new MailjetException(0, 'EventCallbackUrlService:delete() failed', $response); 113 | } 114 | 115 | return $response->getData(); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/Services/MailjetService.php: -------------------------------------------------------------------------------- 1 | client = new Client($key, $secret, $call, $settings); 29 | } 30 | 31 | /** 32 | * Trigger a POST request. 33 | * @param array $resource Mailjet Resource/Action pair 34 | * @param array $args Request arguments 35 | * @param array $options 36 | * @return Response 37 | * @throws MailjetException 38 | */ 39 | public function post(array $resource, array $args = [], array $options = []): Response 40 | { 41 | $response = $this->client->post($resource, $args, $options); 42 | 43 | if (! $response->success()) { 44 | throw new MailjetException(0, 'MailjetService:post() failed', $response); 45 | } 46 | 47 | return $response; 48 | } 49 | 50 | /** 51 | * Trigger a GET request. 52 | * 53 | * @param array $resource Mailjet Resource/Action pair 54 | * @param array $args Request arguments 55 | * @param array $options 56 | * 57 | * @return Response 58 | * @throws MailjetException 59 | */ 60 | public function get(array $resource, array $args = [], array $options = []): Response 61 | { 62 | $response = $this->client->get($resource, $args, $options); 63 | 64 | if (! $response->success()) { 65 | throw new MailjetException(0, 'MailjetService:get() failed', $response); 66 | } 67 | 68 | return $response; 69 | } 70 | 71 | /** 72 | * Trigger a PUT request. 73 | * 74 | * @param array $resource Mailjet Resource/Action pair 75 | * @param array $args Request arguments 76 | * @param array $options 77 | * 78 | * @return Response 79 | * @throws MailjetException 80 | */ 81 | public function put(array $resource, array $args = [], array $options = []): Response 82 | { 83 | $response = $this->client->put($resource, $args, $options); 84 | 85 | if (! $response->success()) { 86 | throw new MailjetException(0, 'MailjetService:put() failed', $response); 87 | } 88 | 89 | return $response; 90 | } 91 | 92 | /** 93 | * Trigger a DELETE request. 94 | * 95 | * @param array $resource Mailjet Resource/Action pair 96 | * @param array $args Request arguments 97 | * @param array $options 98 | * 99 | * @return Response 100 | * @throws MailjetException 101 | */ 102 | public function delete(array $resource, array $args = [], array $options = []): Response 103 | { 104 | $response = $this->client->delete($resource, $args, $options); 105 | 106 | if (! $response->success()) { 107 | throw new MailjetException(0, 'MailjetService:delete() failed', $response); 108 | } 109 | 110 | return $response; 111 | } 112 | 113 | /** 114 | * Get all list on your Mailjet account. 115 | * TODO: Exclude HIGH Level API methods into managers. 116 | * @param array|null $filters Filters that will be use to filter the request 117 | * @return Response 118 | * @throws MailjetException 119 | */ 120 | public function getAllLists(?array $filters = null): Response 121 | { 122 | $response = $this->client->get(Resources::$Contactslist, ['filters' => $filters]); 123 | 124 | if (! $response->success()) { 125 | throw new MailjetException(0, 'MailjetService:getAllLists() failed', $response); 126 | } 127 | 128 | return $response; 129 | } 130 | 131 | /** 132 | * Create a new list. 133 | * @param array $body Information list - the 'Name' field is mandatory. 134 | * @return Response 135 | * @throws 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 | * @param array|null $filters Filters that will be use to filter the request. 151 | * @return Response 152 | * @throws MailjetException 153 | */ 154 | public function getListRecipients(?array $filters = null): Response 155 | { 156 | $response = $this->client->get(Resources::$Listrecipient, ['filters' => $filters]); 157 | 158 | if (! $response->success()) { 159 | throw new MailjetException(0, 'MailjetService:getListRecipients() failed', $response); 160 | } 161 | 162 | return $response; 163 | } 164 | 165 | /** 166 | * Get single contact information. 167 | * @param string $id 168 | * @return Response 169 | * @throws MailjetException 170 | */ 171 | public function getSingleContact(string $id): Response 172 | { 173 | $response = $this->client->get(Resources::$Contact, ['id' => $id]); 174 | 175 | if (! $response->success()) { 176 | throw new MailjetException(0, 'MailjetService:getSingleContact() failed', $response); 177 | } 178 | 179 | return $response; 180 | } 181 | 182 | /** 183 | * Create a contact. 184 | * 185 | * @param array $body Information list - the 'Email' field is mandatory. 186 | * 187 | * @return Response 188 | * @throws MailjetException 189 | */ 190 | public function createContact(array $body): Response 191 | { 192 | $response = $this->client->post(Resources::$Contact, ['body' => $body]); 193 | 194 | if (!$response->success()) { 195 | throw new MailjetException(0, 'MailjetService:createContact() failed', $response); 196 | } 197 | 198 | return $response; 199 | } 200 | 201 | /** 202 | * Create a list recipient (relationship between contact and list). 203 | * 204 | * @param array $body Information list - the 'ContactID' and 'ListID' parameters are mandatory. 205 | * 206 | * @return Response 207 | * @throws MailjetException 208 | */ 209 | public function createListRecipient(array $body): Response 210 | { 211 | $response = $this->client->post(Resources::$Listrecipient, ['body' => $body]); 212 | 213 | if (! $response->success()) { 214 | throw new MailjetException(0, 'MailjetService:createListRecipient() failed', $response); 215 | } 216 | 217 | return $response; 218 | } 219 | 220 | /** 221 | * Edit a list recipient. 222 | * 223 | * @param string $id 224 | * @param array $body Information list - the 'ContactID' and 'ListID' parameters are mandatory. 225 | * 226 | * @return Response 227 | * @throws MailjetException 228 | */ 229 | public function editListRecipient(string $id, array $body): Response 230 | { 231 | $response = $this->client->put(Resources::$Listrecipient, ['id' => $id, 'body' => $body]); 232 | 233 | if (! $response->success()) { 234 | throw new MailjetException(0, 'MailjetService:editListrecipient() failed', $response); 235 | } 236 | 237 | return $response; 238 | } 239 | 240 | /** 241 | * Retrieve Mailjet client. 242 | * @return Client 243 | */ 244 | public function getClient(): Client 245 | { 246 | return $this->client; 247 | } 248 | } 249 | -------------------------------------------------------------------------------- /src/Services/TemplateService.php: -------------------------------------------------------------------------------- 1 | mailjet = $mailjet; 28 | } 29 | 30 | /** 31 | * List template resources available for this apikey, use a GET request. 32 | * Alternatively, you may want to add one or more filters. 33 | * @param array|null $filters 34 | * @return array 35 | * @throws MailjetException 36 | */ 37 | public function getAll(array $filters = null): array 38 | { 39 | $response = $this->mailjet->get(Resources::$Template, ['filters' => $filters]); 40 | 41 | if (! $response->success()) { 42 | throw new MailjetException(0, 'TemplateService:getAll() failed', $response); 43 | } 44 | 45 | return $response->getData(); 46 | } 47 | 48 | /** 49 | * Access a given template resource, use a GET request, providing the template's ID value. 50 | * 51 | * @param string $id 52 | * 53 | * @return array 54 | * @throws MailjetException 55 | */ 56 | public function get(string $id): array 57 | { 58 | $response = $this->mailjet->get(Resources::$Template, ['id' => $id]); 59 | 60 | if (! $response->success()) { 61 | throw new MailjetException(0, 'TemplateService:get() failed', $response); 62 | } 63 | 64 | return $response->getData(); 65 | } 66 | 67 | /** 68 | * Add a new template resource with a POST request. 69 | * 70 | * @param Template $template 71 | * 72 | * @return array 73 | * @throws MailjetException 74 | */ 75 | public function create(Template $template): array 76 | { 77 | $response = $this->mailjet->post(Resources::$Template, ['body' => $template->format()]); 78 | 79 | if (! $response->success()) { 80 | throw new MailjetException(0, 'TemplateService:create() failed', $response); 81 | } 82 | 83 | return $response->getData(); 84 | } 85 | 86 | /** 87 | * Update one specific template resource with a PUT request, providing the template's ID value. 88 | * 89 | * @param string $id 90 | * @param Template $template 91 | * 92 | * @return array 93 | * @throws MailjetException 94 | */ 95 | public function update(string $id, Template $template): array 96 | { 97 | $response = $this->mailjet->put(Resources::$Template, ['id' => $id, 'body' => $template->format()]); 98 | 99 | if (! $response->success()) { 100 | throw new MailjetException(0, 'TemplateService:update() failed', $response); 101 | } 102 | 103 | return $response->getData(); 104 | } 105 | 106 | /** 107 | * Delete a given template. 108 | * 109 | * @param string $id 110 | * 111 | * @return array 112 | * @throws MailjetException 113 | */ 114 | public function delete(string $id): array 115 | { 116 | $response = $this->mailjet->delete(Resources::$Template, ['id' => $id]); 117 | 118 | if (! $response->success()) { 119 | throw new MailjetException(0, 'TemplateService:delete() failed', $response); 120 | } 121 | 122 | return $response->getData(); 123 | } 124 | 125 | /** 126 | * Return the text and html contents of the Template. 127 | * 128 | * @param string $id 129 | * 130 | * @return array 131 | * @throws MailjetException 132 | */ 133 | public function getDetailContent(string $id): array 134 | { 135 | $response = $this->mailjet->get(Resources::$TemplateDetailcontent, ['id' => $id]); 136 | 137 | if (! $response->success()) { 138 | throw new MailjetException(0, 'TemplateService:getDetailContent failed', $response); 139 | } 140 | 141 | return $response->getData(); 142 | } 143 | 144 | /** 145 | * Creates the content of a Template. 146 | * 147 | * @param string $id 148 | * @param array $content 149 | * 150 | * @return array 151 | * @throws MailjetException 152 | */ 153 | public function createDetailContent(string $id, array $content): array 154 | { 155 | $response = $this->mailjet->post(Resources::$TemplateDetailcontent, ['id' => $id, 'body' => $content]); 156 | 157 | if (! $response->success()) { 158 | throw new MailjetException(0, 'TemplateService:createDetailContent failed', $response); 159 | } 160 | 161 | return $response->getData(); 162 | } 163 | 164 | /** 165 | * Deletes the content of a Template. 166 | * 167 | * @param string $id 168 | * 169 | * @return array 170 | * @throws MailjetException 171 | */ 172 | public function deleteDetailContent(string $id): array 173 | { 174 | $response = $this->mailjet->post(Resources::$TemplateDetailcontent, ['id' => $id, 'body' => null]); 175 | 176 | if (! $response->success()) { 177 | throw new MailjetException(0, 'TemplateService:createDetailContent failed', $response); 178 | } 179 | 180 | return $response->getData(); 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /tests/Providers/CampaignDraftServiceProviderTest.php: -------------------------------------------------------------------------------- 1 | setUpMocks(); 29 | 30 | $this->serviceProvider = new CampaignDraftServiceProvider($this->application); 31 | 32 | parent::setUp(); 33 | } 34 | 35 | /** 36 | * @test 37 | */ 38 | public function it_can_be_constructed(): void 39 | { 40 | $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); 41 | } 42 | 43 | /** 44 | * @test 45 | */ 46 | public function it_does_provide_method(): void 47 | { 48 | $this->assertContains(CampaignDraftContract::class, $this->serviceProvider->provides()); 49 | } 50 | 51 | /** 52 | * @test 53 | */ 54 | public function it_performs_nothing_in_a_boot_method(): void 55 | { 56 | $this->assertNull($this->serviceProvider->boot()); 57 | } 58 | 59 | protected function setUpMocks(): void 60 | { 61 | $this->application = Mockery::mock(Application::class); 62 | $this->application->shouldReceive('bind'); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/Providers/CampaignServiceProviderTest.php: -------------------------------------------------------------------------------- 1 | setUpMocks(); 29 | 30 | $this->serviceProvider = new CampaignServiceProvider($this->application); 31 | 32 | parent::setUp(); 33 | } 34 | 35 | /** 36 | * @test 37 | */ 38 | public function it_can_be_constructed(): void 39 | { 40 | $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); 41 | } 42 | 43 | /** 44 | * @test 45 | */ 46 | public function it_does_provide_method(): void 47 | { 48 | $this->assertContains(CampaignContract::class, $this->serviceProvider->provides()); 49 | } 50 | 51 | /** 52 | * @test 53 | */ 54 | public function it_performs_nothing_in_a_boot_method(): void 55 | { 56 | $this->assertNull($this->serviceProvider->boot()); 57 | } 58 | 59 | protected function setUpMocks(): void 60 | { 61 | $this->application = Mockery::mock(Application::class); 62 | $this->application->shouldReceive('bind'); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/Providers/ContactMetadataServiceProviderTest.php: -------------------------------------------------------------------------------- 1 | setUpMocks(); 29 | 30 | $this->serviceProvider = new ContactMetadataServiceProvider($this->application); 31 | 32 | parent::setUp(); 33 | } 34 | 35 | /** 36 | * @test 37 | */ 38 | public function it_can_be_constructed(): void 39 | { 40 | $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); 41 | } 42 | 43 | /** 44 | * @test 45 | */ 46 | public function it_does_provide_method(): void 47 | { 48 | $this->assertContains(ContactMetadataContract::class, $this->serviceProvider->provides()); 49 | } 50 | 51 | /** 52 | * @test 53 | */ 54 | public function it_performs_nothing_in_a_boot_method(): void 55 | { 56 | $this->assertNull($this->serviceProvider->boot()); 57 | } 58 | 59 | protected function setUpMocks(): void 60 | { 61 | $this->application = Mockery::mock(Application::class); 62 | $this->application->shouldReceive('bind'); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/Providers/ContactsListServiceProviderTest.php: -------------------------------------------------------------------------------- 1 | setUpMocks(); 29 | 30 | $this->serviceProvider = new ContactsListServiceProvider($this->application); 31 | 32 | parent::setUp(); 33 | } 34 | 35 | /** 36 | * @test 37 | */ 38 | public function it_can_be_constructed(): void 39 | { 40 | $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); 41 | } 42 | 43 | /** 44 | * @test 45 | */ 46 | public function it_does_provide_method(): void 47 | { 48 | $this->assertContains(ContactsListContract::class, $this->serviceProvider->provides()); 49 | } 50 | 51 | /** 52 | * @test 53 | */ 54 | public function it_performs_nothing_in_a_boot_method(): void 55 | { 56 | $this->assertNull($this->serviceProvider->boot()); 57 | } 58 | 59 | protected function setUpMocks(): void 60 | { 61 | $this->application = Mockery::mock(Application::class); 62 | $this->application->shouldReceive('bind'); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/Providers/EventCallbackUrlServiceProviderTest.php: -------------------------------------------------------------------------------- 1 | setUpMocks(); 29 | 30 | $this->serviceProvider = new EventCallbackUrlServiceProvider($this->application); 31 | 32 | parent::setUp(); 33 | } 34 | 35 | /** 36 | * @test 37 | */ 38 | public function it_can_be_constructed(): void 39 | { 40 | $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); 41 | } 42 | 43 | /** 44 | * @test 45 | */ 46 | public function it_does_provide_method(): void 47 | { 48 | $this->assertContains(EventCallbackUrlContract::class, $this->serviceProvider->provides()); 49 | } 50 | 51 | /** 52 | * @test 53 | */ 54 | public function it_performs_nothing_in_a_boot_method(): void 55 | { 56 | $this->assertNull($this->serviceProvider->boot()); 57 | } 58 | 59 | protected function setUpMocks(): void 60 | { 61 | $this->application = Mockery::mock(Application::class); 62 | $this->application->shouldReceive('bind'); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/Providers/MailjetClientServiceProviderTest.php: -------------------------------------------------------------------------------- 1 | setUpMocks(); 29 | 30 | $this->serviceProvider = new MailjetClientServiceProvider($this->application); 31 | 32 | parent::setUp(); 33 | } 34 | 35 | /** 36 | * @test 37 | */ 38 | public function it_can_be_constructed(): void 39 | { 40 | $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); 41 | } 42 | 43 | /** 44 | * @test 45 | */ 46 | public function it_does_provide_method(): void 47 | { 48 | $this->assertContains(MailjetServiceContract::class, $this->serviceProvider->provides()); 49 | } 50 | 51 | /** 52 | * @test 53 | */ 54 | public function it_performs_nothing_in_a_boot_method(): void 55 | { 56 | $this->assertNull($this->serviceProvider->boot()); 57 | } 58 | 59 | protected function setUpMocks(): void 60 | { 61 | $this->application = Mockery::mock(Application::class); 62 | $this->application->shouldReceive('bind'); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/Providers/TemplateServiceProviderTest.php: -------------------------------------------------------------------------------- 1 | setUpMocks(); 29 | 30 | $this->serviceProvider = new TemplateServiceProvider($this->application); 31 | 32 | parent::setUp(); 33 | } 34 | 35 | /** 36 | * @test 37 | */ 38 | public function it_can_be_constructed(): void 39 | { 40 | $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); 41 | } 42 | 43 | /** 44 | * @test 45 | */ 46 | public function it_does_provide_method(): void 47 | { 48 | $this->assertContains(TemplateServiceContract::class, $this->serviceProvider->provides()); 49 | } 50 | 51 | /** 52 | * @test 53 | */ 54 | public function it_performs_nothing_in_a_boot_method(): void 55 | { 56 | $this->assertNull($this->serviceProvider->boot()); 57 | } 58 | 59 | protected function setUpMocks(): void 60 | { 61 | $this->application = Mockery::mock(Application::class); 62 | $this->application->shouldReceive('bind'); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/Services/MailjetServiceTest.php: -------------------------------------------------------------------------------- 1 | createMock(Response::class); 32 | $responseMock->method('success')->willReturn(true); 33 | 34 | $this->mailjetClientMock = Mockery::mock('overload:' . Client::class); 35 | 36 | $this->mailjetClientMock 37 | ->shouldReceive('put') 38 | ->andReturnUsing(function($resourceArg, $params) use ($responseMock) { 39 | $data = []; 40 | 41 | switch ($resourceArg[0]) { 42 | case 'resource-test-put': 43 | $data = array_merge($params, [ 44 | 'status' => '0003', 45 | ]); 46 | break; 47 | 48 | case 'listrecipient': 49 | $data = array_merge($params, [ 50 | 'status' => '0011', 51 | ]); 52 | break; 53 | } 54 | 55 | $responseMock->method('getData')->willReturn($data); 56 | 57 | return $responseMock; 58 | }); 59 | 60 | $this->mailjetClientMock 61 | ->shouldReceive('post') 62 | ->andReturnUsing(function($resourceArg, $params) use ($responseMock) { 63 | $data = []; 64 | 65 | switch ($resourceArg[0]) { 66 | case 'resource-test-post': 67 | $data = array_merge($params, [ 68 | 'status' => '0001', 69 | ]); 70 | break; 71 | 72 | case 'contactslist': 73 | $data = array_merge($params, [ 74 | 'status' => '0006', 75 | ]); 76 | break; 77 | 78 | case 'contact': 79 | $data = array_merge($params, [ 80 | 'status' => '0009', 81 | ]); 82 | break; 83 | 84 | case 'listrecipient': 85 | $data = array_merge($params, [ 86 | 'status' => '0010', 87 | ]); 88 | break; 89 | 90 | } 91 | 92 | $responseMock->method('getData')->willReturn($data); 93 | 94 | return $responseMock; 95 | }); 96 | 97 | $this->mailjetClientMock 98 | ->shouldReceive('delete') 99 | ->andReturnUsing(function($resourceArg, $params) use ($responseMock) { 100 | $data = []; 101 | 102 | switch ($resourceArg[0]) { 103 | case 'resource-test-delete': 104 | $data = array_merge($params, [ 105 | 'status' => '0004', 106 | ]); 107 | break; 108 | } 109 | 110 | $responseMock->method('getData')->willReturn($data); 111 | 112 | return $responseMock; 113 | }); 114 | 115 | $this->mailjetClientMock 116 | ->shouldReceive('get') 117 | ->andReturnUsing(function($resourceArg, $params) use ($responseMock) { 118 | $data = []; 119 | 120 | switch ($resourceArg[0]) { 121 | case 'resource-test-get': 122 | $data = array_merge($params, [ 123 | 'status' => '0002', 124 | ]); 125 | break; 126 | 127 | case 'contactslist': 128 | $data = array_merge($params, [ 129 | 'status' => '0005', 130 | ]); 131 | break; 132 | 133 | case 'listrecipient': 134 | $data = array_merge($params, [ 135 | 'status' => '0007', 136 | ]); 137 | break; 138 | 139 | case 'contact': 140 | $data = array_merge($params, [ 141 | 'status' => '0008', 142 | ]); 143 | break; 144 | } 145 | 146 | $responseMock->method('getData')->willReturn($data); 147 | 148 | return $responseMock; 149 | }); 150 | 151 | $this->mailjetService = $this->app['Mailjet']; 152 | } 153 | 154 | public function testFacade(): void 155 | { 156 | $this->assertTrue(method_exists($this->mailjetService, 'get')); 157 | $this->assertTrue(method_exists($this->mailjetService, 'post')); 158 | $this->assertTrue(method_exists($this->mailjetService, 'put')); 159 | $this->assertTrue(method_exists($this->mailjetService, 'delete')); 160 | $this->assertTrue(method_exists($this->mailjetService, 'getAllLists')); 161 | $this->assertTrue(method_exists($this->mailjetService, 'createList')); 162 | $this->assertTrue(method_exists($this->mailjetService, 'getListRecipients')); 163 | $this->assertTrue(method_exists($this->mailjetService, 'getSingleContact')); 164 | $this->assertTrue(method_exists($this->mailjetService, 'createContact')); 165 | $this->assertTrue(method_exists($this->mailjetService, 'createListRecipient')); 166 | $this->assertTrue(method_exists($this->mailjetService, 'editListrecipient')); 167 | $this->assertTrue(method_exists($this->mailjetService, 'getClient')); 168 | } 169 | 170 | public function testCanUseClient(): void 171 | { 172 | $client = Mailjet::getClient(); 173 | $this->assertInstanceOf(Client::class, $client); 174 | } 175 | 176 | public function testPost() 177 | { 178 | $response = $this->mailjetService->post(['resource-test-post'], [ 179 | 'data' => 'test0001', 180 | ]); 181 | 182 | $this->assertSame([ 183 | 'data' => 'test0001', 184 | 'status' => '0001', 185 | ], $response->getData()); 186 | } 187 | 188 | public function testGet() 189 | { 190 | $response = $this->mailjetService->get(['resource-test-get'], [ 191 | 'data' => 'test0002', 192 | ], []); 193 | 194 | $this->assertSame([ 195 | 'data' => 'test0002', 196 | 'status' => '0002', 197 | ], $response->getData()); 198 | } 199 | 200 | public function testPut() 201 | { 202 | $response = $this->mailjetService->put(['resource-test-put'], [ 203 | 'data' => 'test0003', 204 | ], []); 205 | 206 | $this->assertSame([ 207 | 'data' => 'test0003', 208 | 'status' => '0003', 209 | ], $response->getData()); 210 | } 211 | 212 | public function testDelete() 213 | { 214 | $response = $this->mailjetService->delete(['resource-test-delete'], [ 215 | 'data' => 'test0004', 216 | ], []); 217 | 218 | $this->assertSame([ 219 | 'data' => 'test0004', 220 | 'status' => '0004', 221 | ], $response->getData()); 222 | } 223 | 224 | public function testGetAllLists() 225 | { 226 | $response = $this->mailjetService->getAllLists([ 227 | 'data' => 'test0005', 228 | ]); 229 | 230 | $this->assertSame([ 231 | 'filters' => [ 232 | 'data' => 'test0005', 233 | ], 234 | 'status' => '0005', 235 | ], $response->getData()); 236 | } 237 | 238 | public function testCreateList() 239 | { 240 | $response = $this->mailjetService->createList([ 241 | 'data' => 'test0006' 242 | ]); 243 | 244 | $this->assertSame([ 245 | 'body' => [ 246 | 'data' => 'test0006', 247 | ], 248 | 'status' => '0006', 249 | ], $response->getData()); 250 | } 251 | 252 | public function testGetListRecipients() 253 | { 254 | $response = $this->mailjetService->getListRecipients([ 255 | 'data' => 'test0007' 256 | ]); 257 | 258 | $this->assertSame([ 259 | 'filters' => [ 260 | 'data' => 'test0007', 261 | ], 262 | 'status' => '0007', 263 | ], $response->getData()); 264 | } 265 | 266 | public function testGetSingleContact() 267 | { 268 | $response = $this->mailjetService->getSingleContact('123'); 269 | 270 | $this->assertSame([ 271 | 'id' => '123', 272 | 'status' => '0008', 273 | ], $response->getData()); 274 | } 275 | 276 | public function testCreateContact() 277 | { 278 | $response = $this->mailjetService->createContact([ 279 | 'data' => 'test0009' 280 | ]); 281 | 282 | $this->assertSame([ 283 | 'body' => [ 284 | 'data' => 'test0009', 285 | ], 286 | 'status' => '0009', 287 | ], $response->getData()); 288 | } 289 | 290 | public function testCreateListRecipient() 291 | { 292 | $response = $this->mailjetService->createListRecipient([ 293 | 'data' => 'test0010' 294 | ]); 295 | 296 | $this->assertSame([ 297 | 'body' => [ 298 | 'data' => 'test0010', 299 | ], 300 | 'status' => '0010', 301 | ], $response->getData()); 302 | } 303 | 304 | public function testEditListrecipient() 305 | { 306 | $response = $this->mailjetService->editListrecipient('1233', [ 307 | 'data' => 'test0011' 308 | ]); 309 | 310 | $this->assertSame([ 311 | 'id' => '1233', 312 | 'body' => [ 313 | 'data' => 'test0011', 314 | ], 315 | 'status' => '0011', 316 | ], $response->getData()); 317 | } 318 | 319 | protected function getPackageAliases($app): array 320 | { 321 | return [ 322 | 'Mailjet' => Mailjet::class 323 | ]; 324 | } 325 | 326 | /** 327 | * Define environment setup. 328 | * 329 | * @param \Illuminate\Foundation\Application $app 330 | * @return void 331 | */ 332 | protected function getEnvironmentSetUp($app): void 333 | { 334 | // Setup default database to use sqlite :memory: 335 | $app['config']->set('services.mailjet.key', 'ABC123456'); 336 | $app['config']->set('services.mailjet.secret', 'ABC123456'); 337 | } 338 | 339 | protected function getPackageProviders($app): array 340 | { 341 | return [MailjetServiceProvider::class]; 342 | } 343 | } 344 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 |