You are here

sendgrid_integration.module in SendGrid Integration 6

Main module file for SendGrid Integration.

Provides module configuration and help functionality.

File

sendgrid_integration.module
View source
<?php

/**
 * @file
 * Main module file for SendGrid Integration.
 *
 * Provides module configuration and help functionality.
 */

/**
 * Implements hook_menu().
 */
function sendgrid_integration_menu() {
  $items = array();
  $items['admin/settings/sendgrid'] = array(
    'title' => 'SendGrid settings',
    'description' => 'SendGrid Integration settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'sendgrid_integration_admin',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer sendgrid settings',
    ),
  );
  return $items;
}

/**
 * Implements hook_permissions().
 */
function sendgrid_integration_permission() {
  return array(
    'administer sendgrid settings',
  );
}

/**
 * Provides Settings Form.
 */
function sendgrid_integration_admin() {
  $form = array();
  $form['authentication'] = array(
    '#type' => 'fieldset',
    '#title' => t('Authentication'),
  );
  $form['authentication']['sendgrid_integration_username'] = array(
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#description' => t('Your SendGrid Username'),
    '#required' => TRUE,
    '#default_value' => variable_get('sendgrid_integration_username', ''),
  );
  $form['authentication']['sendgrid_integration_apikey'] = array(
    '#type' => 'password',
    '#title' => t('API Key'),
    '#description' => t('Your SendGrid Password'),
    '#required' => TRUE,
    '#default_value' => variable_get('sendgrid_integration_apikey', ''),
  );
  $form['security'] = array(
    '#type' => 'fieldset',
    '#title' => t('Security'),
  );
  $form['security']['sendgrid_integration_ssl'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use secure (SSL) connection?'),
    '#default_value' => variable_get('sendgrid_integration_ssl', TRUE),
  );
  return system_settings_form($form);
}
function sendgrid_integration_drupal_mail_wrapper($message) {

  // SendGrid authentication information.
  if (variable_get('sendgrid_integration_ssl', TRUE) == TRUE) {
    $protocol = 'https://';
  }
  else {
    $protocol = 'http://';
  }
  $server = $protocol . 'api.sendgrid.com/api/mail.send.json';

  // @todo This should use API keys.
  $user = variable_get('sendgrid_integration_username', '');
  $key = variable_get('sendgrid_integration_apikey', '');

  // Defining default unique args.
  $unique_args = array(
    'id' => $message['id'],
  );
  if (!empty($message['params']['account']->uid)) {
    $unique_args['uid'] = $message['params']['account']->uid;
  }
  if (!empty($message['module'])) {
    $unique_args['module'] = $message['module'];
  }

  // Allow other modules to modify unique arguments.
  // @todo: invoking removed from d6 version.
  // Check if we got any variable back
  if (!empty($args)) {
    $unique_args = $args;
  }

  // SMTP API, we use these to identify messages.
  $smtp_api = array(
    'category' => variable_get('site_name', 'Drupal'),
    'unique_args' => $unique_args,
  );

  // Data sended to sendgrid.
  $data = array(
    'api_user' => $user,
    'api_key' => $key,
    'x-smtpapi' => sendgrid_json_encode($smtp_api),
    'to' => $message['to'],
    'subject' => $message['subject'],
  );

  // Checking if 'from' email-address already exist.
  if (isset($message['from'])) {
    $data['from'] = $message['from'];
  }
  else {
    $data['from'] = variable_get('site_mail');
    $data['fromname'] = variable_get('site_name');
  }

  // Check Content-Type of message body.
  if (strpos($message['headers']['Content-Type'], 'text/plain') !== FALSE) {

    // Message body is text/plain.
    $data['text'] = drupal_wrap_mail(drupal_html_to_text($message['body']));
  }
  else {
    $data['html'] = $message['body'];
  }

  // Headers.
  $headers = array(
    'Content-Type' => 'application/x-www-form-urlencoded',
  );
  $result = drupal_http_request($server, $headers, 'POST', http_build_query($data), $retry = 3, $timeout = 20.0);

  // Checking if we got any result data
  // (connection didn't time out for example).
  $result_data = array();
  if (isset($result->data)) {
    $result_data = json_decode($result->data, TRUE);
  }

  // Check if sending message succeeded.
  if (isset($result_data['message'])) {
    if ($result_data['message'] == 'success') {
      return TRUE;
    }
  }

  // Default response to message sending failed.
  // Create log entry.
  $errors = '';
  if (isset($result_data['errors'])) {
    foreach ($result_data['errors'] as $error) {
      $errors .= $error . ' ';
    }
  }
  $variables = array(
    '%code' => $result->code,
    '%msg' => $result->error,
    '%errors' => $errors,
  );
  watchdog('SendGrid Integration', 'Email sending failed with %code/%msg. %errors', $variables, WATCHDOG_ERROR, $link = NULL);
  return FALSE;
}
function drupal_mail_wrapper($message) {
  return sendgrid_integration_drupal_mail_wrapper($message);
}

/**
 * Helper function for json encoding
 * @param $var
 * @return string json encoded string
 */
function sendgrid_json_encode($var) {

  // The PHP version cannot change within a request.
  static $php530;
  if (!isset($php530)) {
    $php530 = version_compare(PHP_VERSION, '5.3.0', '>=');
  }
  if ($php530) {

    // Encode <, >, ', &, and " using the json_encode() options parameter.
    return json_encode($var, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
  }

  // json_encode() escapes <, >, ', &, and " using its options parameter, but
  // does not support this parameter prior to PHP 5.3.0.  Use a helper instead.
  include_once drupal_get_path('module', 'sendgrid_integration') . '/json-encode.inc';
  return sendgrid_json_encode_helper($var);
}

Functions

Namesort descending Description
drupal_mail_wrapper
sendgrid_integration_admin Provides Settings Form.
sendgrid_integration_drupal_mail_wrapper
sendgrid_integration_menu Implements hook_menu().
sendgrid_integration_permission Implements hook_permissions().
sendgrid_json_encode Helper function for json encoding