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

Sending a campain from website generated list (not working) #46

Closed webiummedia closed 9 years ago

webiummedia commented 9 years ago

Hi i created a newsletter registration form on this page for one of my clients. https://www.paindevie.org/index.php?mod=page&id=155

When the form is used, the email is added to a contact list (Pain de vie (Site Web)) in mailjet. When i try to send a campain to that contact list, it says the campaing was sucessful but no emails is sent out... When i try to download a CSV version of the contact list the CSV is empty. I don't understand ...

[removed]

I created a test list and imported a CSV of a few emails manualy from the mailjet account and tryed to send a mail campaing and it worked. But when i try the one generated by the website no mails are sent out.

[removed]

As you can see in this image it says that the last campain was 21 minutes ago ... but no emails where sent as you can see in the screen of my dashboard...

[removed]

What is really strange is that when i try to download the list in CSV, the file is empty ... So now i am wandering if i imported my contacts the right way ... This is my PHP code. Let me know if this is an importation issue and where is the error.

if ($_POST[newsletter_form]){
    if ($_POST[nom]==''){$err[] = 'Vous devez fournir un nom';}
    if ($_POST[courriel]==''){$err[] = 'Vous devez fournir un courriel';}
    if ($_POST[check1]==''){$err[] = 'Vous devez cocher la case pour vous abonner.';}
    if ($_POST[check2]==''){$err[] = 'Vous devez accepter les (Mentions Légales) et la (Politique de confidentialité) pour vous abonner.';}
    if ($_POST[captcha]!=$_SESSION['captcha']['code']){$err[] = 'Vous devez retaper le contenue de l\'image pour prouver que vous êtes bien humain.';}

    if (!$err){
        $contact = createContact($_POST[courriel], $_POST[nom]);
        $list = addContactToList($_POST[courriel], 1);
        $result = '1';
    }
}

if ($_POST[desabonement_form]){
    $contact = getcontact($_POST[courriel_d]);        
    $del = deletecontact(1, $contact[Data][0][ID]);
    $result = '2';
}

function createContact($Cemail, $Cname) {
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "Email" => $Cemail,
        "Name" => $Cname
    );

    $result = $mj->contact($params);
    $result = json_decode(json_encode($result),true);

    if ($mj->_response_code == 201)
       $result[status] = "1";
    else
       $result[status] = "0";

    return $result;
}

function addContactToList($email, $listID) {
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "ContactALT" => $email,
        "ListID" => $listID
    );

    $result = $mj->listrecipient($params);
    $result = json_decode(json_encode($result),true);

    if ($mj->_response_code == 201)
       $result[status] = "1";
    else
       $result[status] = "0";

    return $result;
}

function getcontact($email) {
    $mj = new Mailjet();
    $params = array(
        "method" => "VIEW",
        "ContactALT" => "$email",
        "ListID" => "1"
    );

    $result = $mj->listrecipient($params);
    $result = json_decode(json_encode($result),true);

    if ($mj->_response_code == 201)
       $result[status] == "1";
    else
       $result[status] == "0";

    return $result;
}

function deletecontact($listID, $contactID) {
    $mj = new Mailjet();
    $params = array(
        'method' => 'DELETE',
        'ID' => $contactID,
        'ListID' => $listID
    );

    $result = $mj->listrecipient($params);
    $result = json_decode(json_encode($result),true);

    if ($mj->_response_code == 201)
       $result[status] = "1";
    else
       $result[status] = "0";

    return $result;
}
arnaudbreton commented 9 years ago

Hi @webiummedia,

Thanks for reaching out.

First, could you please hide or remove the emails addresses shown in the first screenshot, for privacy reasons? Thanks!

Next, I think your addContactToList function is missing the IsActive field set to True. Otherwise, as indicated in our documentation, the value is False which leads to a full non active contacts list to which we can't send any email.

Hope it helps. Best.

webiummedia commented 9 years ago

I removed the screenshots. Thx i will try that right away.

webiummedia commented 9 years ago

It would of been helpful to have a visuel sign in the contact list to see that they where inactive. A grey dot for exemple and when you hover it, it would give a hint (Status inactive). That would of helped me out a lot. I have a question. Why do we need (Active | Inactive) when we have (Subscribe | Unsubscribe)?

arnaudbreton commented 9 years ago

@webiummedia, your feedback are fair and already taken in account by our product team for a coming release.

Why do we need (Active | Inactive) when we have (Subscribe | Unsubscribe)? Agreed it's a subtile difference in your case but it's needed in some cases. In a future release of our API, the IsActive value is set to true by default.

Is it working for you now? Thanks for your patience.

webiummedia commented 9 years ago

Thx Arnaud,

Not yet ... it seems my update is not working

