You are here

function sendgrid_integration_get_bounces in SendGrid Integration 7

Get list of bounced emails. This is unused and may be come deprecated.

Parameters

string $start_date: Start date in format of YYYY-MM-DD.

string $end_date: End date in format of YYYY-MM-DD.

string $type: Optional type, either 'hard' or 'soft'.

string $email: Optional email address used to searching for.

Return value

array List of bounces.

File

./sendgrid_integration.module, line 132
Main module file for SendGrid Integration.

Code

function sendgrid_integration_get_bounces($start_date, $end_date, $type = NULL, $email = NULL) {

  // Connection url for bounces.
  // @TODO update to V3.
  $server = "https://sendgrid.com/api/bounces.get.json";
  $user = variable_get('sendgrid_integration_username', '');
  $key = variable_get('sendgrid_integration_apikey', '');
  if (isset($start_date) == FALSE || isset($end_date) == FALSE) {
    watchdog('sendgrid_integration', 'Error on calling get_bounches: Missing date parameter', [], WATCHDOG_NOTICE);
    return [];
  }
  $data = [
    'date' => 1,
    'start_date' => $start_date,
    'end_date' => $end_date,
    'api_user' => $user,
    'api_key' => $key,
  ];
  if (isset($type)) {
    $data['type'] = $type;
  }
  if (isset($email)) {
    $data['email'] = $email;
  }

  // Options for drupal_http_request.
  $options = [
    'method' => 'GET',
    'data' => http_build_query($data),
    'timeout' => 30,
    'headers' => [
      'Content-Type' => 'application/x-www-form-urlencoded',
    ],
  ];
  $response = drupal_http_request($server, $options);
  $list = drupal_json_decode($response->data);

  // Check if server returned error messages
  if (isset($list['error'])) {
    watchdog('sendgrid_integration ', 'Error while retrieving bounces list: %error', [
      '%error' => $list['error'],
    ], WATCHDOG_NOTICE);
  }
  return $list;
}