mailjet / mailjet-apiv3-php

[API v3] Mailjet PHP Wrapper
http://dev.mailjet.com
MIT License
263 stars 86 forks source link

Invalid API response #239

Closed grandeljay closed 2 years ago

grandeljay commented 2 years ago

Every time I create a contact I get this response. I've tried about 50 different addresses and they are all successfully created. But I keep getting the response that it already exists. As if the API would create the contact first then check if it exists.

I am expecting a success response and not a failure response since I am adding fresh addresses which are definitely not duplicate.

My code: https://github.com/grandeljay/grandeljay-mailjet-for-wordpress/blob/e2291c920b2e9a356e4cbef733aad14ec03ce39c/inc/hooks/wp.php#L70-L89

Response ``` object(Mailjet\Response)#9734 (5) { ["status":"Mailjet\Response":private]=> int(400) ["success":"Mailjet\Response":private]=> bool(false) ["body":"Mailjet\Response":private]=> array(3) { ["ErrorInfo"]=> string(0) "" ["ErrorMessage"]=> string(83) "MJ18 A Contact resource with value "foo@domain.tld" for Email already exists." ["StatusCode"]=> int(400) } ["rawResponse":"Mailjet\Response":private]=> object(GuzzleHttp\Psr7\Response)#9731 (6) { ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=> string(83) "MJ18 A Contact resource with value "foo@domain.tld" for Email already exists." ["statusCode":"GuzzleHttp\Psr7\Response":private]=> int(400) ["headers":"GuzzleHttp\Psr7\Response":private]=> array(3) { ["date"]=> array(1) { [0]=> string(29) "Wed, 24 Nov 2021 12:43:18 GMT" } ["content-type"]=> array(1) { [0]=> string(16) "application/json" } ["content-length"]=> array(1) { [0]=> string(3) "146" } } ["headerNames":"GuzzleHttp\Psr7\Response":private]=> array(3) { ["date"]=> string(4) "date" ["content-type"]=> string(12) "content-type" ["content-length"]=> string(14) "content-length" } ["protocol":"GuzzleHttp\Psr7\Response":private]=> string(3) "1.1" ["stream":"GuzzleHttp\Psr7\Response":private]=> object(GuzzleHttp\Psr7\Stream)#9727 (7) { ["stream":"GuzzleHttp\Psr7\Stream":private]=> resource(1590) of type (stream) ["size":"GuzzleHttp\Psr7\Stream":private]=> int(146) ["seekable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["readable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["writable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["uri":"GuzzleHttp\Psr7\Stream":private]=> string(10) "php://temp" ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=> array(0) { } } } ["request":"Mailjet\Response":private]=> object(Mailjet\Request)#9581 (8) { ["method":"Mailjet\Request":private]=> string(4) "POST" ["url":"Mailjet\Request":private]=> string(39) "https://api.mailjet.com/v3/REST/contact" ["filters":"Mailjet\Request":private]=> array(0) { } ["body":"Mailjet\Request":private]=> array(1) { ["Email"]=> string(20) "foo@domain.tld" } ["auth":"Mailjet\Request":private]=> array(2) { [0]=> string(32) "714c6d66b3950c9878f2b25ba7ee5376" [1]=> string(32) "e1b2e5c3ff74b871feeaae5082853ea1" } ["type":"Mailjet\Request":private]=> string(16) "application/json" ["requestOptions":"Mailjet\Request":private]=> array(2) { ["timeout"]=> int(15) ["connect_timeout"]=> int(2) } ["guzzleClient":"Mailjet\Request":private]=> object(GuzzleHttp\Client)#9566 (1) { ["config":"GuzzleHttp\Client":private]=> array(9) { ["defaults"]=> array(1) { ["headers"]=> array(1) { ["user-agent"]=> string(30) "mailjet-apiv3-php/8.0.6/v2.0.0" } } ["handler"]=> object(GuzzleHttp\HandlerStack)#9558 (3) { ["handler":"GuzzleHttp\HandlerStack":private]=> object(Closure)#9560 (2) { ["static"]=> array(2) { ["default"]=> object(Closure)#9548 (2) { ["static"]=> array(2) { ["default"]=> object(GuzzleHttp\Handler\CurlMultiHandler)#9554 (6) { ["factory":"GuzzleHttp\Handler\CurlMultiHandler":private]=> object(GuzzleHttp\Handler\CurlFactory)#9565 (2) { ["handles":"GuzzleHttp\Handler\CurlFactory":private]=> array(0) { } ["maxHandles":"GuzzleHttp\Handler\CurlFactory":private]=> int(50) } ["selectTimeout":"GuzzleHttp\Handler\CurlMultiHandler":private]=> int(1) ["active":"GuzzleHttp\Handler\CurlMultiHandler":private]=> NULL ["handles":"GuzzleHttp\Handler\CurlMultiHandler":private]=> array(0) { } ["delays":"GuzzleHttp\Handler\CurlMultiHandler":private]=> array(0) { } ["options":"GuzzleHttp\Handler\CurlMultiHandler":private]=> array(0) { } } ["sync"]=> object(GuzzleHttp\Handler\CurlHandler)#9556 (1) { ["factory":"GuzzleHttp\Handler\CurlHandler":private]=> object(GuzzleHttp\Handler\CurlFactory)#9559 (2) { ["handles":"GuzzleHttp\Handler\CurlFactory":private]=> array(1) { [0]=> object(CurlHandle)#9730 (0) { } } ["maxHandles":"GuzzleHttp\Handler\CurlFactory":private]=> int(3) } } } ["parameter"]=> array(2) { ["$request"]=> string(10) "" ["$options"]=> string(10) "" } } ["streaming"]=> object(GuzzleHttp\Handler\StreamHandler)#9557 (1) { ["lastHeaders":"GuzzleHttp\Handler\StreamHandler":private]=> array(0) { } } } ["parameter"]=> array(2) { ["$request"]=> string(10) "" ["$options"]=> string(10) "" } } ["stack":"GuzzleHttp\HandlerStack":private]=> array(4) { [0]=> array(2) { [0]=> object(Closure)#9571 (2) { ["static"]=> array(1) { ["bodySummarizer"]=> NULL } ["parameter"]=> array(1) { ["$handler"]=> string(10) "" } } [1]=> string(11) "http_errors" } [1]=> array(2) { [0]=> object(Closure)#9575 (1) { ["parameter"]=> array(1) { ["$handler"]=> string(10) "" } } [1]=> string(15) "allow_redirects" } [2]=> array(2) { [0]=> object(Closure)#9562 (1) { ["parameter"]=> array(1) { ["$handler"]=> string(10) "" } } [1]=> string(7) "cookies" } [3]=> array(2) { [0]=> object(Closure)#9569 (1) { ["parameter"]=> array(1) { ["$handler"]=> string(10) "" } } [1]=> string(12) "prepare_body" } } ["cached":"GuzzleHttp\HandlerStack":private]=> object(Closure)#9724 (2) { ["static"]=> array(2) { ["handler"]=> object(GuzzleHttp\RedirectMiddleware)#9723 (1) { ["nextHandler":"GuzzleHttp\RedirectMiddleware":private]=> object(Closure)#9722 (2) { ["static"]=> array(1) { ["handler"]=> object(GuzzleHttp\PrepareBodyMiddleware)#9720 (1) { ["nextHandler":"GuzzleHttp\PrepareBodyMiddleware":private]=> object(Closure)#9560 (2) { ["static"]=> array(2) { ["default"]=> object(Closure)#9548 (2) { ["static"]=> array(2) { ["default"]=> object(GuzzleHttp\Handler\CurlMultiHandler)#9554 (6) { ["factory":"GuzzleHttp\Handler\CurlMultiHandler":private]=> object(GuzzleHttp\Handler\CurlFactory)#9565 (2) { ["handles":"GuzzleHttp\Handler\CurlFactory":private]=> array(0) { } ["maxHandles":"GuzzleHttp\Handler\CurlFactory":private]=> int(50) } ["selectTimeout":"GuzzleHttp\Handler\CurlMultiHandler":private]=> int(1) ["active":"GuzzleHttp\Handler\CurlMultiHandler":private]=> NULL ["handles":"GuzzleHttp\Handler\CurlMultiHandler":private]=> array(0) { } ["delays":"GuzzleHttp\Handler\CurlMultiHandler":private]=> array(0) { } ["options":"GuzzleHttp\Handler\CurlMultiHandler":private]=> array(0) { } } ["sync"]=> object(GuzzleHttp\Handler\CurlHandler)#9556 (1) { ["factory":"GuzzleHttp\Handler\CurlHandler":private]=> object(GuzzleHttp\Handler\CurlFactory)#9559 (2) { ["handles":"GuzzleHttp\Handler\CurlFactory":private]=> array(1) { [0]=> object(CurlHandle)#9730 (0) { } } ["maxHandles":"GuzzleHttp\Handler\CurlFactory":private]=> int(3) } } } ["parameter"]=> array(2) { ["$request"]=> string(10) "" ["$options"]=> string(10) "" } } ["streaming"]=> object(GuzzleHttp\Handler\StreamHandler)#9557 (1) { ["lastHeaders":"GuzzleHttp\Handler\StreamHandler":private]=> array(0) { } } } ["parameter"]=> array(2) { ["$request"]=> string(10) "" ["$options"]=> string(10) "" } } } } ["parameter"]=> array(2) { ["$request"]=> string(10) "" ["$options"]=> string(10) "" } } } ["bodySummarizer"]=> NULL } ["parameter"]=> array(2) { ["$request"]=> string(10) "" ["$options"]=> string(10) "" } } } ["allow_redirects"]=> array(5) { ["max"]=> int(5) ["protocols"]=> array(2) { [0]=> string(4) "http" [1]=> string(5) "https" } ["strict"]=> bool(false) ["referer"]=> bool(false) ["track_redirects"]=> bool(false) } ["http_errors"]=> bool(true) ["decode_content"]=> bool(true) ["verify"]=> bool(true) ["cookies"]=> bool(false) ["idn_conversion"]=> bool(false) ["headers"]=> array(1) { ["User-Agent"]=> string(12) "GuzzleHttp/7" } } } } } ```
Pavlico commented 2 years ago

