PHP code example of mailjet / mailjet-apiv3-php-simple

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




$mj = new Mailjet( $apiKey, $secretKey );

$mj->contact($params);

function sendEmail()
{
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "from" => "[email protected]",
        "to" => "[email protected]",
        "subject" => "Hello World!",
        "text" => "Greetings from Mailjet."
    );

    $result = $mj->sendEmail($params);

    if ($mj->_response_code == 200)
       echo "success - email sent";
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

      "to" => array(
          "[email protected]",
          "[email protected]",
          ...
      ),
      "cc" => array(
          "[email protected]",
          "[email protected]",
          ...
      ),
      "bcc" => array(
          "[email protected]",
          "[email protected]",
          ...
      )
      

function sendEmailWithAttachments()
{
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "from" => "[email protected]",
        "to" => "[email protected]",
        "subject" => "Hello World!",
        "text" => "Greetings from Mailjet.",
        "attachment" => array(
            "MyFirstAttachment" => "@/path/to/first/file.txt",
            "@/path/to/second/file.pdf",
            "MyThirdAttachment" => "@/path/to/third/file.jpg"
            )
    );

    $result = $mj->sendEmail($params);

    if ($mj->_response_code == 200)
       echo "success - email sent";
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

function sendEmailWithInlineAttachments()
{
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "from" => "[email protected]",
        "to" => "[email protected]",
        "subject" => "Hello World!",
        "html" => "<html>Greetings from Mailjet <img src=\"cid:MaPhoto\"><img src=\"cid:photo2.png\"></html>",
        "inlineattachment" => array(
            "MaPhoto" => "@/path/to/photo1.jpg",
            "@/path/to/photo2.png"
            )
    );

    $result = $mj->sendEmail($params);

    if ($mj->_response_code == 200)
       echo "success - email sent";
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

