sendgrid / sendgrid-php

The Official Twilio SendGrid PHP API Library
https://sendgrid.com
MIT License
1.49k stars 624 forks source link

Docs needed for new marketing campaign APIs #896

Open interwap opened 4 years ago

interwap commented 4 years ago

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...

dswmedia commented 4 years ago

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

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

interwap commented 4 years ago

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

dswmedia commented 4 years ago

@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": "example@example.com", 
        "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 commented 4 years ago

@interwap

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

interwap commented 4 years ago

@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.

TomAshe commented 4 years ago

@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":"test@example.com"}]}');

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.

interwap commented 4 years ago

@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":"test@example.com"}]}');

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

childish-sambino commented 4 years ago

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

Preen commented 3 years ago

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

emil-kirilov commented 2 years ago

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!

RayHughes commented 2 years ago

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)
            ]);
    }