PHP code example of mailjet / mailjet-apiv3-php

1. Go to this page and download the library: Download mailjet/mailjet-apiv3-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/ */

    

mailjet / mailjet-apiv3-php example snippets


use \Mailjet\Resources;

// getenv will allow us to get the MJ_APIKEY_PUBLIC/PRIVATE variables we created before:

$apikey = getenv('MJ_APIKEY_PUBLIC');
$apisecret = getenv('MJ_APIKEY_PRIVATE');

$mj = new \Mailjet\Client($apikey, $apisecret);

// or, without using environment variables:

$apikey = 'your API key';
$apisecret = 'your API secret';

$mj = new \Mailjet\Client($apikey, $apisecret);


Mailjet\Resources;

// Use your saved credentials, specify that you are using Send API v3.1

$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'),true,['version' => 'v3.1']);

// Define your request body

$body = [
    'Messages' => [
        [
            'From' => [
                'Email' => "$SENDER_EMAIL",
                'Name' => "Me"
            ],
            'To' => [
                [
                    'Email' => "$RECIPIENT_EMAIL",
                    'Name' => "You"
                ]
            ],
            'Subject' => "My first Mailjet Email!",
            'TextPart' => "Greetings from Mailjet!",
            'HTMLPart' => "<h3>Dear passenger 1, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3>
            <br />May the delivery force be with you!"
        ]
    ]
];

// All resources are located in the Resources class

$response = $mj->post(Resources::$Email, ['body' => $body]);

// Read the response

$response->success() && var_dump($response->getData());

$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'),true,['version' => 'v3.1']);

$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'),
                          getenv('MJ_APIKEY_PRIVATE'), true,
                          ['url' => "api.us.mailjet.com"]
                        );

$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'), false);


/*
Create a new contact:
*/
 \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$body = [
    'Email' => "[email protected]"
];
$response = $mj->post(Resources::$Contact, ['body' => $body]);
$response->success() && var_dump($response->getData());


/*
Manage the subscription status of a contact to multiple lists
*/
C'), getenv('MJ_APIKEY_PRIVATE'));
$body = [
    'ContactsLists' => [
        [
            'ListID' => "$ListID_1",
            'Action' => "addnoforce"
        ],
        [
            'ListID' => "$ListID_2",
            'Action' => "addforce"
        ]
    ]
];
$response = $mj->post(Resources::$ContactManagecontactslists, ['id' => $id, 'body' => $body]);
$response->success() && var_dump($response->getData());


/*
Retrieve all contacts:
*/
\Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$response = $mj->get(Resources::$Contact);
$response->success() && var_dump($response->getData());


/*
Retrieve all contacts that are not in the campaign exclusion list :
*/
etenv('MJ_APIKEY_PRIVATE'));
$filters = [
  'IsExcludedFromCampaigns' => 'false'
];
$response = $mj->get(Resources::$Contact, ['filters' => $filters]);
$response->success() && var_dump($response->getData());


/*
Retrieve a specific contact ID :
*/
lient(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$filters = [
    'Limit'=>40,  // default is 10, max is 1000
    'Offset'=>20,
    'Sort'=>'ArrivedAt DESC',
    'Contact'=>$contact->ID,
    'showSubject'=>true
];
$response = $mj->get(Resources::$Message, ['filters'=>$filters]);
$response->success() && var_dump($response->getData());


/*
Retrieve a specific contact ID :
*/
lient(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$response = $mj->get(Resources::$Contact, ['id' => $id]);
$response->success() && var_dump($response->getData());


/*
Update the contact properties for a contact:
*/
('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$body = [
    'first_name' => "John",
    'last_name' => "Smith"
];
$response = $mj->put(Resources::$ContactData, ['id' => $id, 'body' => $body]);
$response->success() && var_dump($response->getData());


/*
Delete an email template:
*/
iljet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$response = $mj->delete(Resources::$Template, ['id' => $id]);
$response->success() && var_dump($response->getData());


/*
Delete a contact from contact list:
*/
nt(
  getenv('MJ_APIKEY_PUBLIC'),
  getenv('MJ_APIKEY_PRIVATE'),
  true,
  ['version' => 'v4']
);
$response = $mj->delete(Resources::$Contacts, ['ID' => xxxx]);
$response->success() && var_dump($response->getData());

$mj = new \Mailjet\Client(getenv('MJ_APITOKEN'),
                          NULL, true,
                          ['url' => "api.mailjet.com", 'version' => 'v4', 'call' => false]
                        );

//Send an SMS
$mj = new \Mailjet\Client(getenv('MJ_APITOKEN'),
                          NULL, true,
                          ['url' => "api.mailjet.com", 'version' => 'v4', 'call' => false]
                        );
$body = [
    'Text' => "Have a nice SMS flight with Mailjet !",
    'To' => "+336000000000",
    'From' => "MJ Pilot",
];
$response = $mj->post(Resources::$SmsSend, ['body' => $body]);
$response->success() && var_dump($response->getData());



use \Mailjet\Resources;

$mj = new \Mailjet\Client(
    'xxx',
    'xxx',
    true,
);

try {
    $subject = \Mailjet\Utility\StringUtility::utfStringNotation("This is subject with emoji  🤑" );

    $body = [
        'Locale' => "en_US",
        'Sender' => "[email protected]",
        'SenderEmail' => "[email protected]",
        'Subject' => $subject,
        'TemplateID' => 12345,
        'ContactsListID' => 12345,
        'Title' => "Emoji Test" . time(),

    ];
    $response = $mj->post(
        Resources::$Campaigndraft,
        [
            'body' => $body,
        ]
    );
    print_r($response->getData());
} catch (Throwable $throwable) {
    print_r($throwable->getMessage());
}



use \Mailjet\Resources;

$mj = new \Mailjet\Client(
    'xxx',
    'yyy',
    true,
    ['version' => 'v3']
);
try {
    //Note: If no query parameters are used, only campaigns sent since 00:00 UTC on the current day will be returned.
    $filters = [
        'period' => 'Year',
    ];
    $response = $mj->get(Resources::$Campaign, ['filters' => $filters]);
    print_r($response->getBody());

} catch (Throwable $throwable) {
    print_r($throwable->getMessage());
}



use \Mailjet\Resources;

$mj = new \Mailjet\Client(
    'xxxx',
    'yyyy',
    true,
    ['version' => 'v3']
);
try {
    $filters = [
        'CampaignID' => 123456,
    ];
    $response = $mj->get(Resources::$Useragentstatistics, ['filters' => $filters]);
    print_r($response->getBody());


} catch (Throwable $throwable) {
    print_r($throwable->getMessage());
}



use \Mailjet\Resources;

$mj = new \Mailjet\Client(
    'xxx',
    'yyy',
    true,
    ['version' => 'v3']
);
try {
    $response = $mj->get(Resources::$Campaignoverview);
    print_r($response->getBody());


} catch (Throwable $throwable) {
    print_r($throwable->getMessage());
}
bash
composer 

$response = $mj->delete(Resources::$Template, ['id' => $id]);
$response = $mj->put(Resources::$ContactData, ['id' => $id, 'body' => $body]);
$response = $mj->post(Resources::$ContactManagecontactslists, ['id' => $id, 'body' => $body]);