function sendEmailWithCustomID()
{
    $mj = new Mailjet();

    $params = array(
        "method" => "POST",
        "from" => "[email protected]",
        "to" => "[email protected]",
        "subject" => "Hello World!",
        "text" => "Greetings from Mailjet.",
        "mj-customid" => "helloworld"
    );

    $result = $mj->sendEmail($params);

    if ($mj->_response_code == 200)
       echo "success - email sent";
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

function sendEmailWithEventPayload()
{
    $mj = new Mailjet();

    $params = array(
        "method" => "POST",
        "from" => "[email protected]",
        "to" => "[email protected]",
        "subject" => "Hello World!",
        "text" => "Greetings from Mailjet.",
        "mj-eventpayload" => '{"message": "helloworld"}'
    );

    $result = $mj->sendEmail($params);

    if ($mj->_response_code == 200)
       echo "success - email sent";
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

function viewProfileInfo()
{
    $mj = new Mailjet();
    $result = $mj->myprofile();

    if ($mj->_response_code == 200)
       echo "success - got profile information";
    else
       echo "error - ".$mj->_response_code;
}

function updateProfileInfo()
{
    $mj = new Mailjet();
    $params = array(
        "method" => "PUT",
        "AddressCity" => "New York"
    );

    $result = $mj->myprofile($params);

    if ($mj->_response_code == 200)
       echo "success - field AddressCity changed";
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

function listContacts()
{
    $mj = new Mailjet();
    $result = $mj->contact();

    if ($mj->_response_code == 200)
       echo "success - listed contacts";
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

function updateContactData($id)
{
    $mj = new Mailjet();
    $data = array(
        array(
            'Name' => 'lastname',
            'Value' => 'Jet'
        ),
        array(
            'Name' => 'firstname',
            'Value' => 'Mail'
        )
    );

    $params = array(
        'ID' => $id,
        'Data' => $data,
        'method' => 'PUT'
    );

    $result = $mj->contactdata($params);

    if ($mj->_response_code == 200)
       echo "success - data changed";
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

function createList($Lname)
{
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "Name" => $Lname
    );

    $result = $mj->contactslist($params);

    if ($mj->_response_code == 201)
       echo "success - created list ".$Lname;
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

function getList($listID)
{
    $mj = new Mailjet();
    $params = array(
        "method" => "VIEW",
        "ID" => $listID
    );

    $result = $mj->contactslist($params);

    if ($mj->_response_code == 200)
       echo "success - got list ".$listID;
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

function createContact($Cemail)
{
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "Email" => $Cemail
    );

    $result = $mj->contact($params);

    if ($mj->_response_code == 201)
       echo "success - created contact ".$Cemail;
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

/**
 *  @param int  $contactID  The ID of the contact
 */
function getContactsLists ($contactID)
{
    $mj = new Mailjet();
    $params = array(
        "ID"    =>  $contactID
    );
    $result = $mj->contactGetContactsLists($params);
    if ($mj->_response_code == 201)
       echo "success - fetched lists for contact ".$contactID;
    else
       echo "error - ".$mj->_response_code;
    return $result;
}

function addContactToList($contactID, $listID)
{
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "ContactID" => $contactID,
        "ListID" => $listID,
        "IsActive" => "True"
    );

    $result = $mj->listrecipient($params);

    if ($mj->_response_code == 201)
       echo "success - contact ".$contactID." added to the list ".$listID;
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

/**
 *  @param  array   $contact    An array describing a contact.
 *                              Example below the function.
 *  @param  int     $listID     The ID of the list.
 *
 */
 function addDetailedContactToList ($contact, $listID)
 {
     $mj = new Mailjet(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
     $params = array(
         "method" => "POST",
         "ID" => $listID
     );
     $params = array_merge($params, $contact);
     $result = $mj->contactslistManageContact($params);
     if ($mj->_response_code == 201)
        echo "success - detailed contact ".$contactID." added to the list ".$listID;
     else
        echo "error - ".$mj->_response_code;
     return $result;
 }
 // $contact array example
 /*  $contact = array(
  *      "Email"         =>  "[email protected]",   // Mandatory field!
  *      "Name"          =>  "FooBar",
  *      "Action"        =>  "addnoforce",
  *      "Properties"    =>  array(
  *          "Prop1" =>  "value1",
  *          ...
  *      )
  *  );
  */

/**
 *  @param int      $contactID  The ID of the contact
 *  @param array    $lists      An array of arrays,
 *                              each one describing a list.
 *                              Example below the function.
 */
function addContactToLists ($contactID, $lists) {
    $mj = Mailjet('', '');
    $params = array(
        "method"        =>  "POST",
        "ID"            =>  $contactID,
        "ContactsLists"  =>  $lists
    );
    $result = $mj->contactManageContactsLists($params);
    if ($mj->_response_code == 204)
       echo "success - contact ".$contactID." added to the list(s)";
    else
       echo "error - ".$mj->_response_code;
    return $result;
}
// $lists array example
/*  $lists = array(
 *      array(
 *          "ListID"    =>  1,
 *          "Action"    =>  "remove"
 *      ),
 *      array(
 *          "ListID"    =>  4,
 *          "Action"    =>  "addnoforce"
 *      )
 *      // ...
 *  );
 */

function deleteList($listID)
{
    $mj = new Mailjet();
    $params = array(
        "method" => "DELETE",
        "ID" => $listID
    );

    $result = $mj->contactslist($params);

    if ($mj->_response_code == 204)
       echo "success - deleted list";
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

function getUnsubscribedContactsFromList($listID)
{
    $mj = new Mailjet();

    $params = array(
        "method" => "GET",
        "ContactsList" => $listID,
        "Unsub" => true
    );

    $result = $mj->listrecipient($params);

    if ($mj->_response_code == 200)
       echo "success - got unsubscribed contact(s) ";
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

function getContact($contactID)
{
    $mj = new Mailjet();
    $params = array(
        "method" => "VIEW",
        "ID" => $contactID
    );

    $result = $mj->contact($params);

    if ($mj->_response_code == 200)
       echo "success - got contact ".$contactID;
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

/**
 *  @param  array   $contacts   Should be an array of arrays,
 *                              each one describing a contact.
 *                              Example below the function.
 *
 *  @param  array   $lists      Should be an array of arrays,
 *                              each one describing a list.
 *                              Example below the function.
 */
function asyncTransferContactsToLists ($contacts, $lists)
{

    $mj = new Mailjet('', '');

    $params = array(
        "method"        =>  "POST",
        "ContactsLists" =>  $lists,
        "Contacts"      =>  $contacts
    );

    $asyncJobResponse = $mj->contactManageManyContacts($params);

    if ($mj->_response_code == 200)
        echo "success - proper request";
    else
        echo "error while accessing the resource - ".$mj->_response_code;

    return $asyncJobResponse;
}

// $contacts array example
/*  $contacts = array(
 *      array(
 *          "Email" =>  "[email protected]",
 *          ...
 *      ),
 *      array(
 *          "Email" =>  "[email protected]",
 *          ...
 *      )
 *  );
 */

// $lists array example
/*  $lists = array(
 *      array(
 *          "ListID"    =>  1,
 *          "Action"    =>  "remove"
 *      ),
 *      array(
 *          "ListID"    =>  4,
 *          "Action"    =>  "addnoforce"
 *      )
 *  );
 */

/**
 *  @param  array   $contacts   Should be an array of arrays,
 *                              each one describing a contact.
 *                              Example below the function.
 *
 *  @param  int     $listID     The list ID.
 *
 */
function asyncManageContactsToList ($contacts, $listID)
{

    $mj = new Mailjet('', '');

    $params = array(
        "method"        =>  "POST",
        "Action"        =>  "unsub",
        "Contacts"      =>  $contacts
    );

    $asyncJobResponse = $mj->contactslistManageManyContacts($params);

    if ($mj->_response_code == 200)
        echo "success - proper request";
    else
        echo "error while accessing the resource - ".$mj->_response_code;

    return $asyncJobResponse;
}

// $contacts array example
/*  $contacts = array(
 *      array(
 *          "Email" =>  "[email protected]",
 *          ...
 *      ),
 *      array(
 *          "Email" =>  "[email protected]",
 *          ...
 *      )
 *  );
 */

/**
 *  @param array $asyncJobResponse The result object returned by the async job. (See function above)
 *
 */
function getAsyncJobStatus ($asyncJobResponse)
{
    $mj = new Mailjet('', '');

    $jobID = $asyncJobResponse->Data[0]->JobID;

    $statusParams = array(
        "method"    =>  "VIEW",
        "ID"        =>  $jobID
    );

    $status = $mj->contactManageManyContacts($statusParams);
    // OR
    // $status = $mj->contactslistManageManyContacts($statusParams);

    if ($mj->_response_code == 200)
       echo "success - status obtained";
    else
       echo "error while retrieving the status - ".$mj->_response_code;

    return status;
}

$CSVContent = file_get_contents('test.csv');

$uploadParams = array(
    "method" => "POST",
    "ID" => $listID,
    "csv_content" => $CSVContent
);

$csvUpload = $mj->uploadCSVContactslistData($uploadParams);

if ($mj->_response_code == 200)
   echo "success - uploaded CSV file ";
else
   echo "error - ".$mj->_response_code;

$assignParams = array(
    "method" => "POST",
    "ContactsListID" => $listID,
    "DataID" => $csvUpload->ID,
    "Method" => "addnoforce"
);

$csvAssign = $mj->csvimport($assignParams);

if ($mj->_response_code == 201)
   echo "success - CSV data ".$csvUpload->ID." assigned to contactslist ".$listID;
else
   echo "error - ".$mj->_response_code;

$monitorParmas = array (
    "method" => "VIEW",
    "ID" => $csvAssign->Data[0]->ID
);

$res = $mj->batchjob($monitorParmas);

if ($mj->_response_code == 200)
   echo "job ".$res->Data[0]->Status."\n";
else
    echo "error - ".$mj->_response_code."\n";

function getNewsletterDetailcontent($newsletter_id)
{
    $mj = new Mailjet('', '');
    $params = array(
        "method" => "GET",
        "ID" => $newsletter_id
    );

    $result = $mj->newsletterDetailContent($params);

    if ($mj->_response_code == 200)
        echo "success - got content for the newsletter ". $newsletter_id;
    else
        echo "error - ".$mj->_response_code;

    return $result;
}

function scheduleNewsletter($newsletter_id)
{
    $mj = new Mailjet('', '');
    $params = array(
        "method" => "POST",
        "ID" => $newsletter_id,
        "date" => "2014-11-25T10:12:59Z"
    );

    $result = $mj->newsletterSchedule($params);

    if ($mj->_response_code == 201)
        echo "success - schedule done for the newsletter ". $newsletter_id;
    else
        echo "error - ".$mj->_response_code;

    return $result;
}

function sendNewsletter($newsletter_id)
{
    $mj = new Mailjet('', '');
    $params = array(
        "method" => "POST",
        "ID" => $newsletter_id
    );

    $result = $mj->newsletterSend($params);

    if ($mj->_response_code == 201)
        echo "success - newsletter ". $newsletter_id . " has been sent";
    else
        echo "error - ".$mj->_response_code;

    return $result;
}

function testNewsletter($newsletter_id)
{
    $mj = new Mailjet('', '');
    $recipients = array(array('Email' => '[email protected]', 'Name' => 'Mailjet'));
    $params = array(
        "method" => "POST",
        "ID" => $newsletter_id,
        "Recipients" => $recipients
    );

    $result = $mj->newsletterTest($params);

    if ($mj->_response_code == 201)
        echo "success - newsletter ". $newsletter_id . " has been sent";
    else
        echo "error - ".$mj->_response_code;

    return $result;
}

function duplicateNewsletter($newsletter_id)
{
    $mj = new Mailjet('', '');
    $params = array(
        "method" => "POST",
        "EditMode" => "html",
        "Status" => 0,
        "_DuplicateFrom" => $newsletter_id
    );

    $result = $mj->newsletter($params);

    if ($mj->_response_code == 201)
        echo "success - duplicated Newsletter ". $newsletter_id;
    else
        echo "error - ".$mj->_response_code;

    return $result;
}

$params = array (
    "ContactsList"  =>  $contactslistID,
    "Limit" =>  "100"
);

$res = $mj->contacts($params);

$params = array(
        "method" => "POST",
        "EditMode" => "html",
        "Status" => 0,
        "_DuplicateFrom" => $newsletter_id
    );

$result = $mj->newsletter($params);

git clone https://github.com/mailjet/mailjet-apiv3-php-simple.git

cd mailjet-apiv3-php-simple
touch myProjectEmailer.php