Logo

JSON EMAIL API

This document describes Eurobate’s JSON interface for pushing EMAIL messages using JSON over HTTPS. You also need to register the IP address you send from in Eurobate's system.

Endpoint URL

  • https://api.eurobate.com/json_api_email.php

Parameter description

Name Required Description
user yes Username provided by Eurobate
password yes Password provided by Eurobate
from_name no The senders from name f.ex "Eurobate" infront of the e-mail from address. The e-mail adress can not be set with the API, but has to be changed in our customer portal
hash no Email template hash id, required if html is not set. The e-mail template is made in our customer portal
html no A html formated email, required if hash is not set
subject yes Subject of the email.
messages yes Array of 1 or more messages to be sent with template variables for each message. Including to_email, to_name, email_var & email_var_data. See JSON messages array example at the bottom of this table.
The actual html message is kept outside this array for size considerations of the JSON payload. Instead having variables in the html code to ensure unique emails per recipient by replacing variables with either e-mail adress or other variables like firstname, lastname, vouchercodes, username or password
to_email yes The e-mail address to send to
to_name no Name of the person email is sent to. Not required
email_var no Array of variables names to be replaced in the e-mail F.eks [%email%], [mail] OR #EMAIL#. The value of the variable in email_var_data must correspond to the order of the email_var array
email_var_data no Value of the email_var to be replaced in the e-mail F.ex the email address which the email is sent to and must correspond to the order of the variable names in the array email_var

JSON messages array

 "messages": [
        {
            "to_email": "user1@eurobate.com",
            "to_name": "",
            "email_var": [
                "[%email%]",
                "[mail]",
                "#EMAIL#",
                "#FIRSTNAME#",
                "#LASTNAME#",
                "#VOUCHERCODE#",
            ],
            "email_var_data": [
                "user1@eurobate.no",
                "user1@eurobate.no",
                "user1@eurobate.no",
                "user1FirstName",
                "user1LastName",
                "P8GH-U7RA-B2G8-USLJ"
            ]
        },
        {
            "to_email": "user2@eurobate.com",
            "to_name": "",
            "email_var": [
                "[%email%]",
                "[mail]",
                "#EMAIL#",
                "#FIRSTNAME#",
                "#LASTNAME#",
                "#VOUCHERCODE#",
            ],
            "email_var_data": [
                "user2@eurobate.com",
                "user2@eurobate.com",
                "user2@eurobate.com",
                "user2FirstName",
                "user2LastName",
                "T7SO-QI8M-JAIQ-CGV4"

            ]
        }
    ]

JSON response

Value Description
email The e-mail address.
error A numerical error code (see below)
info A textual description of the error. (see below)
logon Status for logon
reason A textual reason description of logon failure
uuid Universally unique identifier of the e-mail transaction
bulk_id Bulk id of email sent. If 10 email are sent this is the id which identify this bulk sending of 10 emails.

Error Codes

Error code Error string
0 Ok
1 Missing parameters
2 Wrong user/password
3 IP not authorized
4 Network error
5 Message body is not valid
6 An error occurred when sending the message to the operator
7 Wrong number format
8 User not allowed
9 Unspecified error
10 Illegal originator
11 Countrycode not allowed Your account is not open for this country, contact you sales person
12 Not in use
13 PKPass is missing!
14 PKPass could not be created
15 PKPasstemplate id is missing
16 JSON decode error
17 PKPasstemplate directory does not exist
18 Invalid sendtime date format. Valid dateformat: YYYY-MM-DD HH:MM
19 Date for sending is too far into the future
20 Prepaid balance is too low
21 Owner ID is missing settings for E-mail
22 E-mail outbox ID is missing
23 Invalid e-mail
24 Subject is missing
25 E-mail hash template id or html is missing
26 E-mail hash template id does not exist

JSON error response example

{
    "LOGON": "OK",
    "messages": [
        {
            "email": "user1@eurobate.com",
            "bulk_id": 65079,
            "uuid": "ac5d4a58-8cac-11f0-96ed-8e81fa05b8bd",
            "error": 0,
            "info": "Ok"
        },
        {
            "email": "user@eurobate.",
            "error": 23,
            "info": "Invalid E-mail"
        }
    ]
}

Testing

We have provided a couple of example clients if you would like to test this. If you already have a username and password from Eurobate for pushing SMS or E-mail you can use this. Otherwise contact us for a test account.

PHP example

<?php
define('USER','user1email');
define('PASS','pass1email');
define('ENDPOINT', 'https://api.eurobate.com/json_api_email.php');


 $to[] = array("to_email" => 'user1@eurobate.com',
               "email_var1" => "57PCHT9E4FSZ8XOJ"
              );        


 $to[] = array("to_email" => 'user2@eurobate.com',
               "email_var1" => "57PCHT966666666");      


 foreach($to as $t) {
        # EMAIL VARIABLES TO BE REPLACED BY REAL VALUES
        # F.EKS email sendt to OR senderid  
        $epost_var = ["[%email%]","[mail]","#EPOST#","#SENDERID#"];
        $epost_var_data   = [$t['to_email'],$t['to_email'],$t['to_email'],$t['email_var1']];      
        $messages[] = array("to_email"=>$t['to_email'], 
                            "to_name"=>'',                                                  
                            "email_var"=>$epost_var,
                            "email_var_data"=>$epost_var_data
                          );                        
 }

$hash = 'MY-EMAIL-TEMPLATE-HASH';
$html = null;
# GET HTML from file, hash then must be null
#$hash = null;      
#$html = file_get_contents('test.html');
$subject = "My Email Subject";
$from_name = "Eurobate";                            