Hi I quickly tested it myself and it works as expected, maybe your app triggers creation function 2 times?

grandeljay commented 2 years ago

Darn it. I can't figure out what I'm doing wrong then. I tried dumping the response and then die() to make sure it only runs once, but it's the same response.

Do you happen to know how a duplicate email is determined?

Because my email provider has catch all enabled, so all the email addresses I have tested have the same domain and are fake and just redirect to my main address, like such:

Email Redirects To
test@domain.com main@domain.com
test-02@domain.com main@domain.com
test-03@domain.com main@domain.com

and they are all added to mailjet but I always get the "contact already exists" response.

Pavlico commented 2 years ago

Well I dont know how it checks duplicates under the hood, mb you can test it out by just downloading repo, and then create php file like below and run it, and then test it with your domain mail? i m just loudly thinking mb that will help us determine where is the problem :)


require 'vendor/autoload.php';
$mailjet = new \Mailjet\Client('key', 'secret',true,['version' => 'v3']);
$contact_add_body = array(
    'Email' => 'test@test.test',
);

$contact_add = $mailjet->post(
    \Mailjet\Resources::$Contact,
    array(
        'body' => $contact_add_body,
    )
);
var_dump($contact_add->success());
var_dump($contact_add->getReasonPhrase());
grandeljay commented 2 years ago

Changing the email address solved the issue, seems like Mailjets duplicate mail check is broken.

Sorry for taking your time and thank you for helping!

Pavlico commented 2 years ago

we cool :)