function updateContactToList($email, $listID) {

    $mj = new Mailjet();

    // Try update if case creation fails
    $params = array(
        "method" => "UPDATE",
        "ContactALT" => $email,
        "ListID" => $listID,
        "IsActive" => true,
        "IsUnsubscribed" => false
    );

    $result = $mj->listrecipient($params);
    $result = json_decode(json_encode($result),true);

    return $result;
}

The contact does not turn active.

arnaudbreton commented 9 years ago

What is the return code of that call?

webiummedia commented 9 years ago

When i try to create the email i get [ErrorMessage] => MJ18 A Contact resource with value "***@gmail.com" for Email already exists.

When i try to add it to the list i get [ErrorMessage] => A duplicate ListRecipient already exists.

When i try to update it i get

Resource: listrecipient Request type: POST Get Arguments: (NONE)

Post Arguments ContactALT = ***@gmail.com ListID = 1 IsActive = 1 IsUnsubscribed =

Call url https://api.mailjet.com/v3/REST/listrecipient

But when i try to download the list of emails from the CSV the ***@gmail.com is not in the list.

arnaudbreton commented 9 years ago

To update you need to retrieve the ID of the ListRecipient you want to update.

To achieve that you can use the filters, as described on our documentation.

webiummedia commented 9 years ago

What is the diffrence between ContactID and ID? and if ID is not ContactID, where do i get it?

fturk commented 9 years ago

ID represent id of listrecipient, and ContactID represent id of contact

webiummedia commented 9 years ago

Why do we need to provide that for an update ... the ContactID or ContactALT should be all he needs considering the ContactID is supose to be unic in that list. Normaly you should be able to make a query without needing the ID ...

So how do i get the ID i need?

fturk commented 9 years ago

Tu n'arrive pas a faire un UPDATE d'un contact dans une liste ?

webiummedia commented 9 years ago

non ça marche pas ... J'essaye de changer les courriel de ma liste en "active" mais le changement ne s'opère pas ... Pourtant ça ne retourne pas d'erreur dans le dubug. Quand je vais dans ma liste sur le site de mailjet et que je veux télécharger le CSV des courriels de la liste il me retourne seulement que 3 courriel sur 170. (3 que j'ai ajouter moi même). Les 167 autres on été ajouter via un script mais j'ai pas spécifier que je voulais qu'il soit actif ... et la j'arrive plus a changer leur status. En plus je vois pas de place pour le faire depuis le site non plus.

<?php
include("../../init.php");

echo "<h1>START</h1>";

$num=0;

$SELECT = mysql_query("SELECT * FROM membre order by id desc limit 25,1");
while($m=mysql_fetch_array($SELECT)){

    $nom = ucfirst(strtolower($m[prenom]))." ".ucfirst(strtolower($m[nom]));
    $contact = createContact($m[pseudo], $nom);
    $list = addContactToList($m[pseudo], 1);

    echo "$nom ($m[pseudo]) added/updated to list<br />";   
    $num++;
}

echo "<h1>DONE ($num where added)</h1>";

function createContact($Cemail, $Cname) {
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "Email" => $Cemail,
        "Name" => $Cname
    );

    $result = $mj->contact($params);
    $result = json_decode(json_encode($result),true);

    if ($mj->_response_code == 201)
       $result[status] = "1";
    else
       $result[status] = "0";

    return $result;
}

function addContactToList($email, $listID) {
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "ContactALT" => $email,
        "ListID" => $listID,
        "IsActive" => true,
        "IsUnsubscribed" => false
    );

    $result = $mj->listrecipient($params);
    $result[INSERT] = json_decode(json_encode($result),true);
    $result[UPDATE] = updateContactToList($email, $listID);;

    return $result;
}

    function updateContactToList($email, $listID) {

        $mj = new Mailjet();

        // Try update if case creation fails
        $params = array(
            "method" => "UPDATE",
            "ContactALT" => $email,
            "ListID" => $listID,
            "IsActive" => true,
            "IsUnsubscribed" => false
        );

        $result = $mj->listrecipient($params);
        $result = json_decode(json_encode($result),true);

        return $result;
    }

function getcontact($email) {
    $mj = new Mailjet();
    $params = array(
        "method" => "VIEW",
        "ContactALT" => "$email",
        "ListID" => "1"
    );

    $result = $mj->listrecipient($params);
    $result = json_decode(json_encode($result),true);

    if ($mj->_response_code == 201)
       $result[status] == "1";
    else
       $result[status] == "0";

    return $result;
}

function deletecontact($listID, $contactID) {
    $mj = new Mailjet();
    $params = array(
        'method' => 'DELETE',
        'ID' => $contactID,
        'ListID' => $listID
    );

    $result = $mj->listrecipient($params);
    $result = json_decode(json_encode($result),true);

    if ($mj->_response_code == 201)
       $result[status] = "1";
    else
       $result[status] = "0";

    return $result;
}
?>
fturk commented 9 years ago

tu veut les mettres en actif les 167 autres ?

webiummedia commented 9 years ago

Oui donc j'essaye de faire un update sur chaque courriel

