sendgrid-php icon indicating copy to clipboard operation
sendgrid-php copied to clipboard

Docs needed for new marketing campaign APIs

Open interwap opened this issue 6 years ago • 11 comments

I'm trying to add a recipient(s) to a list using the PHP SDK but it doesn't seem to work. I get hit with the unauthorized access error (403). Funny thing is I'm even on a paid plan, changed API keys but all to no avail. Sending emails works fine but I can't get a subscription to work.

I contacted support with my issue and I was told they don't do that kind of diagnosis and will have to leave a message here on git. smh...

interwap avatar Oct 18 '19 09:10 interwap

We have the same issue here. The other APIs are working fine.

We get this response: {"errors":[{"field":null,"message":"access forbidden"}]}

dswmedia avatar Dec 05 '19 09:12 dswmedia

Hi @dswmedia apparently, using the SDK's subscribe method doesn't work right out the box for version 2. You may have to use the rest API. I, however, had to pay to get the legacy contacts activated on my account then subscription started working. Contact support to activate legacy marketing

interwap avatar Dec 13 '19 17:12 interwap

@interwap

I have solved the issue in another way. I have contacted support they told me to use the new api.

I have used something like this:

$sg = new \SendGrid("SG.XXXX");
$request_body = json_decode('[
    {
        "age": 25, 
        "email": "[email protected]", 
        "first_name": "", 
        "last_name": "User"
    }
]');

try {
    $response = $sg->client->contactdb()->recipients()->post($request_body);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

dswmedia avatar Dec 17 '19 10:12 dswmedia

@interwap

Sorry, this should be it. https://sendgrid.api-docs.io/v3.0/contacts/add-or-update-a-contact

dswmedia avatar Dec 18 '19 14:12 dswmedia

@dswmedia yes, this is via the API... but not using the Sendgrid SDK. I'd imagine that the point of even using the SDK at all is so we don't get to write curl requests ourselves.

interwap avatar Dec 18 '19 21:12 interwap

@interwap I just encountered the same issue, but was able to resolve it using the SDK with the following:

$sg = new \SendGrid("SG.XXXX");
$request_body = json_decode('{"contacts":[{"email":"[email protected]"}]}');

try {
    $response = $sg->client->marketing()->contacts()->put($request_body);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

It seems that the SDK examples needs to be updated with the new marketing resources vs. the legacy ones.

TomAshe avatar Mar 12 '20 07:03 TomAshe

@interwap I just encountered the same issue, but was able to resolve it using the SDK with the following:

$sg = new \SendGrid("SG.XXXX"); $request_body = json_decode('{"contacts":[{"email":"[email protected]"}]}');

try { $response = $sg->client->marketing()->contacts()->put($request_body); print $response->statusCode() . "\n"; print_r($response->headers()); print $response->body() . "\n"; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; }

It seems that the SDK examples needs to be updated with the new marketing resources vs. the legacy ones.

Thanks, will test that out

interwap avatar Mar 12 '20 08:03 interwap

I'll review a PR to update the docs if anyone wants to create it.

childish-sambino avatar Mar 12 '20 15:03 childish-sambino

bah.. just spent an hour to fix the legacy error...

Preen avatar Oct 27 '20 09:10 Preen

Yeah, new docs need to be created. I just lost an hour trying to figure out why postman works and the library does not. I would suggest SendGrid add a disclaimer line and a link to this issue in the docs. That would save a lot of time!

emil-kirilov avatar Sep 21 '21 10:09 emil-kirilov

I figured out adding pretty easily. However deleting was much more of a pain than it needed to be. Add a little defensive programing to this and you should be golden.

This library has been frustrating to work with.

private SendGrid $emailProvider;

Add

    public function addContacts(array $users): void
    {
        $contacts = array_map(
            function (array $user): array
            {
                return [
                    'email' => $user['email'],
                    'first_name' => $user['firstName'],
                    'last_name' => $user['lastName'],
                    "unique_name" => $user['username'],
                ];
            },
            $users
        );

        $this->emailProvider->client->marketing()
            ->contacts()
            ->put(['contacts' => $contacts]);
    }

Delete

    /**
     * @psalm-suppress UndefinedMagicMethod
     */
    public function removeContacts(array $emails): void
    {
        $request = ['emails' => $emails];

        /** @var string $response */
        $response = $this->emailProvider->client->marketing()
            ->contacts()
            ->search()
            ->emails() //magic method
            ->post($request)
            ->body();

        $results = json_decode($response, true);

        $emailIds = [];

        foreach ($results['result'] as $contact) {
            if (isset($contact['contact'])) {
                $emailIds[] = $contact['contact']['id'];
            }
        }

        $this->emailProvider->client->marketing()
            ->contacts()
            ->delete(null, [
                'ids' => implode(',', $emailIds)
            ]);
    }

RayHughes avatar Sep 23 '21 00:09 RayHughes