PHP code example of mailgun / mailgun-php

1. Go to this page and download the library: Download mailgun/mailgun-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mailgun / mailgun-php example snippets



use Mailgun\Mailgun;

// First, instantiate the SDK with your API credentials
$mg = Mailgun::create('key-example'); // For US servers
$mg = Mailgun::create('key-example', 'https://api.eu.mailgun.net'); // For EU servers

// Now, compose and send your message.
// $mg->messages()->send($domain, $params);
$mg->messages()->send('example.com', [
  'from'    => '[email protected]',
  'to'      => '[email protected]',
  'subject' => 'The PHP SDK is awesome!',
  'text'    => 'It is so simple to send a message.'
]);

# Include the Autoloader (see "Libraries" for install instructions)
::create('KEY', 'FULL_DOMAIN_URL');
$domain = "DOMAIN";

# Issue the call to the client.
$result = $mgClient->domains()->updateWebScheme($domain, 'https');

print_r($result);

# Include the Autoloader (see "Libraries" for install instructions)
::create('KEY', 'FULL_DOMAIN_URL');
$domain = "DOMAIN";

# Issue the call to the client.
$result = $mgClient->domains()->updateWebPrefix($domain, 'tracking');
print_r($result);


# Include the Autoloader (see "Libraries" for install instructions)
te('KEY', 'ENDPOINT');
$domain = "DOMAIN";

$path = 'some path';
$params = [];

# Issue the call to the client.
$resultPost = $mgClient->httpClient()->httpPost($path, $params);

$resultGet = $mgClient->httpClient()->httpGet($path, $params);

$resultPut = $mgClient->httpClient()->httpPut($path, $params);

$resultDelete = $mgClient->httpClient()->httpDelete($path, $params);


//Enable Sub Account
try {
    $items = $mgClient->subaccounts()->enable($id);
} catch (Exception $exception) {
    echo sprintf('HTTP CODE - %s,', $exception->getCode());
    echo sprintf('Error - %s', $exception->getMessage());
}

//Create a new Sub Account
try {
    $items = $mgClient->subaccounts()->create('some name');
} catch (Exception $exception) {
    echo sprintf('HTTP CODE - %s,', $exception->getCode());
    echo sprintf('Error - %s', $exception->getMessage());
}

//Get All
try {
    $items = $mgClient->subaccounts()->index();

    print_r($items->getItems());
} catch (Exception $exception) {
    echo sprintf('HTTP CODE - %s,', $exception->getCode());
    echo sprintf('Error - %s', $exception->getMessage());
}

$mgClient = Mailgun::create(
    'xxx',
    'yyy',
    $subAccountId
);

use Mailgun\HttpClient\HttpClientConfigurator;
use Mailgun\Hydrator\NoopHydrator;

$configurator = new HttpClientConfigurator();
$configurator->setEndpoint('http://bin.mailgun.net/aecf68de');
$configurator->setApiKey('key-example');
$configurator->setSubAccountId($subAccountId)

$mg = Mailgun::create('key-example');
$dns = $mg->domains()->show('example.com')->getInboundDNSRecords();

foreach ($dns as $record) {
  echo $record->getType();
}

use Mailgun\Hydrator\ArrayHydrator;

$configurator = new HttpClientConfigurator();
$configurator->setApiKey('key-example');

$mg = new Mailgun($configurator, new ArrayHydrator());
$data = $mg->domains()->show('example.com');

foreach ($data['receiving_dns_records'] as $record) {
  echo isset($record['record_type']) ? $record['record_type'] : null;
}

use Mailgun\HttpClient\HttpClientConfigurator;
use Mailgun\Hydrator\NoopHydrator;

$configurator = new HttpClientConfigurator();
$configurator->setEndpoint('http://bin.mailgun.net/aecf68de');
$configurator->setApiKey('key-example');
$configurator->setDebug(true);

$mg = new Mailgun($configurator, new NoopHydrator());

# Now, compose and send your message.
$mg->messages()->send('example.com', [
  'from'    => '[email protected]',
  'to'      => '[email protected]',
  'subject' => 'The PHP SDK is awesome!',
  'text'    => 'It is so simple to send a message.'
]);
bash
curl -sS https://getcomposer.org/installer | php
bash
composer 
terminal
git clone [email protected]:mailgun/mailgun-php.git
cd mailgun-php
composer update
composer test