Mailjet: Adding email to list does not work

旧时模样 提交于 2019-12-11 03:55:00

问题


I have started to use Mailjet to store the subscription emails 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 email to the Mailjet list. What did I do wrong here? Please help me.


回答1:


Edit:

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

If possible, try to upgrade instead :-)


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're asking here :-) .
Also, the README for the Github repo for that library has an example section on contacts and contactslists.

Now that you know where to first look the next time you have issues with this library, let's get to the fix, shall we? ;-)

The Fix

Your add_Params array simply needs a ContactID field.
Here is what it should look like:

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

This should solve your issue.

Read on if you want to know why.
Also, full process of creating a contact and adding it to a new list described at the end.

The "Why"

The listrecipient resource is a way to link a contact resource to a contactslist resource.
Which means that the API doesn't know what to do when you create a listrecipient resource without all the necessary parameters (more info on that here).

The Whole Process

Let's create a contact and a contactslist resources and add the former to the latter.
(I assume that you have a $mj instance of the Mailjet class.)


EDIT

Make sure that the contact you're trying to create hasn't already been created.
See here for more infos.


$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);

I hope this helped you solve your problem and understand why it was there in the first place :-)



来源:https://stackoverflow.com/questions/30322925/mailjet-adding-email-to-list-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!