Ejemplo n.º 1
0
 /**
  * Get the message from the designated template files.
  *
  * @return array ($phpMsg (bool), $message (string))
  */
 private function _message()
 {
     $phpMsg = false;
     $params = $this->getParams();
     $contentTemplate = $params->get('emailtable_template_content', '');
     $content = empty($contentTemplate) ? '' : FabrikHelperHTML::getContentTemplate($contentTemplate);
     $emailTemplate = $this->_emailTemplate();
     if (JFile::exists($emailTemplate)) {
         if (JFile::getExt($emailTemplate) == 'php') {
             $message = '';
             $phpMsg = true;
         } else {
             $message = FabrikHelperHTML::getTemplateFile($emailTemplate);
         }
         $message = str_replace('{content}', $content, $message);
     } else {
         $message = $contentTemplate != '' ? $content : '';
     }
     return array($phpMsg, $message);
 }
Ejemplo n.º 2
0
 /**
  * Run right at the end of the form processing
  * form needs to be set to record in database for this to hook to be called
  *
  * @return	bool
  */
 public function onAfterProcess()
 {
     $profiler = JProfiler::getInstance('Application');
     JDEBUG ? $profiler->mark("email: start: onAfterProcess") : null;
     $params = $this->getParams();
     $input = $this->app->input;
     jimport('joomla.mail.helper');
     $w = new FabrikWorker();
     /** @var \FabrikFEModelForm $formModel */
     $formModel = $this->getModel();
     $emailTemplate = JPath::clean(JPATH_SITE . '/plugins/fabrik_form/email/tmpl/' . $params->get('email_template', ''));
     $this->data = $this->getProcessData();
     /* $$$ hugh - moved this to here from above the previous line, 'cos it needs $this->data
      * check if condition exists and is met
      */
     if ($this->alreadySent() || !$this->shouldProcess('email_conditon', null, $params)) {
         return true;
     }
     /**
      * Added option to run content plugins on message text.  Note that rather than run it one time at the
      * end of the following code, after we have assembled all the various options in to a single $message,
      * it needs to be run separately on each type of content.  This is because we do placeholder replacement
      * in various places, which will strip all {text} which doesn't match element names.
      */
     $runContentPlugins = $params->get('email_run_content_plugins', '0') === '1';
     $contentTemplate = $params->get('email_template_content');
     $content = $contentTemplate != '' ? FabrikHelperHTML::getContentTemplate($contentTemplate, 'both', $runContentPlugins) : '';
     // Always send as html as even text email can contain html from wysiwyg editors
     $htmlEmail = true;
     $messageTemplate = '';
     if (JFile::exists($emailTemplate)) {
         $messageTemplate = JFile::getExt($emailTemplate) == 'php' ? $this->_getPHPTemplateEmail($emailTemplate) : $this->_getTemplateEmail($emailTemplate);
         // $$$ hugh - added ability for PHP template to return false to abort, same as if 'condition' was was false
         if ($messageTemplate === false) {
             return true;
         }
         if ($runContentPlugins === true) {
             FabrikHelperHTML::runContentPlugins($messageTemplate);
         }
         $messageTemplate = str_replace('{content}', $content, $messageTemplate);
     }
     $messageText = $params->get('email_message_text', '');
     if (!empty($messageText)) {
         if ($runContentPlugins === true) {
             FabrikHelperHTML::runContentPlugins($messageText);
         }
         $messageText = str_replace('{content}', $content, $messageText);
         $messageText = str_replace('{template}', $messageTemplate, $messageText);
         $messageText = $w->parseMessageForPlaceholder($messageText, $this->data, false);
     }
     if (!empty($messageText)) {
         $message = $messageText;
     } elseif (!empty($messageTemplate)) {
         $message = $messageTemplate;
     } elseif (!empty($content)) {
         $message = $content;
     } else {
         $message = $this->_getTextEmail();
     }
     $this->addAttachments();
     $cc = null;
     $bcc = null;
     // $$$ hugh - test stripslashes(), should be safe enough.
     $message = stripslashes($message);
     $editURL = COM_FABRIK_LIVESITE . 'index.php?option=com_' . $this->package . '&view=form&fabrik=' . $formModel->get('id') . '&rowid=' . $input->get('rowid', '', 'string');
     $viewURL = COM_FABRIK_LIVESITE . 'index.php?option=com_' . $this->package . '&view=details&fabrik=' . $formModel->get('id') . '&rowid=' . $input->get('rowid', '', 'string');
     $editLink = '<a href="' . $editURL . '">' . FText::_('EDIT') . '</a>';
     $viewLink = '<a href="' . $viewURL . '">' . FText::_('VIEW') . '</a>';
     $message = str_replace('{fabrik_editlink}', $editLink, $message);
     $message = str_replace('{fabrik_viewlink}', $viewLink, $message);
     $message = str_replace('{fabrik_editurl}', $editURL, $message);
     $message = str_replace('{fabrik_viewurl}', $viewURL, $message);
     // $$$ rob if email_to is not a valid email address check the raw value to see if that is
     $emailTo = explode(',', $params->get('email_to'));
     foreach ($emailTo as &$emailKey) {
         $emailKey = $w->parseMessageForPlaceholder($emailKey, $this->data, false);
         // Can be in repeat group in which case returns "email1,email2"
         $emailKey = explode(',', $emailKey);
         foreach ($emailKey as &$key) {
             // $$$ rob added strstr test as no point trying to add raw suffix if not placeholder in $emailKey
             if (!FabrikWorker::isEmail($key) && trim($key) !== '' && strstr($key, '}')) {
                 $key = explode('}', $key);
                 if (substr($key[0], -4) !== '_raw') {
                     $key = $key[0] . '_raw}';
                 } else {
                     $key = $key[0] . '}';
                 }
                 $key = $w->parseMessageForPlaceholder($key, $this->data, false);
             }
         }
     }
     // Reduce back down to single dimension array
     foreach ($emailTo as $i => $a) {
         foreach ($a as $v) {
             $emailTo[] = $v;
         }
         unset($emailTo[$i]);
     }
     $emailToEval = $params->get('email_to_eval', '');
     if (!empty($emailToEval)) {
         $emailToEval = $w->parseMessageForPlaceholder($emailToEval, $this->data, false);
         $emailToEval = @eval($emailToEval);
         FabrikWorker::logEval($emailToEval, 'Caught exception on eval in email emailto : %s');
         $emailToEval = explode(',', $emailToEval);
         $emailTo = array_merge($emailTo, $emailToEval);
     }
     @(list($emailFrom, $emailFromName) = explode(":", $w->parseMessageForPlaceholder($params->get('email_from'), $this->data, false), 2));
     if (empty($emailFrom)) {
         $emailFrom = $this->config->get('mailfrom');
     }
     if (empty($emailFromName)) {
         $emailFromName = $this->config->get('fromname', $emailFrom);
     }
     // Changes by JFQ
     @(list($returnPath, $returnPathName) = explode(":", $w->parseMessageForPlaceholder($params->get('return_path'), $this->data, false), 2));
     if (empty($returnPath)) {
         $returnPath = null;
     }
     if (empty($returnPathName)) {
         $returnPathName = null;
     }
     // End changes
     $subject = $params->get('email_subject');
     if ($subject == '') {
         $subject = $this->config->get('sitename') . " :: Email";
     }
     $subject = preg_replace_callback('/&#([0-9a-fx]+);/mi', array($this, 'replace_num_entity'), $subject);
     $attachType = $params->get('email_attach_type', '');
     $attachFileName = $this->config->get('tmp_path') . '/' . uniqid() . '.' . $attachType;
     $query = $this->_db->getQuery(true);
     $emailTo = array_map('trim', $emailTo);
     // Add any assigned groups to the to list
     $sendTo = (array) $params->get('to_group');
     $groupEmails = (array) $this->getUsersInGroups($sendTo, $field = 'email');
     $emailTo = array_merge($emailTo, $groupEmails);
     $emailTo = array_unique($emailTo);
     // Remove blank email addresses
     $emailTo = array_filter($emailTo);
     $dbEmailTo = array_map(array($this->_db, 'quote'), $emailTo);
     // Get an array of user ids from the email to array
     if (!empty($dbEmailTo)) {
         $query->select('id, email')->from('#__users')->where('email IN (' . implode(',', $dbEmailTo) . ')');
         $this->_db->setQuery($query);
         $userIds = $this->_db->loadObjectList('email');
     } else {
         $userIds = array();
     }
     // Send email
     foreach ($emailTo as $email) {
         $email = strip_tags($email);
         if (FabrikWorker::isEmail($email)) {
             $thisAttachments = $this->attachments;
             $this->data['emailto'] = $email;
             $userId = array_key_exists($email, $userIds) ? $userIds[$email]->id : 0;
             $thisUser = JFactory::getUser($userId);
             $thisMessage = $w->parseMessageForPlaceholder($message, $this->data, true, false, $thisUser);
             $thisSubject = strip_tags($w->parseMessageForPlaceholder($subject, $this->data, true, false, $thisUser));
             if (!empty($attachType)) {
                 if (JFile::write($attachFileName, $thisMessage)) {
                     $thisAttachments[] = $attachFileName;
                 } else {
                     $attachFileName = '';
                 }
             }
             $this->pdfAttachment($thisAttachments);
             /*
              * Sanity check for attachment files existing.  Could have base folder paths for things
              * like file upload elements with no file.  As of J! 3.5.1, the J! mailer tosses an exception
              * if files don't exist.  We catch that in the sendMail helper, but remove non-files here anyway
              */
             foreach ($thisAttachments as $aKey => $attachFile) {
                 if (!JFile::exists($attachFile)) {
                     unset($thisAttachments[$aKey]);
                 }
             }
             $res = FabrikWorker::sendMail($emailFrom, $emailFromName, $email, $thisSubject, $thisMessage, $htmlEmail, $cc, $bcc, $thisAttachments, $returnPath, $returnPathName);
             /*
              * $$$ hugh - added some error reporting, but not sure if 'invalid address' is the appropriate message,
              * may need to add a generic "there was an error sending the email" message
              */
             if ($res !== true) {
                 $this->app->enqueueMessage(JText::sprintf('PLG_FORM_EMAIL_DID_NOT_SEND_EMAIL', $email), 'notice');
             }
             if (JFile::exists($attachFileName)) {
                 JFile::delete($attachFileName);
             }
         } else {
             $this->app->enqueueMessage(JText::sprintf('PLG_FORM_EMAIL_DID_NOT_SEND_EMAIL_INVALID_ADDRESS', $email), 'notice');
         }
     }
     foreach ($this->deleteAttachments as $attachment) {
         if (JFile::exists($attachment)) {
             JFile::delete($attachment);
         }
     }
     $this->updateRow();
     return true;
 }