send_email($hash,$html,$subject,$from_name,$messages);


function send_email($hash=null,$html=null,$subject,$from_name=null,$messages) {    

    $arr = array(
        'user' =>USER, 
        'password' => PASS, 
        'from_name'=> $from_name,
        'hash'=>$hash,
        "html"=>$html,
        'subject'=>$subject,
        'messages' =>$messages
        );
        $json = json_encode($arr,JSON_PRETTY_PRINT);        
        echo $json."\n";
        #exit();
        $curl = curl_init(ENDPOINT);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json; charset=utf-8',                                                                                
        'Content-Length: ' . strlen($json))                                                                       
        );    
        if (!$buffer = curl_exec($curl)) {
                $buffer = 'EMPTY BUFFER';
                $antall = 0;
                error_log($buffer);
        }    
        echo json_encode(json_decode($buffer),JSON_PRETTY_PRINT)."\n";
}

CURL example

curl -X POST \
  'https://api.eurobate.com/json_api_email.php' \
  -H 'Content-Type: application/json; charset=utf-8' \
  -d '{
    "user": "user1email",
    "password": "user1password",
    "from_name": "Eurobate",
    "hash": "MY-EMAIL-TEMPLATE-HASH",
    "html": null,
    "subject": "My Subject",
   "messages": [
        {
            "to_email": "user1@eurobate.com",
            "to_name": "",
            "email_var": [
                "[%email%]",
                "[mail]",
                "#EMAIL#"                
            ],
            "email_var_data": [
                "user1@eurobate.no",
                "user1@eurobate.no",
                "user1@eurobate.no"                
            ]
        },
        {
            "to_email": "user2@eurobate.com",
            "to_name": "",
            "email_var": [
                "[%email%]",
                "[mail]",
                "#EMAIL#"                
            ],
            "email_var_data": [
                "user2@eurobate.com",
                "user2@eurobate.com",
                "user2@eurobate.com"

            ]
        }
    ]
  }'

Python example


import json
import requests  # Using the 'requests' library for making HTTP requests

# --- Constants ---
# In Python, we typically use uppercase for constants.
USER = 'user1email'
PASS = 'pass1email'
ENDPOINT = 'https://api.eurobate.com/json_api_email.php'

# --- Data Structure for Recipients ---
# In Python, lists of dictionaries are the equivalent of PHP arrays of associative arrays.
recipients = [
    {
        "to_email": 'user1@eurobate.com',
        "email_var1": "57PCHT9E4FSZ8XOJ"
    },
    {
        "to_email": 'user2@eurobate.com',
        "email_var1": "57PCHT966666666"
    }
]

# --- Prepare Messages ---
messages = []
for recipient in recipients:
    # Define email variables to be replaced by real values
    # In Python, we can use lists directly for this.
    epost_vars = ["[email]", "[mail]", "#EPOST#", "#SENDERID#"]
    epost_vars_data = [
        recipient['to_email'],
        recipient['to_email'],
        recipient['to_email'],
        recipient['email_var1']
    ]

    messages.append({
        "to_email": recipient['to_email'],
        "to_name": '',  # Empty string as in the PHP
        "email_var": epost_vars,
        "email_var_data": epost_vars_data
    })

# --- Email Details ---
hash_value = 'MY-EMAIL-TEMPLATE-HASH'  
html_content = None     

subject = "My Email Subject"
from_name = "Eurobate"

# --- Function to Send Email ---
def send_email(hash_val=None, html=None, subject_line=None, from_name_sender=None, message_list=None):

    payload = {
        'user': USER,
        'password': PASS,
        'from_name': from_name_sender,
        'hash': hash_val,
        "html": html,
        'subject': subject_line,
        'messages': message_list
    }

    # Convert the payload to a JSON string with pretty printing
    json_payload = json.dumps(payload, indent=4)
    print("--- Sending Payload ---")
    print(json_payload)
    print("-----------------------")

    # Using the 'requests' library to make the POST request
    # It's generally preferred over manual curl handling in Python.
    headers = {
        'Content-Type': 'application/json; charset=utf-8',
        'Content-Length': str(len(json_payload))
    }

    try:
        # The 'requests' library handles the details of making the POST request,
        # including setting the Content-Type and Content-Length headers.
        # We set verify=False to mimic CURLOPT_SSL_VERIFYPEER, 0 and CURLOPT_SSL_VERIFYHOST, 0.
        # In production, you should ideally configure SSL verification properly.
        response = requests.post(ENDPOINT, data=json_payload, headers=headers, verify=False)

        # Check for HTTP errors
        response.raise_for_status()

        # Get the response content and decode it
        buffer = response.text

        # Decode the response JSON, pretty-print it, and print
        try:
            decoded_response = json.loads(buffer)
            print("--- API Response ---")
            print(json.dumps(decoded_response, indent=4))
            print("--------------------")
        except json.JSONDecodeError:
            print("--- API Response (Non-JSON) ---")
            print(buffer)
            print("-------------------------------")

    except requests.exceptions.RequestException as e:
        print(f"An error occurred during the request: {e}")
        # In Python, we often log errors or handle them differently.
        # For simplicity, we'll print the error.
        # The PHP script had: $buffer = 'EMPTY BUFFER'; $antall = 0; error_log($buffer);
        # We can simulate error logging if needed:
        # import logging
        # logging.error(f"EMPTY BUFFER: {e}")
        return None # Indicate failure

# --- Call the function ---
send_email(hash_value, html_content, subject, from_name, messages)

Eurobate AS - www.eurobate.no