{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-docs/email-api/sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":[]},"type":"markdown"},"seo":{"title":"Mailjet PHP SDK","siteUrl":"https://dev.mailjet.com/","projectTitle":"Mailjet Developer Documentation","description":"Install and use the official Mailjet PHP wrapper: PSR-18 clients, authentication, sending with Send API v3.1, REST requests, templates and SMS.","image":"https://dev.mailjet.com/og-image.png","lang":"en-US","keywords":["Mailjet API","Email API","Content API","SMTP Relay","transactional email","email delivery","Send API","email templates","developer documentation"],"meta":[{"name":"og:type","content":"website"},{"name":"og:site_name","content":"Mailjet Developer Documentation"},{"name":"twitter:card","content":"summary_large_image"},{"name":"robots","content":"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1"},{"name":"googlebot","content":"index, follow, max-snippet:-1, max-image-preview:large"}],"jsonLd":{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://dev.mailjet.com/#organization","name":"Mailjet","alternateName":"Sinch Mailjet","url":"https://www.mailjet.com","logo":"https://assets.mailjet.com/lib/images/mailjetLogo/mj_logo_with_text_color.png","parentOrganization":{"@type":"Organization","name":"Sinch","url":"https://www.sinch.com"}},{"@type":"WebSite","@id":"https://dev.mailjet.com/#website","name":"Mailjet Developer Documentation","description":"Official developer documentation for the Mailjet Email API, Content API and SMTP Relay.","url":"https://dev.mailjet.com/","inLanguage":"en-US","publisher":{"@id":"https://dev.mailjet.com/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://dev.mailjet.com/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]},"llmstxt":{"hide":false,"title":"Mailjet Developer Documentation","description":"Developer documentation for the Mailjet Email API, Content API and SMTP Relay - guides, API references and code samples for sending, templating and tracking email at scale.","details":{"content":"Mailjet (part of Sinch) provides APIs for sending and managing email.\n\nThis documentation covers four main areas:\n\n- **Email API** - the Send API (v3 and v3.1) for transactional and marketing email,\n  plus contact management, segmentation, campaigns, statistics, webhooks, the Parse API\n  and the Mailjet Templating Language.\n- **Content API** - programmatic management of email templates, template contents,\n  images, labels and authentication tokens (v1).\n- **SMTP Relay** - sending through Mailjet's SMTP servers, including custom headers.\n- **API Reference** - the complete, generated reference for every endpoint, built directly\n  from the OpenAPI specifications.\n\nBase URL for all REST APIs: `https://api.mailjet.com`.\nAuthentication uses HTTP Basic auth with an API key and secret key.\n\n## Machine-readable entry points\n\nPrefer these when you need the API surface in as few requests as possible:\n\n- `/openapi/openapi-mailjet.md` - the **complete** Email API reference (every endpoint,\n  grouped by resource) as a single Markdown document.\n- `/openapi/openapi-passport.md` - the **complete** Content API reference as a single\n  Markdown document.\n- `/_bundle/openapi/openapi-mailjet.yaml` - the raw, fully bundled OpenAPI 3 description\n  of the Email API.\n- `/_bundle/openapi/openapi-passport.yaml` - the raw, fully bundled OpenAPI 3 description\n  of the Content API.\n- `/sitemap.xml` - every indexable URL on this site.\n\nEvery page on this site is also available as clean Markdown by appending `.md` to its URL."},"sections":[{"title":"Email API","description":"Sending, contacts, campaigns, statistics, webhooks and templating.","includeFiles":["docs/email-api/**/*.md"],"excludeFiles":[]},{"title":"Content API","description":"Manage templates, template contents, images, labels and tokens.","includeFiles":["docs/content-api/**/*.md","docs/content-api.md"],"excludeFiles":[]},{"title":"SMTP Relay","description":"Send email through Mailjet's SMTP servers.","includeFiles":["docs/smtp-relay/**/*.md"],"excludeFiles":[]},{"title":"API Reference","description":"Complete generated reference for every Mailjet API endpoint.","includeFiles":["openapi/openapi-mailjet.yaml","openapi/openapi-passport.yaml","docs/api-reference/**/*.md"],"excludeFiles":[]},{"title":"Migrating from Mandrill","description":"Guides for switching from Mandrill to Mailjet.","includeFiles":["docs/mandrill/**/*.md"],"excludeFiles":[]}],"excludeFiles":[]}},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"php-sdk","__idx":0},"children":["PHP SDK"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The official PHP wrapper for the Mailjet API is installed with Composer as ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["mailjet/mailjet-apiv3-php"]}]}," and developed at ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://github.com/mailjet/mailjet-apiv3-php"},"children":["mailjet/mailjet-apiv3-php"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Current release: ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["2.0.0"]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"compatibility","__idx":1},"children":["Compatibility"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The library requires ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["PHP 8.1 or higher"]},"."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"installation","__idx":2},"children":["Installation"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"bash","header":{"controls":{"copy":{}}},"source":"composer require mailjet/mailjet-apiv3-php\n","lang":"bash"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The wrapper needs a ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://www.php-fig.org/psr/psr-18/"},"children":["PSR-18"]}," HTTP client and ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://www.php-fig.org/psr/psr-17/"},"children":["PSR-17"]}," factories. If your project does not already have one, Composer installs a compatible default (such as Guzzle) via ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://github.com/php-http/discovery"},"children":["php-http/discovery"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To pin a specific client, require it explicitly:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"bash","header":{"controls":{"copy":{}}},"source":"# Guzzle\ncomposer require guzzlehttp/guzzle\n\n# Symfony HttpClient\ncomposer require symfony/http-client nyholm/psr7\n","lang":"bash"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"bring-your-own-http-client","__idx":3},"children":["Bring your own HTTP client"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Since 2.0 the transport layer is fully delegated to the injected PSR-18 client, which is what makes timeouts, proxies and retries your own to configure:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"use \\Mailjet\\Client;\n\n// auto-discovery (default) — uses whatever PSR-18 client is installed\n$mj = new Client($apikey, $apisecret);\n\n// inject a specific HTTP client\n$httpClient = new \\GuzzleHttp\\Client(['timeout' => 30, 'proxy' => 'tcp://localhost:8080']);\n$mj = new Client($apikey, $apisecret, true, [], $httpClient);\n\n// inject a Symfony HttpClient\n$httpClient = new \\Symfony\\Component\\HttpClient\\Psr18Client();\n$mj = new Client($apikey, $apisecret, true, [], $httpClient);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["PSR-17 request and stream factories can be injected as the sixth and seventh constructor arguments."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"authentication","__idx":4},"children":["Authentication"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The Email API authenticates with your API key and secret:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"bash","header":{"controls":{"copy":{}}},"source":"export MJ_APIKEY_PUBLIC='your API key'\nexport MJ_APIKEY_PRIVATE='your API secret'\n","lang":"bash"},"children":[]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"use \\Mailjet\\Resources;\n\n$apikey = getenv('MJ_APIKEY_PUBLIC');\n$apisecret = getenv('MJ_APIKEY_PRIVATE');\n\n$mj = new \\Mailjet\\Client($apikey, $apisecret);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The SMS API authenticates with a ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["bearer token"]}," instead — see ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"#sms-api"},"children":["SMS API"]},"."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"send-your-first-email","__idx":5},"children":["Send your first email"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"<?php\nrequire 'vendor/autoload.php';\n\nuse \\Mailjet\\Resources;\n\n$mj = new \\Mailjet\\Client(\n    getenv('MJ_APIKEY_PUBLIC'),\n    getenv('MJ_APIKEY_PRIVATE'),\n    true,\n    ['version' => 'v3.1']\n);\n\n$body = [\n    'Messages' => [\n        [\n            'From' => [\n                'Email' => 'pilot@example.com',\n                'Name' => 'Mailjet Pilot'\n            ],\n            'To' => [\n                [\n                    'Email' => 'passenger@example.com',\n                    'Name' => 'Passenger 1'\n                ]\n            ],\n            'Subject' => 'My first Mailjet Email!',\n            'TextPart' => 'Greetings from Mailjet!',\n            'HTMLPart' => '<h3>Dear passenger 1, welcome to Mailjet!</h3>'\n        ]\n    ]\n];\n\n$response = $mj->post(Resources::$Email, ['body' => $body]);\n\n$response->success() && var_dump($response->getData());\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"client-and-call-configuration","__idx":6},"children":["Client and call configuration"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The constructor signature is:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"new \\Mailjet\\Client(\n    $MJ_APIKEY_PUBLIC,\n    $MJ_APIKEY_PRIVATE,\n    $CALL,\n    $OPTIONS,\n    $HTTP_CLIENT,\n    $REQUEST_FACTORY,\n    $STREAM_FACTORY\n);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Argument"},"children":["Argument"]},{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Meaning"},"children":["Meaning"]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["$MJ_APIKEY_PUBLIC"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["public API key"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["$MJ_APIKEY_PRIVATE"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["private API key"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["$CALL"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["boolean; ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["true"]}," performs the actual API call"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["$OPTIONS"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["associative array of connection options"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["$HTTP_CLIENT"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["optional PSR-18 ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["ClientInterface"]}," — auto-discovered if ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["null"]}]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["$REQUEST_FACTORY"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["optional PSR-17 ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["RequestFactoryInterface"]}]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["$STREAM_FACTORY"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["optional PSR-17 ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["StreamFactoryInterface"]}]}]}]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"api-versioning","__idx":7},"children":["API versioning"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Version"},"children":["Version"]},{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Scope"},"children":["Scope"]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["v3"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Email API"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["v3.1"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Send API v3.1 (latest send version)"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["v4"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["SMS API, contact delete API"]}]}]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Most Email API endpoints sit under ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["v3"]},", which is the default. For anything else, set ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["version"]},":"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$mj = new \\Mailjet\\Client(\n    getenv('MJ_APIKEY_PUBLIC'),\n    getenv('MJ_APIKEY_PRIVATE'),\n    true,\n    ['version' => 'v3.1']\n);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"base-url","__idx":8},"children":["Base URL"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The default base domain is ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["api.mailjet.com"]},". Override it with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["url"]},":"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$mj = new \\Mailjet\\Client(\n    getenv('MJ_APIKEY_PUBLIC'),\n    getenv('MJ_APIKEY_PRIVATE'),\n    true,\n    ['url' => 'api.us.mailjet.com']\n);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Accounts on Mailjet's ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["US architecture"]}," must set ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["api.us.mailjet.com"]},"."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"network-connectivity","__idx":9},"children":["Network connectivity"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["If you hit intermittent ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["SSL_ERROR_SYSCALL"]}," errors, force IPv4 on your HTTP client:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$httpClient = new \\GuzzleHttp\\Client(['force_ip_resolve' => 'v4']);\n$mj = new \\Mailjet\\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'), true, [], $httpClient);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"disable-the-api-call","__idx":10},"children":["Disable the API call"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Useful in tests, to build a request without reaching Mailjet — set the third argument to ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["false"]},":"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$mj = new \\Mailjet\\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'), false);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"request-examples","__idx":11},"children":["Request examples"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["All resources are constants on the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Resources"]}," class, which maps PHP resource names to their API reference names. The full list is in ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://github.com/mailjet/mailjet-apiv3-php/blob/master/src/Mailjet/Resources.php"},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["/src/Mailjet/Resources.php"]}]},"."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$response = $mj->delete(Resources::$Template, ['id' => $id]);\n$response = $mj->put(Resources::$ContactData, ['id' => $id, 'body' => $body]);\n$response = $mj->post(Resources::$ContactManagecontactslists, ['id' => $id, 'body' => $body]);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"post--create-an-object","__idx":12},"children":["POST — create an object"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["$params"]}," accepts ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["body"]}," (the object to create) and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["id"]}," (when acting on an existing resource)."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$body = [\n    'Email' => 'passenger@example.com'\n];\n\n$response = $mj->post(Resources::$Contact, ['body' => $body]);\n$response->success() && var_dump($response->getData());\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"post--endpoints-with-an-action","__idx":13},"children":["POST — endpoints with an action"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$body = [\n    'ContactsLists' => [\n        [\n            'ListID' => $listID1,\n            'Action' => 'addnoforce'\n        ],\n        [\n            'ListID' => $listID2,\n            'Action' => 'addforce'\n        ]\n    ]\n];\n\n$response = $mj->post(Resources::$ContactManagecontactslists, ['id' => $id, 'body' => $body]);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"get--all-objects","__idx":14},"children":["GET — all objects"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["$params"]}," accepts ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["id"]}," (a single object) and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["filters"]}," (query parameters)."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$response = $mj->get(Resources::$Contact);\n$response->success() && var_dump($response->getData());\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"get--with-filters","__idx":15},"children":["GET — with filters"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$filters = [\n    'IsExcludedFromCampaigns' => 'false'\n];\n\n$response = $mj->get(Resources::$Contact, ['filters' => $filters]);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"get--with-paging-and-sorting","__idx":16},"children":["GET — with paging and sorting"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$filters = [\n    'Limit' => 40,   // default 10, maximum 1000\n    'Offset' => 20,\n    'Sort' => 'ArrivedAt DESC',\n    'Contact' => $contact->ID,\n    'showSubject' => true\n];\n\n$response = $mj->get(Resources::$Message, ['filters' => $filters]);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"get--a-single-object","__idx":17},"children":["GET — a single object"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$response = $mj->get(Resources::$Contact, ['id' => $id]);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"put--update-an-object","__idx":18},"children":["PUT — update an object"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["A ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["PUT"]}," in the Mailjet API behaves like a ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["PATCH"]},": only the properties you send are updated, everything else is left untouched, and non-mandatory properties can be omitted."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$body = [\n    'first_name' => 'John',\n    'last_name' => 'Smith'\n];\n\n$response = $mj->put(Resources::$ContactData, ['id' => $id, 'body' => $body]);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"delete--remove-an-object","__idx":19},"children":["DELETE — remove an object"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["A successful ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["DELETE"]}," returns ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["204 No Content"]}," with no response body."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$response = $mj->delete(Resources::$Template, ['id' => $id]);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Contact deletion (GDPR) uses the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["v4"]}," endpoint:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$mj = new \\Mailjet\\Client(\n    getenv('MJ_APIKEY_PUBLIC'),\n    getenv('MJ_APIKEY_PRIVATE'),\n    true,\n    ['version' => 'v4']\n);\n\n$response = $mj->delete(Resources::$Contacts, ['ID' => $contactID]);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"response-object","__idx":20},"children":["Response object"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["get"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["post"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["put"]}," and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["delete"]}," return a ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Response"]}," object:"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Method"},"children":["Method"]},{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Returns"},"children":["Returns"]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["success()"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["whether the call succeeded"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["getStatus()"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["HTTP status code (200, 400, …)"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["getData()"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Data"]}," property of the JSON payload as a PHP associative array, or the full payload if there is none"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["getCount()"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["number of objects returned"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["getReasonPhrase()"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["HTTP reason phrase (\"OK\", \"Bad Request\", …)"]}]}]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"sms-api","__idx":21},"children":["SMS API"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["SMS endpoints authenticate with a bearer token generated in the ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://app.mailjet.com/sms"},"children":["SMS section"]}," of your account. Pass the token as the first argument and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["NULL"]}," as the second:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$mj = new \\Mailjet\\Client(\n    getenv('MJ_APITOKEN'),\n    NULL,\n    true,\n    ['url' => 'api.mailjet.com', 'version' => 'v4', 'call' => false]\n);\n\n$body = [\n    'Text' => 'Have a nice SMS flight with Mailjet!',\n    'To' => '+33600000000',\n    'From' => 'MJPilot',\n];\n\n$response = $mj->post(Resources::$SmsSend, ['body' => $body]);\n$response->success() && var_dump($response->getData());\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"further-examples","__idx":22},"children":["Further examples"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"emoji-in-a-subject-line","__idx":23},"children":["Emoji in a subject line"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Use the string utility to get the UTF notation the API expects:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"use \\Mailjet\\Resources;\n\n$subject = \\Mailjet\\Utility\\StringUtility::utfStringNotation('This is a subject with emoji 🤑');\n\n$body = [\n    'Locale' => 'en_US',\n    'Sender' => 'sender@example.com',\n    'SenderEmail' => 'sender@example.com',\n    'Subject' => $subject,\n    'TemplateID' => 12345,\n    'ContactsListID' => 12345,\n    'Title' => 'Emoji Test ' . time(),\n];\n\n$response = $mj->post(Resources::$Campaigndraft, ['body' => $body]);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"retrieve-campaigns-with-filters","__idx":24},"children":["Retrieve campaigns with filters"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Without query parameters, only campaigns sent since 00:00 UTC today are returned — set ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["period"]}," to widen the window:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$filters = [\n    'period' => 'Year',\n];\n\n$response = $mj->get(Resources::$Campaign, ['filters' => $filters]);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"retrieve-esp-statistics","__idx":25},"children":["Retrieve ESP statistics"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$filters = [\n    'CampaignID' => 123456,\n];\n\n$response = $mj->get(Resources::$Useragentstatistics, ['filters' => $filters]);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"campaign-overview","__idx":26},"children":["Campaign overview"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$response = $mj->get(Resources::$Campaignoverview);\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"data-transfer-objects","__idx":27},"children":["Data transfer objects"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["DTOs give you typed objects instead of raw arrays:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"use Mailjet\\Model\\MailjetCampaignDataDTO;\nuse \\Mailjet\\Resources;\n\n$filters = [\n    'period' => 'Month',\n    'Limit' => 10,\n];\n\n$response = $mj->get(\n    Resources::$Campaigndraft + ['model' => MailjetCampaignDataDTO::class],\n    ['filters' => $filters]\n);\n\nforeach ($response->getData() as $campaign) {\n    /** @var MailjetCampaignDataDTO $campaign */\n    print_r($campaign);\n}\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"create-a-template-via-the-api","__idx":28},"children":["Create a template via the API"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Creating a template takes two calls: one for the metadata, one for the content. The content endpoint does ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["not"]}," accept MJML — send HTML and text parts."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"php","header":{"controls":{"copy":{}}},"source":"$body = [\n    'Author' => 'John Doe',\n    'Copyright' => 'Mailjet',\n    'Description' => 'Used to send out promo codes.',\n    'EditMode' => 2,\n    'IsStarred' => false,\n    'IsTextPartGenerationEnabled' => true,\n    'Locale' => 'en_US',\n    'Name' => 'promo-codes',\n    'OwnerType' => 'user',\n    'Purposes' => ['marketing'],\n];\n\n$response = $mj->post(Resources::$Template, ['body' => $body]);\n$id = $response->getData()[0]['ID'];\n\nif ($id) {\n    $content = [\n        'Headers' => '',\n        'Html-part' => '<h3>Dear passenger, welcome to Mailjet!</h3>',\n        'MJMLContent' => '',\n        'Text-part' => 'Dear passenger, welcome to Mailjet!'\n    ];\n\n    $mj->post(Resources::$TemplateDetailcontent, ['id' => $id, 'body' => $content]);\n}\n","lang":"php"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"contribute","__idx":29},"children":["Contribute"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The wrapper is open source. Fork the repository, branch, implement your fix or feature, document it, and open a pull request at ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://github.com/mailjet/mailjet-apiv3-php"},"children":["mailjet/mailjet-apiv3-php"]},". Documentation improvements belong in the ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://github.com/mailjet/api-documentation"},"children":["API documentation repo"]},"."]}]},"headings":[{"value":"PHP SDK","id":"php-sdk","depth":1},{"value":"Compatibility","id":"compatibility","depth":2},{"value":"Installation","id":"installation","depth":2},{"value":"Bring your own HTTP client","id":"bring-your-own-http-client","depth":3},{"value":"Authentication","id":"authentication","depth":2},{"value":"Send your first email","id":"send-your-first-email","depth":2},{"value":"Client and call configuration","id":"client-and-call-configuration","depth":2},{"value":"API versioning","id":"api-versioning","depth":3},{"value":"Base URL","id":"base-url","depth":3},{"value":"Network connectivity","id":"network-connectivity","depth":3},{"value":"Disable the API call","id":"disable-the-api-call","depth":3},{"value":"Request examples","id":"request-examples","depth":2},{"value":"POST — create an object","id":"post--create-an-object","depth":3},{"value":"POST — endpoints with an action","id":"post--endpoints-with-an-action","depth":3},{"value":"GET — all objects","id":"get--all-objects","depth":3},{"value":"GET — with filters","id":"get--with-filters","depth":3},{"value":"GET — with paging and sorting","id":"get--with-paging-and-sorting","depth":3},{"value":"GET — a single object","id":"get--a-single-object","depth":3},{"value":"PUT — update an object","id":"put--update-an-object","depth":3},{"value":"DELETE — remove an object","id":"delete--remove-an-object","depth":3},{"value":"Response object","id":"response-object","depth":2},{"value":"SMS API","id":"sms-api","depth":2},{"value":"Further examples","id":"further-examples","depth":2},{"value":"Emoji in a subject line","id":"emoji-in-a-subject-line","depth":3},{"value":"Retrieve campaigns with filters","id":"retrieve-campaigns-with-filters","depth":3},{"value":"Retrieve ESP statistics","id":"retrieve-esp-statistics","depth":3},{"value":"Campaign overview","id":"campaign-overview","depth":3},{"value":"Data transfer objects","id":"data-transfer-objects","depth":3},{"value":"Create a template via the API","id":"create-a-template-via-the-api","depth":3},{"value":"Contribute","id":"contribute","depth":2}],"frontmatter":{"title":"Mailjet PHP SDK","seo":{"title":"Mailjet PHP SDK","description":"Install and use the official Mailjet PHP wrapper: PSR-18 clients, authentication, sending with Send API v3.1, REST requests, templates and SMS."}},"lastModified":"2026-08-26T16:26:02.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/docs/email-api/developer-tools/php","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}