Crear cuenta Mailrelay Crear cuenta Mailrelay Crear cuenta Mailrelay

Sending newsletters with the Mailrelay API

Published by Mailrelay on April 5, 2012

In this article, we will see how to send a newsletter with the Mailrelay API. The first step is to read this article on our blog:
Example of accessing the API with Json

This article includes basic information on the use of the API, knowledge that will be necessary for this article. The next step will be to create a PHP file in which we will prepare the necessary code; we can name the file anything we want. The first thing that we will include in this file is the access to the API:

1
$hostname = 'tu-host.ip-zone.com';$apiKey = 'la-clave-de-acceso-al-api';

$hostname = 'tu-host.ip-zone.com';
$apiKey = 'la-clave-de-acceso-al-api';


The next thing we need is to know how to identify our “packageId” in order to add it to our code.

1
2
3
4
5
6
7
8
9
10
$curl = curl_init('http://' . $hostname . '/ccm/admin/api/version/2/&type=json');
$postData = array('function' => 'getPackages','apiKey' => $apiKey,);
curl_setopt($curl, CURLOPT_POST, true);curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($curl);$result = json_decode($json);
if ($result->status == 0) {throw new Exception('Hubo algún problema durante la ejecución');}
echo '
'; 
    var_dump($result->data);
    echo '
';

$curl = curl_init('http://' . $hostname . '/ccm/admin/api/version/2/&type=json');
$postData = array(
'function' => 'getPackages',
'apiKey' => $apiKey,
);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($curl);
$result = json_decode($json);
if ($result->status == 0) {
throw new Exception('Hubo algún problema durante la ejecución');
}
echo '
'; 
	var_dump($result->data);
	echo '
';


When we do this, we will get a screen that looks like the following. What we want to look for is the field ID number, highlighted in the image. We will take note of this number:
Enviar newsletters desde Mailrelay con el API
Sending newsletters from Mailrelay with the API

Another number that we will need is the “mailbox” identifier. This is much simpler to find; we can find it in the Mailrelay panel by going to “Settings” and then to “Manage senders.” There, we will see a table that looks like this:
Enviar newsletters desde Mailrelay con el API
Sending newsletters from Mailrelay with the API

We will take note of the “mailbox” ID that we want. Finally, we need the ID of the groups to which we want to send our newsletters, and we will go to the Mailrelay menu and then to “Subscriber Groups” where we will see the following table:
Enviar newsletters desde Mailrelay con el API
Sending newsletters from Mailrelay with the API

When we have the entire information ready, we can create another PHP file, or erase all the lines of the previous file, except the following:

1
$hostname = 'tu-host.ip-zone.com';$apiKey = 'la-clave-de-acceso-al-api';

$hostname = 'tu-host.ip-zone.com';
$apiKey = 'la-clave-de-acceso-al-api';


And the following:

1
2
3
4
5
6
7
8
9
10
11
$curl = curl_init('http://' . $hostname . '/ccm/ccm/admin/api/version/2/&type=json');
$postData = array('function' => 'addCampaign','apiKey' => $apiKey,'subject' => 'Mi newsletter enviada desde el API','mailboxFromId' => 1,'mailboxReplyId' => 1,'mailboxReportId' => 1,'emailReport' => true,'groups' => array(1,17),'text' => 'Nuevas promociones en Email Marketing','html' => 'Nuevas promociones en Email Marketing','packageId' => 6,'campaignFolderId' => 1,);
$post = http_build_query($postData);
curl_setopt($curl, CURLOPT_POST, true);curl_setopt($curl, CURLOPT_POSTFIELDS, $post);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($curl);$result = json_decode($json);
if ($result->status == 0) {throw new Exception('Bad status returned. Something went wrong.');}
echo '
'; 
    var_dump($result->data);
    echo '
';

$curl = curl_init('http://' . $hostname . '/ccm/ccm/admin/api/version/2/&type=json');
$postData = array(
'function' => 'addCampaign',
'apiKey' => $apiKey,
'subject' => 'Mi newsletter enviada desde el API',
'mailboxFromId' => 1,
'mailboxReplyId' => 1,
'mailboxReportId' => 1,
'emailReport' => true,
'groups' => array(
1,
17
),
'text' => 'Nuevas promociones en Email Marketing',
'html' => 'Nuevas promociones en Email Marketing',
'packageId' => 6,
'campaignFolderId' => 1,
);
$post = http_build_query($postData);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($curl);
$result = json_decode($json);
if ($result->status == 0) {
throw new Exception('Bad status returned. Something went wrong.');
}
echo '
'; 
	var_dump($result->data);
	echo '
';


By executing this code, we will get an image that looks like this:
Enviar newsletters desde Mailrelay con el API
Sending newsletters from Mailrelay with the API

This number is the identifier of the newsletter that we are creating through the API. We can also see it from the Mailrelay panel in the “Newsletter” and “Newsletter list” menus. Then we will click on “Show all” and we will see an image that looks like this:
Enviar newsletters desde Mailrelay con el API
Sending newsletters from Mailrelay with the API

Now, we can send the newsletter from the Mailrelay panel as we normally do, or from the API. To do this, we can create another PHP file with the following code:

1
2
3
4
5
6
7
$hostname = 'tu-host.ip-zone.com';$apiKey = 'la-clave-de-acceso-al-api';
$curl = curl_init('http://' . $hostname . '/ccm/admin/api/version/2/&type=json');
$postData = array('function' => 'sendCampaign','apiKey' => $apiKey,'id' => 28,);
curl_setopt($curl, CURLOPT_POST, true);curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($curl);$result = json_decode($json);
if ($result->status == 0) {throw new Exception('Bad status returned. Something went wrong.');}
var_dump($result->data);

$hostname = 'tu-host.ip-zone.com';
$apiKey = 'la-clave-de-acceso-al-api';
$curl = curl_init('http://' . $hostname . '/ccm/admin/api/version/2/&type=json');
$postData = array(
'function' => 'sendCampaign',
'apiKey' => $apiKey,
'id' => 28,
);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($curl);
$result = json_decode($json);
if ($result->status == 0) {
throw new Exception('Bad status returned. Something went wrong.');
}
var_dump($result->data);


As we can see, we only have to enter the ID of the newsletter that we want to send and execute this PHP code, which will return the following results:
Enviar newsletters desde Mailrelay con el API
Sending newsletters from Mailrelay with the API

Once we have sent the newsletter, we can access the “Sent messages” menu and submenu, where we will see a list of outgoing messages:
Enviar newsletters desde Mailrelay con el API
Sending newsletters from Mailrelay with the API

And of course, it will arrive in our subscribers’ inboxes:
Enviar newsletters desde Mailrelay con el API
Sending newsletters from Mailrelay with the API

This whole process can also be done automatically from the API- that of obtaining the groups, mailboxes, etc. But to simplify the example, we have done it manually. You can see more examples of the API on this page:
http://mailrelay.com/es/api
We hope this article has been useful to you!


El motivo principal por el que no vendes con tus emails es porque acaban en SPAM. Eso tiene solución, está en este ebook, lo puedes descargar registrándote, abajo







Acepto la política de privacidad.



Comments

Leave a comment

Your email address will not be published. Required fields are marked *


Menu Title