mailjet / mailjet-apiv3-php-simple

[API v3] Simple PHP wrapper for the Mailjet API /!\ [DEPRECATED - SEE README] /!\
https://dev.mailjet.com
MIT License
55 stars 45 forks source link

Can't add a contact to a list #2

Closed Colir closed 10 years ago

Colir commented 10 years ago

Hi, i try to add a contact to a list but without succes

here is my code

$params = array(
                'method'    => 'POST',
                'Email' =>  htmlentities($_POST['inputMail'])
            );      
            $result = $mj->contact($params);

            if(isset($result->StatusCode) && $result->StatusCode == '400')
                return false;

            $contact_id = $result->Data[0]->ID;

            // Add the contact to a contact list
            $params = array(
                'method'    => 'POST',
                'ContactID' => $contact_id,
                'ListID'    => $list_id
            );      
            $result = $mj->listrecipient($params);  

The problem is that on the first call, mailjet tell me that a contact with this email already exist.

So i can't assign him to a list I can't retrieve this contact ID

thanks for you help

CharlesCollas commented 10 years ago

Hi,

I guess the best way to handle this is to add a new case if the email already exists. Instead of returning false, just assume that if you have a 400 HTTP response code, it's because the email you POST already exists as a contact, so you get the contact who has this email with the following function :

function getContactByEmail($contactEmail) {
    $mj = new Mailjet();
    $params = array(
        "method" => "VIEW",
        "ID" => $contactEmail
    );
    $mj->contact($params);
}

(just a trick for this wrapper to make a request on https://api.mailjet.com/v3/REST/contact/youremail@test.com)

And then, you get the ID of the object returned by the previous function (the contact ID).

You now just have to add the contact with this ID to your list with the function addContactToList written in the README file.

WeshGuillaume commented 9 years ago

Hey @Colir :airplane:

We just released a new PHP API wrapper. It is now more PHP-ish (including namespace, PSR-0 compliance, and globally a better architecture). All it requires is PHP 5.4

In addition to our API Guides, we would be happy to help you get started with it.

Guillaume, from Mailjet!