You are here

function support_mail in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 support.module \support_mail()

Implementation of hook_mail().

File

./support.module, line 1323
support.module

Code

function support_mail($key, &$message, $params) {
  $language = $message['language'];
  $variables = support_mail_tokens($params['account'], $language, $params['nid'], isset($params['comment']) ? $params['comment'] : new stdClass(), $params['suppress']);
  $message['subject'] .= _support_mail_text($key . '_subject', $language, $variables, $params['integrate_email']);
  $message['body'] = array(
    _support_mail_text($key . '_body', $language, $variables, $params['integrate_email']),
  );
  $node = node_load($params['nid']);
  if ($client = support_client_load($node->client)) {
    if ($client->integrate_email) {

      // Add a string allowing the support module to try and strip previously
      // quoted ticket notifications from emailed replies.
      // @@@ TODO This is probabaly not sane in D7.
      $array = explode("\n", $message['body'][0]);
      $length = sizeof($array);
      $md5 = md5($length);
      $message['body'][0] .= "\n\n[{$length}:{$md5}]\n";
    }
  }

  // We generate message ids based on comment cids and support nids.
  // This allows readers to properly thread the messages coming from us,
  // as well as allow us to detect what comment an incoming email is a reply to,
  // so we can (optionally) handle threaded comments.
  $cid = $params['cid'];
  $references = array();
  while ($cid) {
    $cid = db_query('SELECT pid FROM {comment} WHERE cid = :cid', array(
      ':cid' => $cid,
    ))
      ->fetchField();

    // The last one will be cid 0.
    $references[] = _support_generate_message_id($params['nid'], $cid);
  }

  // Set up message id and threading.
  if (!isset($message['headers']['Message-ID'])) {
    $message['headers']['Message-ID'] = _support_generate_message_id($params['nid'], $params['cid']);
  }
  if (!empty($references)) {
    $message['headers']['In-Reply-To'] = $references[0];
    $message['headers']['References'] = implode(' ', array_reverse($references));
  }
}