function addContactToList($email, $listID) {
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "ContactALT" => $email,
        "ListID" => $listID,
        "IsActive" => true,
        "IsUnsubscribed" => false
    );

    $result = $mj->listrecipient($params);
    $result[INSERT] = json_decode(json_encode($result),true);
    $result[UPDATE] = updateContactToList($email, $listID);

    return $result;
}

    function updateContactToList($email, $listID) {

        $mj = new Mailjet();

        // Try update if case creation fails
        $params = array(
            "method" => "UPDATE",
            "ContactALT" => $email,
            "ListID" => $listID,
            "IsActive" => true,
            "IsUnsubscribed" => false
        );

        $result = $mj->listrecipient($params);
        $result = json_decode(json_encode($result),true);

        return $result;
    }
fturk commented 9 years ago

Deja pour faire un UPDATE, il faut utiliser la methode PUT

webiummedia commented 9 years ago

capture6

Ya pas PUT dans les methode

fturk commented 9 years ago

Il faut bien lire regarde ce que j'ai encadrer, sans titre

fturk commented 9 years ago

Essaie avec PUT ça va aller beaucoup mieux :D

webiummedia commented 9 years ago

wow je l'ai complètement manquer celui la ... a leur place je metrais la methode a utiliser dans le titre ... c'est pas évidant lol Bon j'ai changer mais j'ai toujours le même problème le contact n'est pas dans la liste du CSV

Quand j'ouvre la liste de contact et que je clique sur le contact que je veux rendre actif j'ai remarquer que ça dit qu'il fait partie d'aucune liste .... pourtant il est bien dedans

capture7

fturk commented 9 years ago

execute la function listrecipient() et dit moi si tu arrive a voir s'il est inactif ou pas ?

fturk commented 9 years ago

essaie ça:

$params = array( "method" => "GET", "ContactsList" => $listID, "Active" => 0
); var_dump($mj->listrecipient($params));

Si tu fais ça tu récupere la liste des contacts non actif d'une liste

fturk commented 9 years ago

Tiens execute ça en n'oubliant pas de remplacer $listID:

    $params = array(
        "method" => "GET",
        "ContactsList" => $listID,
        "Active" => 0            
    );
    $a = $mj->listrecipient($params)->Data;

    foreach($a as $value){
        $params = array(
            "method" => "PUT",
            "ID" => $value->ID,
            "IsActive" => true
        );
        $mj->listrecipient($params);
    }

Tu me dis si ça marche ?

webiummedia commented 9 years ago

parfait je test toute suite merci

webiummedia commented 9 years ago

non toujours pas ... je comprend pas pourquoi ça ne fonctionne pas ... En fin de compte j'ai supprimer la liste puis importer manuelement chaque utilisateur via l'importantion de CSV

J'ai fait ce script pour me generer la liste.

echo "email;name<br />"; 
$SELECT = mysql_query("SELECT * FROM membre");
while($m=mysql_fetch_array($SELECT)){
    $nom = ucfirst(strtolower($m[prenom]))." ".ucfirst(strtolower($m[nom]));
    echo "$m[pseudo];$nom<br />";   
}

Quand je télécharge le CSV tout les mail y sont ... Je pense que le bug est du côté de mailjet. Plusieurs des essay que j'ai fait aurais du marcher incluant la dernière exemple que tu m'a donner.

-- BUG DESCRIPTION --

Pour reproduire: Créé un nouveau contact puis l'inséré dans une liste sans spécifier qu'il est actif et ensuite essayer de le metre actif. Tout ce que j'ai essayé n'a pas marché. J'espère ne pas rencontrer d'autre problème du genre, j'ai investie trop de temps déjà pour recommencer avec un autre API.

function createContact($Cemail, $Cname) {
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "Email" => $Cemail,
        "Name" => $Cname
    );

    $result = $mj->contact($params);   
    return $result;
}

function addContactToList($email, $listID) {
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "ContactALT" => $email,
        "ListID" => $listID
    );

    $result = $mj->listrecipient($params);
    return $result;
}

createContact("testing@testing.com", "mailjet test");
addContactToList("testing@testing.com", 1);

Une fois le teste exécuté, vous ne pourrez pas télécharger le CSV car aucun utilisateur n'est actif. Aucun indice visuel dans la liste qui permet de savoir que 'testing@testing.com' n'est pas actif ... Tout les tentatives que j'ai fait par la suite pour mettre l'utilisateur actif dans la list a échouer. Aucune méthode pour le faire via le site...

Solution temporaire: importer les mêmes courriel par CSV via le site ce qui update ensuite leur status.

fturk commented 9 years ago

Demain je te donnerai les fonctions pour résoudre ton problème.

PS: mon skype est pokipok73

fturk commented 9 years ago

Salut @webiummedia, alors tu as résolé ton problème ?

WeshGuillaume commented 9 years ago

Hey @webiummedia :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!