{"id":13624171,"url":"https://github.com/mailgun/mailgun-php","last_synced_at":"2026-02-23T00:50:12.041Z","repository":{"id":441864,"uuid":"11654443","full_name":"mailgun/mailgun-php","owner":"mailgun","description":"Mailgun's Official SDK for PHP","archived":false,"fork":false,"pushed_at":"2024-09-26T13:35:06.000Z","size":3267,"stargazers_count":1098,"open_issues_count":6,"forks_count":314,"subscribers_count":87,"default_branch":"master","last_synced_at":"2024-10-29T11:26:59.168Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.mailgun.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mailgun.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-07-25T07:20:47.000Z","updated_at":"2024-10-23T00:55:34.000Z","dependencies_parsed_at":"2023-07-05T14:56:01.535Z","dependency_job_id":"c0d1403a-be21-49ee-a183-f4fe5239d083","html_url":"https://github.com/mailgun/mailgun-php","commit_stats":{"total_commits":636,"total_committers":98,"mean_commits":6.489795918367347,"dds":0.690251572327044,"last_synced_commit":"b268fd0ace869c76ca369b2719f2740d02564b5f"},"previous_names":[],"tags_count":59,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mailgun%2Fmailgun-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mailgun%2Fmailgun-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mailgun%2Fmailgun-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mailgun%2Fmailgun-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mailgun","download_url":"https://codeload.github.com/mailgun/mailgun-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247934841,"owners_count":21020729,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T21:01:39.724Z","updated_at":"2026-02-23T00:50:12.035Z","avatar_url":"https://github.com/mailgun.png","language":"PHP","readme":"# Mailgun PHP client\n\nThis is the Mailgun PHP SDK. This SDK contains methods for easily interacting\nwith the Mailgun API. Below are examples to get you started. For additional\nexamples, please see our official documentation at http://documentation.mailgun.com\n\n[![Latest Version](https://img.shields.io/github/release/mailgun/mailgun-php.svg?style=flat-square)](https://github.com/mailgun/mailgun-php/releases)\n[![Total Downloads](https://img.shields.io/packagist/dt/mailgun/mailgun-php.svg?style=flat-square)](https://packagist.org/packages/mailgun/mailgun-php)\n[![Join the chat at https://gitter.im/mailgun/mailgun-php](https://badges.gitter.im/mailgun/mailgun-php.svg)](https://gitter.im/mailgun/mailgun-php?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\n## Installation\n\nTo install the SDK, you will need to be using [Composer](http://getcomposer.org/)\nin your project.\nIf you aren't using Composer yet, it's really simple! Here's how to install\ncomposer:\n\n```bash\ncurl -sS https://getcomposer.org/installer | php\n```\n\n## Required minimum php version\n - minimum php version 7.4\n\nThe Mailgun API Client is not hard coupled to Guzzle, Buzz or any other library that sends\nHTTP messages. Instead, it uses the [PSR-18](https://www.php-fig.org/psr/psr-18/) client abstraction.\nThis will give you the flexibility to choose what\n[PSR-7 implementation](https://packagist.org/providers/psr/http-factory-implementation)\nand [HTTP client](https://packagist.org/providers/psr/http-client-implementation)\nyou want to use.\n\nIf you just want to get started quickly you should run the following command:\n\n```bash\ncomposer require mailgun/mailgun-php symfony/http-client nyholm/psr7\n```\n\n## Usage\n\nYou should always use Composer autoloader in your application to automatically load\nyour dependencies. All the examples below assume you've already included this in your\nfile:\n\n```php\nrequire 'vendor/autoload.php';\nuse Mailgun\\Mailgun;\n```\n\nHere's how to send a message using the SDK:\n\n```php\n// First, instantiate the SDK with your API credentials\n$mg = Mailgun::create('your_api_key_here'); // For US servers\n$mg = Mailgun::create('your_api_key_here', 'https://api.eu.mailgun.net'); // For EU servers\n\n// Now, compose and send your message.\n// $mg-\u003emessages()-\u003esend($domain, $params);\n$mg-\u003emessages()-\u003esend('example.com', [\n  'from'    =\u003e 'bob@example.com',\n  'to'      =\u003e 'sally@example.com',\n  'subject' =\u003e 'The PHP SDK is awesome!',\n  'text'    =\u003e 'It is so simple to send a message.'\n]);\n```\n\nAttention: `$domain` must match to the domain you have configured on [app.mailgun.com](https://app.mailgun.com/app/domains).\n\n### Usage of new method for updating web scheme\n\n```php\n# Include the Autoloader (see \"Libraries\" for install instructions)\nrequire 'vendor/autoload.php';\nuse Mailgun\\Mailgun;\n\n# Instantiate the client.\n$mgClient = Mailgun::create('KEY', 'FULL_DOMAIN_URL');\n$domain = \"DOMAIN\";\n\n# Issue the call to the client.\n$result = $mgClient-\u003edomains()-\u003eupdateWebScheme($domain, 'https');\n\nprint_r($result);\n```\n\n### Update web prefix\n\n```php\n# Include the Autoloader (see \"Libraries\" for install instructions)\nrequire 'vendor/autoload.php';\nuse Mailgun\\Mailgun;\n\n# Instantiate the client.\n$mgClient = Mailgun::create('KEY', 'FULL_DOMAIN_URL');\n$domain = \"DOMAIN\";\n\n# Issue the call to the client.\n$result = $mgClient-\u003edomains()-\u003eupdateWebPrefix($domain, 'tracking');\nprint_r($result);\n```\n\n - Example of response\n```\nMailgun\\Model\\Domain\\WebPrefixResponse Object\n(\n    [message:Mailgun\\Model\\Domain\\AbstractDomainResponse:private] =\u003e Domain web prefix updated\n    [domain:Mailgun\\Model\\Domain\\AbstractDomainResponse:private] =\u003e\n    [inboundDnsRecords:Mailgun\\Model\\Domain\\AbstractDomainResponse:private] =\u003e Array\n        (\n        )\n    [outboundDnsRecords:Mailgun\\Model\\Domain\\AbstractDomainResponse:private] =\u003e Array\n        (\n        )\n)\n```\n\n### Custom http request to the API\n\n```php\n\u003c?php\n# Include the Autoloader (see \"Libraries\" for install instructions)\nrequire 'vendor/autoload.php';\nuse Mailgun\\Mailgun;\n\n# Instantiate the client.\n$mgClient = Mailgun::create('KEY', 'ENDPOINT');\n$domain = \"DOMAIN\";\n\n$path = 'some path';\n$params = [];\n\n# Issue the call to the client.\n$resultPost = $mgClient-\u003ehttpClient()-\u003ehttpPost($path, $params);\n\n$resultGet = $mgClient-\u003ehttpClient()-\u003ehttpGet($path, $params);\n\n$resultPut = $mgClient-\u003ehttpClient()-\u003ehttpPut($path, $params);\n\n$resultDelete = $mgClient-\u003ehttpClient()-\u003ehttpDelete($path, $params);\n\n```\n\n\n### SubAccounts\n\n```php\n//Enable Sub Account\ntry {\n    $items = $mgClient-\u003esubaccounts()-\u003eenable($id);\n} catch (Exception $exception) {\n    echo sprintf('HTTP CODE - %s,', $exception-\u003egetCode());\n    echo sprintf('Error - %s', $exception-\u003egetMessage());\n}\n\n//Create a new Sub Account\ntry {\n    $items = $mgClient-\u003esubaccounts()-\u003ecreate('some name');\n} catch (Exception $exception) {\n    echo sprintf('HTTP CODE - %s,', $exception-\u003egetCode());\n    echo sprintf('Error - %s', $exception-\u003egetMessage());\n}\n\n//Get All\ntry {\n    $items = $mgClient-\u003esubaccounts()-\u003eindex();\n\n    print_r($items-\u003egetItems());\n} catch (Exception $exception) {\n    echo sprintf('HTTP CODE - %s,', $exception-\u003egetCode());\n    echo sprintf('Error - %s', $exception-\u003egetMessage());\n}\n```\n### Performing API Requests \"On Behalf Of\" Subaccounts\nMore Detailed you can read here - [https://help.mailgun.com/hc/en-us/articles/16380043681435-Subaccounts#01H2VMHAW8CN4A7WXM6ZFNSH4R](https://help.mailgun.com/hc/en-us/articles/16380043681435-Subaccounts#01H2VMHAW8CN4A7WXM6ZFNSH4R)\n```php\n$mgClient = Mailgun::create(\n    'xxx',\n    'yyy',\n    $subAccountId\n);\n```\n\n```php\nuse Mailgun\\HttpClient\\HttpClientConfigurator;\nuse Mailgun\\Hydrator\\NoopHydrator;\n\n$configurator = new HttpClientConfigurator();\n$configurator-\u003esetEndpoint('http://bin.mailgun.net/aecf68de');\n$configurator-\u003esetApiKey('your_api_key_here');\n$configurator-\u003esetSubAccountId($subAccountId)\n```\n\n### Load data from the Analytics API\n\n```php\n\u003c?php\n# Include the Autoloader (see \"Libraries\" for install instructions)\nrequire 'vendor/autoload.php';\n\nuse Mailgun\\Mailgun;\n\n# Instantiate the client.\n$mgClient = Mailgun::create('xxx');\n$domain = \"xxx.mailgun.org\";\n\n$payload = [\n    \"resolution\" =\u003e \"day\",\n    \"metrics\" =\u003e [\n        \"accepted_count\",\n        \"delivered_count\",\n        \"clicked_rate\",\n        \"opened_rate\"\n    ],\n    \"include_aggregates\" =\u003e true,\n    \"start\" =\u003e \"Sun, 22 Dec 2024 18:29:02 +0000\",\n    \"dimensions\" =\u003e [\n        \"time\"\n    ],\n    \"end\" =\u003e \"Wed, 25 Dec 2024 18:29:02 +0000\",\n    \"include_subaccounts\" =\u003e true\n];\n\n$result = $mgClient-\u003emetrics()-\u003eloadMetrics($payload);\n\nprint_r($result-\u003egetItems());\n\n````\n\n### All usage examples\n\nYou will find more detailed documentation at [/doc](doc/index.md) and on\n[https://documentation.mailgun.com](https://documentation.mailgun.com/docs/mailgun/api-reference).\n\n### Response\n\nThe result of an API call is, by default, a domain object. This will make it easy\nto understand the response without reading the documentation. One can just read the\ndoc blocks on the response classes. This provides an excellent IDE integration.\n\n```php\n$mg = Mailgun::create('your_api_key_here');\n$dns = $mg-\u003edomains()-\u003eshow('example.com')-\u003egetInboundDNSRecords();\n\nforeach ($dns as $record) {\n  echo $record-\u003egetType();\n}\n```\n\nIf you'd rather work with an array than an object you can inject the `ArrayHydrator`\nto the Mailgun class.\n\n```php\nuse Mailgun\\Hydrator\\ArrayHydrator;\n\n$configurator = new HttpClientConfigurator();\n$configurator-\u003esetApiKey('your_api_key_here');\n\n$mg = new Mailgun($configurator, new ArrayHydrator());\n$data = $mg-\u003edomains()-\u003eshow('example.com');\n\nforeach ($data['receiving_dns_records'] as $record) {\n  echo isset($record['record_type']) ? $record['record_type'] : null;\n}\n```\n\nYou can also use the `NoopHydrator` to get a PSR7 Response returned from\nthe API calls.\n\n**Warning: When using `NoopHydrator` there will be no exceptions on a non-200 response.**\n\n### Debugging\n\nDebugging the PHP SDK can be helpful when things aren't working quite right.\nTo debug the SDK, here are some suggestions:\n\nSet the endpoint to Mailgun's Postbin. A Postbin is a web service that allows you to\npost data, which then you can display it through a browser. Using Postbin is an easy way\nto quickly determine what data you're transmitting to Mailgun's API.\n\n**Step 1 - Create a new Postbin.**\nGo to http://bin.mailgun.net. The Postbin will generate a special URL. Save that URL.\n\n**Step 2 - Instantiate the Mailgun client using Postbin.**\n\n*Tip: The bin id will be the URL part after bin.mailgun.net. It will be random generated letters and numbers.\nFor example, the bin id in this URL (http://bin.mailgun.net/aecf68de) is `aecf68de`.*\n\n```php\nuse Mailgun\\HttpClient\\HttpClientConfigurator;\nuse Mailgun\\Hydrator\\NoopHydrator;\n\n$configurator = new HttpClientConfigurator();\n$configurator-\u003esetEndpoint('http://bin.mailgun.net/aecf68de');\n$configurator-\u003esetApiKey('your_api_key_here');\n$configurator-\u003esetDebug(true);\n\n$mg = new Mailgun($configurator, new NoopHydrator());\n\n# Now, compose and send your message.\n$mg-\u003emessages()-\u003esend('example.com', [\n  'from'    =\u003e 'bob@example.com',\n  'to'      =\u003e 'sally@example.com',\n  'subject' =\u003e 'The PHP SDK is awesome!',\n  'text'    =\u003e 'It is so simple to send a message.'\n]);\n```\n### Additional Info\n\nFor usage examples on each API endpoint, head over to our official documentation\npages.\n\nThis SDK includes a [Message Builder](src/Message/README.md),\n[Batch Message](src/Message/README.md).\n\nMessage Builder allows you to quickly create the array of parameters, required\nto send a message, by calling a methods for each parameter.\nBatch Message is an extension of Message Builder, and allows you to easily send\na batch message job within a few seconds. The complexity of\nbatch messaging is eliminated!\n\n## Framework integration\n\nIf you are using a framework you might consider these composer packages to make the framework integration easier.\n\n* [tehplague/swiftmailer-mailgun-bundle](https://github.com/tehplague/swiftmailer-mailgun-bundle) for Symfony\n* [katanyoo/yii2-mailgun-mailer](https://github.com/katanyoo/yii2-mailgun-mailer) for Yii2\n* [narendravaghela/cakephp-mailgun](https://github.com/narendravaghela/cakephp-mailgun) for CakePHP\n* [drupal/mailgun](https://www.drupal.org/project/mailgun) for Drupal\n* [Laravel](https://laravel.com/docs/8.x/mail#mailgun-driver) Mail comes with Mailgun driver support\n\n## Contribute\n\nThis SDK is an Open Source under the MIT license. It is, thus, maintained by collaborators and contributors.\n\nFeel free to contribute in any way. As an example you may:\n* Trying out the `dev-master` code\n* Create issues if you find problems\n* Reply to other people's issues\n* Review PRs\n\n### Running the test code\n\nIf you want to run the tests you should run the following commands:\n\n```terminal\ngit clone git@github.com:mailgun/mailgun-php.git\ncd mailgun-php\ncomposer update\ncomposer test\n```\n\n## Support and Feedback\n\nBe sure to visit the Mailgun official\n[documentation website](http://documentation.mailgun.com/) for additional\ninformation about our API.\n\nIf you find a bug, please submit the issue in Github directly.\n[Mailgun-PHP Issues](https://github.com/mailgun/mailgun-php/issues)\n\nAs always, if you need additional assistance, drop us a note through your account at\n[https://app.mailgun.com/support](https://app.mailgun.com/support).\n\n## Examples section\n[Examples section](doc/examples.md) contains examples of how to use the SDK.\n","funding_links":[],"categories":["Table of Contents","Third Party APIs","第三方API","配置 Configuration","Configuration","第三方API( Third Party APIs )"],"sub_categories":["Third Party APIs","第三方api Third Party APIs"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmailgun%2Fmailgun-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmailgun%2Fmailgun-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmailgun%2Fmailgun-php/lists"}