bento icon close

How do I insert data into a contact list with the Mailify API?

There are a variety of methods but we have chosen an example using an HTML form that will be used to send data to your contact list. Below is an example of a form with an email field to be filled in:

     <form action="" method="POST">       <input type="text" placeHolder="Email" name="email" />       <input type="text" placeHolder="Apellido" name="nom" />       <input type="text" placeHolder="Nombre" name="prenom" />       <input type="submit">   </form> 

Let's switch to data processing with the Mailify API.

To use the Mailify API, firstly you have to check that the "php_curl" extension be enabled in your PHP setup. (archivo php.ini)


Step 1 - Copy the code


As some specific data is similar each time the API is called, copy this PHP function at the beginning of the file so as to create your start string and connect to the API more easily.

<?php    function post_to_url($url, $data) {    $fields = '';    foreach($data as $key => $value) {       $fields .= $key . '=' . $value . '&';    }    rtrim($fields, '&');     $post = curl_init();    curl_setopt($post, CURLOPT_URL, $url);    curl_setopt($post, CURLOPT_POST, count($data));    curl_setopt($post, CURLOPT_POSTFIELDS, $fields);    curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);     $result = curl_exec($post);     curl_close($post); }  ?>

As you can see, this function needs 2 parameters to be called:

  • A URL ($url)
  • Data ($data)



Step 2 - Find the URL in Mailify and paste it


To connect to the API, we will use the URL from the contact list($url). Copy and paste the URL that appears under 'To insert a contact' in the API access window.


To find this URL, follow the steps in our guide here.


https://api-sd.sarbacane.com/lists/325/7CsS69R44UaSy7TazcRLMw?action=INSERT&Email=contact@domaine.com

We only need to use part of the URL so as to get one that looks like this:

https://api-sd.sarbacane.com/lists/325/7CsS69R44UaSy7TazcRLMw

(everything that comes after "?" was deleted)


Then we get:

<?php    $url = "https://api-sd.sarbacane.com/lists/325/7CsS69R44UaSy7TazcRLMw";   ?>

Step 3 - Send the data


We now have to send the data ($data) that we want to be added to that list:

$data is a chart with data that comes as follows:

<?php    $data=array(       'action' => 'INSERT',       'Email' => $email  );   ?>

/!\ In this example, $email has been previously set up with an email address.


The 'action' box is mandatory as it allows you to choose which of the three actions you want to perform with the data.

The 3 possible values are: 'INSERT', 'UPDATE', 'DELETE'.


The HTML form that is shown above only contains the 'Email' box, the $data table thus only has the 'Email' box.


/!\ The contact list only has the 'Email' column by default. If you decide to add columns to your list, you will also have to add them to your list and report them into the $data chart.



Example with more than one column


Example with a list that shows the email, first name, surname columns:

<?php    $data=array(       'action' => 'INSERT',       'Email' => $email,       'Prenom' => $prenom,       'Nom' => $nom  );   ?>

Once the values have been reported in the '$data' column, you just have to send it to the API with the following command.

<?php    post_to_url($url, $data);   ?> 

Your data has now been included in your contact list.
/!\ Mailify updates your contact list every 4 minutes. To force the list update, restart the software.



Summary of the PHP code file


<?php    function post_to_url($url, $data) {    $fields = '';    foreach($data as $key => $value) {       $fields .= $key . '=' . $value . '&';    }    rtrim($fields, '&');     $post = curl_init();    curl_setopt($post, CURLOPT_URL, $url);    curl_setopt($post, CURLOPT_POST, count($data));    curl_setopt($post, CURLOPT_POSTFIELDS, $fields);    curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);     $result = curl_exec($post);     curl_close($post); }  //We now have data to send:   $url = "https://api-sd.sarbacane.com/lists/325/7CsS69R44UaSy7TazcRLMw";   $email = $_POST["email"];  $data=array(       'action' => 'INSERT',       'Email' => $email   );  //Sending data   post_to_url($url, $data);   ?>

To connect to the API, you need to carry out the following steps:


Step 1 - Find the URL for the contact lists.

To find this URL, follow the steps in our guide here.


Step 2 - Copy the URL of the action which you want to take

(INSERT, UPDATE, DELETE)


Example


In our example, the action we will use will be "insert a contact"


https://api-sd.sarbacane.com/lists/325/7CsS69R44UaSy7TazcRLMw?action=INSERT&Email=email@yourdomain.com

This URL is composed of two parts:


1. → https://api-sd.sarbacane.com/lists/325/7CsS69R44UaSy7TazcRLMw which allows you to identify your list.

2. → ?action=INSERT&Email=email@yourdomain.com which represents the data sent in the URL.


What do these characters mean?


The chain of characters "action=INSERT" allows you to choose which action to carry out (In this case, the action is INSERT which means insert contacts to the list).

The character '&' allows you to separate different data sent with the URL.

Email=email@yourdomain.com corresponds to the column "Email" which will be filled in with the value "email@yourdomain.com"


/!\ If your list contains a lot of columns, Mailify will fill in the URL with all the different data.


Here is an example of a URL that contains a field for email, name and surname.


https://api-sd.sarbacane.com/lists/325/7CsS69R44UaSy7TazcRLMw?action=INSERT&Email=contact@domaine.com&NSurname=value&Name=value

Step 3 - Paste the URL


Once the URL has been obtained, you just need to replace the content of the URL variable with the URL containing your data. (The rest of your ASP code allows you to send the request, therefore you don´t need to edit it.)

<% url="https://api-sd.sarbacane.com/lists/325/7CsS69R44UaSy7TazcRLMw?action=INSERT&Email=contact@domain.com&Surname=value&Name=value" Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0") xmlhttp.open "GET", url, false xmlhttp.send data %>