Skip to content

Commit

Permalink
Issue: Template Variables in Ticket Filter
Browse files Browse the repository at this point in the history
This commit fixes an issue where you could not use template variables when using the 'Send an Email' filter action. All variables would just display as 'Array'.

We needed to pass the Ticket object to FA_SendEmail::apply however, we were running the filters before the Ticket object was created. To fix this, we can add the Ticket object to vars and run the filterTicketData function again after the Ticket is created and only apply the send email filter action if the Ticket object is found.
  • Loading branch information
aydreeihn committed Dec 17, 2019
1 parent 7578d5c commit 12e52f4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
66 changes: 35 additions & 31 deletions include/class.filter_action.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,40 +516,44 @@ class FA_SendEmail extends TriggerAction {
function apply(&$ticket, array $info) {
global $ost;

$config = $this->getConfiguration();
$info = array('subject' => $config['subject'],
'message' => $config['message']);
$info = $ost->replaceTemplateVariables(
$info, array('ticket' => $ticket)
);

// Honor FROM address settings
if (!$config['from'] || !($mailer = Email::lookup($config['from'])))
$mailer = new Mailer();

// Allow %{user} in the To: line
$replacer = new VariableReplacer();
$replacer->assign(array(
'user' => sprintf('"%s" <%s>', $ticket['name'], $ticket['email'])
));
$to = $replacer->replaceVars($config['recipients']);

require_once PEAR_DIR . 'Mail/RFC822.php';
require_once PEAR_DIR . 'PEAR.php';

if (!($mails = Mail_RFC822::parseAddressList($to)) || PEAR::isError($mails))
return false;

// Allow %{recipient} in the body
foreach ($mails as $R) {
$recipient = sprintf('%s <%s@%s>', $R->personal, $R->mailbox, $R->host);
if ($ticket['ticket']) {
$config = $this->getConfiguration();
$vars = array(
'url' => $ost->getConfig()->getBaseUrl(),
'ticket' => $ticket['ticket'],
);
$info = $ost->replaceTemplateVariables(array(
'subject' => $config['subject'],
'message' => $config['message'],
), $vars);

// Honor FROM address settings
if (!$config['from'] || !($mailer = Email::lookup($config['from'])))
$mailer = new Mailer();

// Allow %{user} in the To: line
$replacer = new VariableReplacer();
$replacer->assign(array(
'recipient' => new EmailAddress($recipient),
'user' => sprintf('"%s" <%s>', $ticket['name'], $ticket['email'])
));
$I = $replacer->replaceVars($info);
$mailer->send($recipient, $I['subject'], $I['message']);
$to = $replacer->replaceVars($config['recipients']);

require_once PEAR_DIR . 'Mail/RFC822.php';
require_once PEAR_DIR . 'PEAR.php';

if (!($mails = Mail_RFC822::parseAddressList($to)) || PEAR::isError($mails))
return false;

// Allow %{recipient} in the body
foreach ($mails as $R) {
$recipient = sprintf('%s <%s@%s>', $R->personal, $R->mailbox, $R->host);
$replacer->assign(array(
'recipient' => new EmailAddress($recipient),
));
$I = $replacer->replaceVars($info);
$mailer->send($recipient, $I['subject'], $I['message']);
}
}

}

static function getVarScope() {
Expand Down
3 changes: 3 additions & 0 deletions include/class.ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -3851,6 +3851,9 @@ static function create($vars, &$errors, $origin, $autorespond=true,
return null;

/* -------------------- POST CREATE ------------------------ */
$vars['ticket'] = $ticket;
self::filterTicketData($origin, $vars,
array_merge(array($form), $topic_forms), $user);

// Save the (common) dynamic form
// Ensure we have a subject
Expand Down

0 comments on commit 12e52f4

Please sign in to comment.