Skip to content

afiocre/mailjet-apiv3-php-simple

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

[API v3] Mailjet PHP Wrapper

Introduction

Provides a simple PHP library for the last version of the MailJet API. The goal of this component is to simplify the usage of the MailJet API for PHP developers.

Prerequisites

Make sure to have the following details:

  • Mailjet API Key
  • Mailjet API Secret Key
  • PHP
  • This PHP class

Installation

First clone the repository

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

Go into the mailjet-apiv3-php-simple folder and create an empty file named mailjetapi.php

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

You are now ready to go !

Usage

First, you need to edit our php-mailjet-v3-simple.class.php file to assign your API keys to the variables $apiKey and $secretKey on lines 23 and 24 between simple quotes. You can find this keys in your Mailjet account here : https://app.mailjet.com/account/api_keys

Then, in your mailjetapi.php file, you need to include our php class :

include("php-mailjet-v3-simple.class.php");

Be Careful : Make sure that both mailjetapi.php and php-mailjet-v3-simple.class.php files are in the same folder to make this include work

Now you can start working with the mailjetapi.php file by creating a new Mailjet object :

$mj = new Mailjet();

This basically starts the engine. Now what you're going to do next depends on what you want to GET, POST, PUT or DELETE from the Mailjet servers through the API. Take a tour on the Reference documentation to see all the resources available.

Next you will specify which resource to call this way (resource Contact in this example) with an array of parameters $params :

$mj->contact($params);

Info : If you don't specify the method of the request in the array $params (see examples below), it will be a GET.

Examples

SendAPI

  • A function to send an email :
function sendEmail() {
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "from" => "ms.mailjet@example.com",
        "to" => "mr.mailjet@example.com",
        "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;
}
  • A function to send an email with some attachments (absolute paths on your computer) :
function sendEmailWithAttachments() {
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "from" => "ms.mailjet@example.com",
        "to" => "mr.mailjet@example.com",
        "subject" => "Hello World!",
        "text" => "Greetings from Mailjet.",
        "attachment" => array("@/path/to/first/file.txt", "@/path/to/second/file.txt")
    );

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

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

    return $result;
}
  • A function to send an email with some inline attachments (absolute paths on your computer) :
function sendEmailWithInlineAttachments() {
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "from" => "ms.mailjet@example.com",
        "to" => "mr.mailjet@example.com",
        "subject" => "Hello World!",
        "html" => "<html>Greetings from Mailjet <img src=\"cid:photo1.jpg\"><img src=\"cid:photo2.jpg\"></html>",
	"inlineattachment" => array("@/path/to/photo1.jpg", "@/path/to/photo2.jpg")
    );

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

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

    return $result;
}

Account Settings

  • A function to get your profile information :
function viewProfileInfo() {
    $mj = new Mailjet();
    $result = $mj->myprofile();

    if ($mj->_response_code == 200)
       echo "success - got profile information";
    else
       echo "error - ".$mj->_response_code;
}
  • A function to update the field AddressCity of your profile :
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;
}

Contact Lists

  • A function to print the list of your contacts :
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;
}
  • A function to update your contactData resource with ID $id, using arrays :
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;
}
  • A function to create a list with name $Lname :
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;
}
  • A function to get a list with ID $listID :
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;
}

Note : You can use unique fields of resources instead of IDs, like "unique" => "test@gmail.com" in your params array for this example

  • A function to create a contact with email $Cemail :
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 ".$Cname;
    else
	   echo "error - ".$mj->_response_code;

    return $result;
}
  • A function to add the contact which ID is $contactID to the list which ID is $listID :
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;
}
  • A function to delete the list which ID is $listID :
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;
}

Newsletters

  • A function to get the HTML body of the newsletter $newsletter_id :
function getHTMLbody($newsletter_id) {
    $mj = new Mailjet();
    $params = array(
        "method" => "GET",
        "ID" => $newsletter_id
    );

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

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

    return $result;
}

Note : You need to put an underscore character before your parameters that are not filters in a GET request

  • A function to add an HTML body $html_content to a newsletter $newsletter_id :
function addHTMLbody($newsletter_id, $html_content) {
    $mj = new Mailjet();
    $params = array(
        "method" => "PUT",
        "ID" => $newsletter_id,
        "html_content" => $html_content
    );

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

    if ($mj->_response_code == 200)
        echo "success - body ".$html_content." added to the newsletter ". $newsletter_id;
    else
        echo "error - ".$mj->_response_code;

    return $result;
}

Reporting issues

Open an issue on github.

About

[API v3] Simple PHP wrapper for the Mailjet API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%