/**
  * Emails a document as an attachment or hyperlink to a list of users, groups or external email addresses.
  * In the case of external addresses, if a hyperlink is used then a timed download link (via webservices) is sent allowing the recipient a window period in which to download the document.
  * The period is set through the webservices config option webservice/downloadExpiry. Defaults to 30 minutes.
  *
  * @author KnowledgeTree Team
  * @access public
  * @param array $members The email recipients - KTPAI_Users, KTAPI_Groups or email addresses
  * @param string $comment Content to be appended to the email
  * @param bool $attachDocument TRUE if document is an attachment | FALSE if using a hyperlink to the document
  */
 public function email($members, $comment, $attachDocument = true)
 {
     if (empty($members)) {
         return;
     }
     $userIds = array();
     $groupIds = array();
     $emailAddrs = array();
     foreach ($members as $member) {
         if ($member instanceof KTAPI_User) {
             $userIds[] = $member->Id;
         } elseif ($member instanceof KTAPI_Group) {
             $groupIds[] = $member->Id;
         } elseif (is_string($member)) {
             $emailAddrs[] = $member;
         }
     }
     $config = KTConfig::getSingleton();
     $allowAttachment = $config->get('email/allowAttachment', false);
     $allowEmailAddresses = $config->get('email/allowEmailAddresses', false);
     // if attachments aren't allowed, set $attachDocument to false
     if (!$allowAttachment) {
         $attachDocument = false;
     }
     // If sending to external email addresses is not allowed - set array of external recipients to empty
     if (!$allowEmailAddresses) {
         $emailAddrs = array();
     }
     $emailErrors = array();
     $userEmails = array();
     $listEmails = array();
     sendGroupEmails($groupIds, $userEmails, $emailErrors);
     sendUserEmails($userIds, $userEmails, $emailErrors);
     if ($attachDocument) {
         sendManualEmails($emailAddrs, $userEmails, $emailErrors);
     } else {
         sendExternalEmails($emailAddrs, $this->documentid, $this->get_title(), $comment, $emailErrors);
     }
     if (empty($userEmails)) {
         return;
     }
     $listEmails = array_keys($userEmails);
     sendEmail($listEmails, $this->documentid, $this->get_title(), $comment, (bool) $attachDocument, $emailErrors);
 }
Example #2
0
 function do_email()
 {
     $groupNewRight = trim($_REQUEST['groups_items_added'], chr(160));
     $userNewRight = trim($_REQUEST['users_items_added'], chr(160));
     $fEmailAddresses = trim($_REQUEST['fEmailAddresses']);
     $fAttachDocument = $_REQUEST['fAttachDocument'];
     $fComment = $this->oValidator->validateString($_REQUEST['fComment'], array('redirect_to' => array('', sprintf('fDocumentId=%d', $this->oDocument->getId()))));
     // explode group and user ids
     $aGroupIDs = array();
     $aUserIDs = array();
     $aEmailAddresses = array();
     if (!empty($groupNewRight)) {
         $aGroupIDs = explode(',', $groupNewRight);
     }
     if (!empty($userNewRight)) {
         $aUserIDs = explode(',', $userNewRight);
     }
     if (!empty($fEmailAddresses)) {
         $aAddresses = explode("\n", $fEmailAddresses);
         foreach ($aAddresses as $item) {
             $aItems = explode(' ', $item);
             $aEmailAddresses = array_merge($aEmailAddresses, $aItems);
         }
     }
     $oConfig = KTConfig::getSingleton();
     $bAttachment = $oConfig->get('email/allowAttachment', false);
     $bEmailAddresses = $oConfig->get('email/allowEmailAddresses', false);
     if (empty($bAttachment)) {
         $fAttachDocument = false;
     }
     if (empty($bEmailAddresses)) {
         $aEmailAddresses = array();
     }
     //if we're going to send a mail, first make there is someone to send it to
     if (count($aGroupIDs) == 0 && count($aUserIDs) == 0 && count($aEmailAddresses) == 0) {
         $this->errorRedirectToMain(_kt('No recipients set'), sprintf('fDocumentId=%d', $this->oDocument->getId()));
         exit(0);
     }
     $iDocumentID = $this->oDocument->getID();
     $sDocumentName = $this->oDocument->getName();
     $aEmailErrors = array();
     $aUserEmails = array();
     // send group emails
     sendGroupEmails($aGroupIDs, $aUserEmails, $aEmailErrors);
     // send user emails
     sendUserEmails($aUserIDs, $aUserEmails, $aEmailErrors);
     // send manual/external email addresses
     if ((bool) $fAttachDocument) {
         sendManualEmails($aEmailAddresses, $aUserEmails, $aEmailErrors);
     } else {
         sendExternalEmails($aEmailAddresses, $iDocumentID, $sDocumentName, $fComment, $aEmailErrors);
     }
     // get list of email addresses and send
     if (!empty($aUserEmails)) {
         // email addresses are in the keys -> extract the keys
         $aListEmails = array_keys($aUserEmails);
         sendEmail($aListEmails, $iDocumentID, $sDocumentName, $fComment, (bool) $fAttachDocument, $aEmailErrors);
     }
     if (count($aEmailErrors)) {
         $_SESSION['KTErrorMessage'][] = join('<br />\\n', $aEmailErrors);
     }
     $_SESSION['KTInfoMessage'][] = _kt('Email sent');
     //go back to the document view page
     controllerRedirect('viewDocument', sprintf("fDocumentId=%d", $this->oDocument->getId()));
 }