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);
/*
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());
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());
}