예제 #1
0
 /**
  * Method used to forward the new email to the list of subscribers.
  *
  * @param   integer $user_id The user ID of the person performing this action
  * @param   integer $issue_id The issue ID
  * @param   array $message An array containing the email
  * @param   boolean $internal_only Whether the email should only be redirected to internal users or not
  * @param   boolean $assignee_only Whether the email should only be sent to the assignee
  * @param   boolean $type The type of email this is
  * @param   integer $sup_id the ID of this email
  * @return  void
  */
 public static function notifyNewEmail($usr_id, $issue_id, $message, $internal_only = false, $assignee_only = false, $type = '', $sup_id = false)
 {
     $prj_id = Issue::getProjectID($issue_id);
     $full_message = $message['full_email'];
     $sender = $message['from'];
     $sender_email = strtolower(Mail_Helper::getEmailAddress($sender));
     // get ID of whoever is sending this.
     $sender_usr_id = User::getUserIDByEmail($sender_email, true);
     if (empty($sender_usr_id)) {
         $sender_usr_id = false;
     }
     // automatically subscribe this sender to email notifications on this issue
     $subscribed_emails = self::getSubscribedEmails($issue_id, 'emails');
     $subscribed_emails = Misc::lowercase($subscribed_emails);
     if (!self::isIssueRoutingSender($issue_id, $sender) && !self::isBounceMessage($sender_email) && !in_array($sender_email, $subscribed_emails) && Workflow::shouldAutoAddToNotificationList($prj_id)) {
         $actions = array('emails');
         self::subscribeEmail($usr_id, $issue_id, $sender_email, $actions);
     }
     // get the subscribers
     $emails = array();
     $users = self::getUsersByIssue($issue_id, 'emails');
     foreach ($users as $user) {
         if (empty($user['sub_usr_id'])) {
             if ($internal_only == false) {
                 $email = $user['sub_email'];
             }
         } else {
             // if we are only supposed to send email to internal users, check if the role is lower than standard user
             if ($internal_only == true && User::getRoleByUser($user['sub_usr_id'], Issue::getProjectID($issue_id)) < User::ROLE_USER) {
                 continue;
             }
             // check if we are only supposed to send email to the assignees
             if ($internal_only == true && $assignee_only == true) {
                 $assignee_usr_ids = Issue::getAssignedUserIDs($issue_id);
                 if (!in_array($user['sub_usr_id'], $assignee_usr_ids)) {
                     continue;
                 }
             }
             $email = User::getFromHeader($user['sub_usr_id']);
         }
         if (empty($email)) {
             continue;
         }
         // don't send the email to the same person who sent it unless they want it
         if ($sender_usr_id != false) {
             $prefs = Prefs::get($sender_usr_id);
             if (!isset($prefs['receive_copy_of_own_action'][$prj_id])) {
                 $prefs['receive_copy_of_own_action'][$prj_id] = 0;
             }
             if ($prefs['receive_copy_of_own_action'][$prj_id] == 0 && (!empty($user['sub_usr_id']) && $sender_usr_id == $user['sub_usr_id'] || strtolower(Mail_Helper::getEmailAddress($email)) == $sender_email)) {
                 continue;
             }
         }
         $emails[] = $email;
     }
     if (!$emails) {
         return;
     }
     // change the sender of the message to {prefix}{issue_id}@{host}
     //  - keep everything else in the message, except 'From:', 'Sender:', 'To:', 'Cc:'
     // make 'Joe Blow <*****@*****.**>' become 'Joe Blow [CSC] <*****@*****.**>'
     $from = self::getFixedFromHeader($issue_id, $sender, 'issue');
     list($_headers, $body) = Mime_Helper::splitBodyHeader($full_message);
     $header_names = Mime_Helper::getHeaderNames($_headers);
     $current_headers = Mail_Helper::stripHeaders($message['headers']);
     $headers = array();
     // build the headers array required by the smtp library
     foreach ($current_headers as $header_name => $value) {
         if ($header_name == 'from') {
             $headers['From'] = $from;
         } else {
             if (is_array($value)) {
                 $value = implode('; ', $value);
             }
             $headers[$header_names[$header_name]] = $value;
         }
     }
     $headers['Subject'] = Mail_Helper::formatSubject($issue_id, $headers['Subject']);
     if (empty($type)) {
         if ($sender_usr_id != false && User::getRoleByUser($sender_usr_id, Issue::getProjectID($issue_id)) == User::ROLE_CUSTOMER) {
             $type = 'customer_email';
         } else {
             $type = 'other_email';
         }
     }
     $options = array('save_email_copy' => 1, 'issue_id' => $issue_id, 'type' => $type, 'sender_usr_id' => $sender_usr_id, 'type_id' => $sup_id);
     foreach ($emails as $to) {
         // add the warning message about replies being blocked or not
         // FIXME: $headers contains $headers['To'] from previous iteration
         $fixed_body = Mail_Helper::addWarningMessage($issue_id, $to, $body, $headers);
         $headers['To'] = Mime_Helper::encodeAddress($to);
         $mail = array('to' => $to, 'headers' => $headers, 'body' => $fixed_body);
         Mail_Queue::addMail($mail, $options);
     }
 }
예제 #2
0
 /**
  * Method used to send the SMTP based email message.
  *
  * @param   string $from The originator of the message
  * @param   string $to The recipient of the message
  * @param   string $subject The subject of the message
  * @param   integer $issue_id The ID of the issue. If false, email will not be associated with issue.
  * @param   string $type The type of message this is
  * @param   integer $sender_usr_id The id of the user sending this email.
  * @param   integer $type_id The ID of the event that triggered this notification (issue_id, sup_id, not_id, etc)
  * @return  string The full body of the message that was sent
  */
 public function send($from, $to, $subject, $save_email_copy = 0, $issue_id = false, $type = '', $sender_usr_id = false, $type_id = false)
 {
     // encode the addresses
     $from = Mime_Helper::encodeAddress($from);
     $to = Mime_Helper::encodeAddress($to);
     $subject = Mime_Helper::encode($subject);
     $body = $this->mime->get(array('text_charset' => APP_CHARSET, 'html_charset' => APP_CHARSET, 'head_charset' => APP_CHARSET, 'text_encoding' => APP_EMAIL_ENCODING));
     $headers = array('From' => $from, 'To' => self::fixAddressQuoting($to), 'Subject' => $subject);
     $this->setHeaders($headers);
     $hdrs = $this->mime->headers($this->headers);
     $mail = array('to' => $to, 'headers' => $hdrs, 'body' => $body);
     $options = array('save_email_copy' => $save_email_copy, 'issue_id' => $issue_id, 'type' => $type, 'sender_usr_id' => $sender_usr_id, 'type_id' => $type_id);
     $res = Mail_Queue::addMail($mail, $options);
     if (Misc::isError($res) || $res == false) {
         return $res;
     }
     // RFC 822 formatted date
     $header = 'Date: ' . Date_Helper::getRFC822Date(time()) . "\r\n";
     // return the full dump of the email
     foreach ($hdrs as $name => $value) {
         $header .= "{$name}: {$value}\r\n";
     }
     $header .= "\r\n";
     return $header . $body;
 }