6

I have used the Mailjet api to store submitted emails in a Mailjet list. This worked properly when there was the Mailjet 0.1 version API (then there wasn't any PHP wrapper but was easy to use with their examples), but when they changed the API to version 3 their PHP wrapper does not return any contact ID when adding a new contact to a contact list. I have raised a similar question earlier how to get Mailjet to work but now this issue arose with the new version 3.

Again here is the corrected code,

*Assume that the shown email is a new contact that's not already created in Mailjet.

$mj = new Mailjet();
$contact_params = array("method" => "POST", "Email" => "abc@gmail.com");
$contact = $mj->contact($contact_params);

$add_params = array(
    "method" => "POST",
    "ContactID" => $contact,
    "ListID" => _MAILJET_LIST,
    "IsActive" => "True"
);
$mj->listrecipient($add_params);

Developer @Gormador said to use $contact->Data[0]->ID as contact id but it still returns NULL.

I get this error when trying to add a created contact to a list (with debug mode set to 2).

array(3) { ["ContactID"]=> NULL ["ListID"]=> string(1) "2"
["IsActive"]=> string(4) "True" } string(105) "{ "ErrorInfo" : "",
"ErrorMessage" : "Invalid data: \"null\" is an invalid integer",
"StatusCode" : 400 }"

Previous question: Mailjet: Adding email to list does not work

Updated: Full code with corrections,

$mj = new Mailjet(); 
$add_email = "testing" . rand(0, 1000) . "@gmail.com"; 
$contact_params = array("method" => "POST", "Email" => $add_email); 
$mj->contact($contact_params); 
$viewContact = array("method" => "VIEW","unique" => $add_email); 
$contact = $mj->contact($viewContact); 
$add_params = array( "method" => "POST", "ContactID" => $contact->Data[0]->ID, "ListID" => 1, "IsActive" => "True" ); 
$mj->listrecipient($add_params);
Community
  • 1
  • 1
Teshan N.
  • 2,307
  • 3
  • 30
  • 59
  • Sorry, I really can't reproduce on my side... This updated code works fine for me. Could you update with the full output? And notify me through a comment, otherwise I will miss it :-) – Gormador May 26 '15 at 14:49

2 Answers2

3

EDIT:

This issue isn't necessarily specific to the goal you try to achieve.
Although, there definitely is a bug in our 1.0.6 wrapper. In the wrapper code, we use the 4th parameter of the json_decode() function, which was only introduced in PHP 5.4 (See the "Changelog" section here).

Two ways you can solve this:

  • Either update your system's PHP version to >= 5.4
  • Or (until we introduce a fix on our side), modify the wrapper locally to not use that option (see here), which very well might bring new issues and isn't the recommended fix.

Hope this helped. We certainly are grateful that this brought that particular issue to our attention :)

Edit2:

To clarify, here's what the second approach means in term of code:

$this->_response = json_decode($buffer, false, 512, JSON_BIGINT_AS_STRING);
//Becomes
$this->_response = json_decode($buffer, false, 512);

That should eliminate the warning and solve the issue. Isn't that great?! ;-)


As I really am not able to reproduce your issue on my end, here is a question:
Is the contact you are trying to create listed in the response when you issue the following code ?
Alternatively, go here for a list of your account's contacts.

$mj = new Mailjet($apiKey, $secretKey);
$contact = $mj->contact();

var_dump($contact);

If it is, this is why what I wrote in my response on your other question doesn't work.

But why?

Well, you are trying to create a contact that is already listed in your account. The process of creating a contact doesn't necessarily mean that it has to be assigned to a contacts list.

The fix

Either get the existing contact's id instead of creating it before adding it to a list:

$mj = new Mailjet($apiKey, $secretKey);

$viewContact = [
    "method"    =>  "VIEW",
    "unique"    =>  "gaetan.philippot@gmail.com"
];

$contact = $mj->contact($viewContact);

echo "Contact ID: ".$contact->Data[0]->ID."\n";

// Then proceed as described in my original answer.

Or delete it through the web interface and try following my answer again.
(Click on the contact's email, then click on the yellow menu next to its avatar and click on remove.)

Does that solve your issue? :-)

Community
  • 1
  • 1
Gormador
  • 380
  • 2
  • 15
  • I always used a unique email (auto generated) to test the code such as "abc".rand(1,1000)."@gmail.com". The contact is recorded in Mailjet without any issue and showed the email in the response. – Teshan N. May 22 '15 at 11:11
  • $mj = new Mailjet(); $add_email = "teshan" . rand(0, 1000) . "@gmail.com"; $contact_params = array("method" => "POST", "Email" => $add_email); $mj->contact($contact_params); $viewContact = array("method" => "VIEW","unique" => $add_email); $contact = $mj->contact($viewContact); $add_params = array( "method" => "POST", "ContactID" => $contact->Data[0]->ID, "ListID" => _MAILJET_LIST, "IsActive" => "True" ); $mj->listrecipient($add_params); – Teshan N. May 22 '15 at 11:18
  • Could you post the whole response when using that code? The code in you question won't work, hence why the response doesn't really help me debugging your issue. Also, please issue a `var_dump` on each call (contact creation result, contact view result, contact to list association result). Thanks in advance! Also, please use code blocks as it makes everything much simpler to read :-) If need be, simply edit your question with an update. – Gormador May 22 '15 at 13:56
  • This is the test code I used. Please go through it and let me know how to extract the contact ID from that response. It cannot be retrieved by json as it does not return a value even after parsing through json_decode() or as an array. https://www.certificatestreet.com/mailjet_test.php – Teshan N. May 27 '15 at 07:20
  • Which version of php are you using? `php -v` – Gormador May 27 '15 at 14:53
  • There, answer edited. Sorry it took a bit longer than expected. – Gormador May 28 '15 at 08:17
  • Shall I just comment the highlighted line out? – Teshan N. May 28 '15 at 09:25
  • I tried commenting it but it only does is not showing the error. I cannot retrieve the created contact's id using "$contact->Data[0]->ID" which always returns NULL when I var dump. – Teshan N. May 28 '15 at 09:55
  • 1
    Added a new edit to detail the process. Look at "Edit2". Also, don't forget to upvote / accept comments and/or answers that have been helpful. Also, if this fix solves your problem, I will edit my answer to your first question so don't forget to check there as well to confirm the answer fixed your problem :-) – Gormador May 28 '15 at 10:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79006/discussion-between-gormador-and-techytee). – Gormador May 28 '15 at 12:23
0
 $email     = 'youname@yourcompany.com'
 $mj        = new \Mailjet\Client(MAIL_LOGIN, API_KEY);
 $response  = $mj->get(\Mailjet\Resources::$Contact, ['id' => $email]);
 $contactID = $response->success() ? $response->getData()[0]['ID'] : null;
francoisV
  • 79
  • 3
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Jan 04 '22 at 10:25