You are here

function sendgrid_integration_drupal_mail_wrapper in SendGrid Integration 6

1 call to sendgrid_integration_drupal_mail_wrapper()
drupal_mail_wrapper in ./sendgrid_integration.module

File

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

Code

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