PHP

Learn more about how to download and implement our PHP API Integration Library to your own project.

Overview - what this Library is?

We want to provide our clients a way to use our system however they like. That is why we have introduced the Integration Libraries - a group of functions and declarations to help you implementing our API to your system.

How to download and implement the Library

All of our libraries are available here, whereas the specific library for PHP can be found at the following link.

After you have downloaded the library (which is a single .php file), you might want to put it in your PHP directory to have a quick and easy access to it, manually or through your own code.

Insert this line of code at the beginning of your script to be able to use the library:

require_once 'ElasticEmailClient.php';

How to start working with the Library on your project

All of our library's methods are available using the static class named ApiClient. Whether you want to send an email or modify your template, you can do it using the ApiClient class. You can use the ApiTypes class to create the desired object that would be used as a parameter in some calls.

Before everything else, we need to setup an ApiKey, that is the unique identifier of your account. You can do this in two ways:

First option: open up ElasticEmailClient.php in your editor. Then, in the ApiClient class, you can find two fields ApiKey and ApiUri. Change the former's value to your ApiKey available for you here. Don't mind the ApiUri field, it is best to leave it untouched.

Second option: simply overwrite the ApiKey field's value of the static class ApiClient at the start of your code:

ElasticEmailClient\ApiClient::SetApiKey("11111111-2222-3333-4444-555555555555");

This can also be used to swap between the accounts while your program is running, so you can keep this line in mind.

Some basic API calls made using this Library

An example of loading info about your own account and displaying the price per email:

<?php
    require_once 'ElasticEmailClient.php';
    
    ElasticEmailClient\ApiClient::SetApiKey("11111111-2222-3333-4444-555555555555");
    
    function LoadAccount() {	
        $EEaccount = new ElasticEmailClient\Account();
        
        try
        {
            $account = $EEaccount->Load();		
        }
        catch (Exception $e)
        {
            echo 'Something went wrong: ', $e->getMessage(), "\n";
            
            return;
        }
        
        echo $account->priceperemail; // what a deal!
    }
    
    LoadAccount();
?>

One of many ways to save your contacts:

<?php
    require_once 'ElasticEmailClient.php';
    
    ElasticEmailClient\ApiClient::SetApiKey("11111111-2222-3333-4444-555555555555");
    
    function QuickAddContacts($contacts) {	
        $EEcontact = new ElasticEmailClient\Contact();
        
        try
        {
            $EEcontact->QuickAdd($contacts);		
        }
        catch (Exception $e)
        {
            echo 'Something went wrong: ', $e->getMessage(), '\n';
            
            return;
        }		
        
        echo 'Success';
    }
    
    $contacts = [ "test1@test.com", "test2@test.com" ];
    
    QuickAddContacts($contacts);
?>

Sending an email with attachments:

<?php
    require_once 'ElasticEmailClient.php';
    
    ElasticEmailClient\ApiClient::SetApiKey("11111111-2222-3333-4444-555555555555");
    
    function SendEmail($recipients, $subject, $from, $fromName, $text, $html, $attachments) {	
        $EEemail = new ElasticEmailClient\Email();
        
        try
        {
            $response = $EEemail->Send($subject, $from, $fromName, null, null, null, null, null, null, $recipients, array(), array(), array(), array(), array(), null, null, $html, $text, null, null, null, null, null, $attachments);		
        }
        catch (Exception $e)
        {
            echo 'Something went wrong: ', $e->getMessage(), '\n';
            
            return;
        }		
        
        echo 'MsgID to store locally: ', $response->messageid, '\n'; // Available only if sent to a single recipient
        echo 'TransactionID to store locally: ', $response->transactionid;
    }
    
    $recipients = [ "test1@test.com" ];
    $subject = "Your subject";
    $fromEmail = "Your Email";
    $fromName = "Your Company Name";
    $bodyText = "Text body";
    $bodyHtml = "<h1>Html Body</h1>";
    $attachments = array('C:/Users/test.txt');
    
    SendEmail($recipients, $subject, $fromEmail, $fromName, $bodyText, $bodyHtml, $attachments);
?>

License

All code samples are licensed under MIT license.