You are here

function casetracker_mail_send in Case Tracker 5

Sends out emails. Woot! Do people still say woot? Man, I'm old.

Parameters

$node: The case $node object that is being inserted or modified.

$comment: The $comment array, passed only if this a comment has been left.

2 calls to casetracker_mail_send()
casetracker_mail_comment in ./casetracker_mail.module
Implementation of hook_comment().
casetracker_mail_nodeapi in ./casetracker_mail.module
Implementation of hook_nodeapi().

File

./casetracker_mail.module, line 47
Enables mail sending and Mailhandler integration for Case Tracker.

Code

function casetracker_mail_send($case, $comment = NULL) {
  global $user;

  // is it a comment post
  $isComment = !is_null($comment);

  // get the project data
  $pid = $isComment ? $comment['prid'] : $case->pid;
  $project = db_fetch_object(db_query("SELECT cp.project_number, n.title FROM {casetracker_project} cp LEFT JOIN {node} n ON (n.vid = cp.vid) WHERE cp.nid = %d", $pid));

  // get the assigned to name
  $assignToId = $isComment ? $comment['assign_to'] : $case->assign_to;
  $assignToName = is_numeric($assignToId) ? casetracker_get_name($assignToId) : $assignToId;
  $variables = array(
    '%project_id' => $pid,
    '%project_number' => $project->project_number,
    '%project_title' => $project->title,
    '%case_id' => $case->nid,
    '%case_number' => $project->project_number . '-' . $case->case_number,
    '%case_title' => $isComment ? $comment['case_title'] : $case->title,
    '%case_type' => casetracker_case_state_load('type', $isComment ? $comment['case_type_id'] : $case->case_type_id),
    '%case_priority' => casetracker_case_state_load('priority', $isComment ? $comment['case_priority_id'] : $case->case_priority_id),
    '%case_status' => casetracker_case_state_load('status', $isComment ? $comment['case_status_id'] : $case->case_status_id),
    '%case_assigned' => $assignToName,
    '%case_author' => casetracker_get_name($case->uid),
    '%case_created' => format_date($case->created, 'large'),
    '%case_changed' => format_date($case->changed, 'large'),
    '%case_url' => url('node/' . $case->nid, NULL, NULL, TRUE),
    // @todo fails for CCK or non-body cases.
    '%case_description' => _casetracker_mail_plain_description($case->body),
    '%comment' => NULL,
  );
  if (isset($comment)) {

    // make a master %comment variable that contains the values of this specific comment.
    // @todo it'd be nice if we could display all the previous comments, as the project.module currently does.
    $variables['%comment'] = strtr(variable_get('casetracker_mail_comment_message', _casetracker_mail_comment_message()), array(
      '%comment_author' => casetracker_get_name($comment['uid']),
      '%comment_created' => format_date($comment['date'], 'large'),
      '%comment_title' => $comment['subject'],
      '%comment_description' => _casetracker_mail_plain_description($comment['comment']),
    ));
  }

  // make our own message ID so we can log it and allow responses via mailhandler.
  $msg_id = '<' . time() . '.' . mt_rand() . '@' . drupal_strtolower($_SERVER['SERVER_NAME']) . '>';
  $from = variable_get('casetracker_mail_address', variable_get('site_mail', ini_get('sendmail_from')));
  $subject = strtr(variable_get('casetracker_mail_subject', _casetracker_mail_subject()), $variables);
  $body = strtr(variable_get('casetracker_mail_case_message', _casetracker_mail_case_message()), $variables);
  db_query("INSERT INTO {casetracker_mail} (msg_id, nid, cid) VALUES ('%s', %d, %d)", $msg_id, $case->nid, isset($comment['cid']) ? $comment['cid'] : 0);

  // @todo this currently sends to only author and assigned. there needs to be
  // finer-grain control here, like an OG subscribers or all commenters, etc.
  $results = db_query("SELECT uid, name, mail FROM {users} WHERE uid IN (%d, %d, %d)", $comment['uid'], $case->uid, is_numeric($assignToId) ? $assignToId : casetracker_get_uid($assignToId));
  while ($result = db_fetch_object($results)) {
    if ($result->uid == $user->uid) {
      continue;
    }

    // don't fire to currently commenting user.
    if (!$result->mail) {
      continue;
    }

    // don't fire blanks.
    // if we get here a mail is send
    $mail_status = drupal_mail('casetracker_mail', $result->mail, $subject, $body, $from, array(
      'Message-ID' => $msg_id,
    ));
    if (!$mail_status) {

      // mail failure doesn't actually tell us much, since PHP returns no error string, but hey, feel good, right?
      watchdog('casetracker_mail', t('E-mail notification failed for %address.', array(
        '%address' => $result->mail,
      )));
    }
  }
}