Mailjet: adding email to the list does not work

I started using Mailjet to store a subscription using a form. The library I used for this task is https://github.com/mailjet/mailjet-apiv3-php-simple "

include("php-mailjet-v3-simple.class.php"); $apiKey = "xxx"; $secretKey = "yyy"; $mj = new Mailjet($apiKey, $secretKey); $contact_params = array("method" => "POST", "Email" => " abc@gmail.com "); $contact = $mj->contact($contact_params); $add_params = array( "method" => "POST", "ListID" => "11223344", "IsActive" => "True" ); $result = $mj->listrecipient($add_params); 

But this method does not add the letter to the Mailjet list. What have I done wrong here? Please help me.

+2
source share
1 answer

Edit:

See the changes in this answer for a fix if you are using a PHP version older than 5.4.

If possible, try updating :-)


First of all, thank you for your interest in Mailjet!

Now, before giving you the answer, please know that there is a guide for what you are asking here :-). <br> In addition, the README for Github repo for this library has an example section on contacts and contact lists .

Now that you know where to look first the next time you have problems with this library, let me get to the fix, right? -)

Correction

Your add_Params array just needs the ContactID field.
Here's how it should look:

 $add_params = [ "method" => "POST", "ListID" => [TheListID], "ContactID" => [TheContactID], "IsActive" => True ]; 

This should solve your problem.

Read if you want to know why.
In addition, the complete process of creating a contact and adding it to the new list described at the end.

"Why"

The listrecipient resource is a way to associate a contact resource with a contactslist resource.
This means that the API does not know what to do when you create a listrecipient resource without all the necessary parameters (more on this here ).

The whole process

Create contact and contactslist resources and add them to the latter. (I assume you have an instance of $mj of the Mailjet class.)


EDIT

Make sure that the contact you are trying to create has not yet been created.
See here for more details.


 $makeContactParams = [ "method" => "POST", "Email" => " testSO@example.org " ]; $contact = $mj->contact($makeContactParams); echo "Contact ID: ".$contact->Data[0]->ID."\n"; $contactslistParams = [ "method" => "POST", "Name" => "TestSO" ]; $list = $mj->contactslist($contactslistParams); echo "List ID: ".$list->Data[0]->ID."\n\n"; $listRecepParams = [ "method" => "POST", "ListID" => $list->Data[0]->ID, "ContactID" => $contact->Data[0]->ID, "IsActive" => True ]; $recep = $mj->listrecipient($listRecepParams); 

Hope this helps you solve your problem and understand why it was in the first place :-)

+3
source

Source: https://habr.com/ru/post/987729/


All Articles