user6098475
user6098475

Reputation:

Sendgrid API Usage

I want to know email delivery status via sendgrid API in php script. I am not getting the exact function name which will return open, click, deliver status for a particular email id.

require 'vendor/autoload.php';
$sg = new \SendGrid($apiKey);
$response = $sg->client->FUNCTION_NAME->get();
var_dump( $response->body() );

Upvotes: 0

Views: 380

Answers (2)

M A SIDDIQUI
M A SIDDIQUI

Reputation: 2215

As per the sendgrid documentation https://sendgrid.com/docs/Glossary/opens.html It might be possible that you don't get accurate numbers.

$response = $sg->client->tracking_settings()->open()->get();
echo $response->statusCode();
echo $response->body();
print_r($response->headers());

// Reference url : https://github.com/sendgrid/sendgrid-php/blob/master/examples/trackingsettings/trackingsettings.php

Upvotes: 0

Thamaraiselvam
Thamaraiselvam

Reputation: 7080

You can use SendGrid's Webhooks to receive information on individual emails. https://sendgrid.com/docs/API_Reference/Webhooks/index.html

Another option is to use the Stats endpoints in SendGrid's WebAPI v3 to get those types of statistics about emails. Documentation is available at https://sendgrid.com/docs/API_Reference/Web_API_v3/Stats/index.html

For example:

<?php
// If you are using Composer
require 'vendor/autoload.php';
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
////////////////////////////////////////////////////
// Retrieve global email statistics #
// GET /stats #
$query_params = json_decode('{"aggregated_by": "day", "limit": 1, "start_date": "2016-01-01", "end_date": "2016-04-01", "offset": 1}');
$response = $sg->client->stats()->get(null, $query_params);
echo $response->statusCode();
echo $response->body();
print_r($response->headers());

Reference: https://github.com/sendgrid/sendgrid-php

Upvotes: 1

Related Questions