Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs needed for new marketing campaign APIs #896

Open
interwap opened this issue Oct 18, 2019 · 11 comments
Open

Docs needed for new marketing campaign APIs #896

interwap opened this issue Oct 18, 2019 · 11 comments
Labels
difficulty: easy fix is easy in difficulty status: help wanted requesting help from the community type: docs update documentation change not affecting the code

Comments

@interwap
Copy link

interwap commented Oct 18, 2019

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
Copy link

dswmedia commented Dec 5, 2019

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

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

@interwap
Copy link
Author

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
Copy link

@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
Copy link

@interwap
Copy link
Author

@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
Copy link

TomAshe commented Mar 12, 2020

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

@interwap
Copy link
Author

@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

@childish-sambino childish-sambino changed the title Add recipient doesn't seem to work Docs needed for new marketing campaign APIs Mar 12, 2020
@childish-sambino childish-sambino added status: help wanted requesting help from the community type: docs update documentation change not affecting the code labels Mar 12, 2020
@childish-sambino
Copy link
Contributor

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

@childish-sambino childish-sambino added the difficulty: easy fix is easy in difficulty label Mar 12, 2020
@Preen
Copy link

Preen commented Oct 27, 2020

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

@emil-kirilov
Copy link

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
Copy link

RayHughes commented Sep 23, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: easy fix is easy in difficulty status: help wanted requesting help from the community type: docs update documentation change not affecting the code
Projects
None yet
Development

No branches or pull requests

7 participants