From 66618315b4a6f9a71ad2ffa24da15218442037a1 Mon Sep 17 00:00:00 2001 From: elvill <elias.villiger@uzh.ch> Date: Fri, 9 Feb 2018 09:44:08 +0100 Subject: [PATCH] Feature #5318 - Allow sendmail speaking word tokens --- extension/Documentation/Manual.rst | 5 +-- extension/qfq/qfq/Constants.php | 6 ++-- extension/qfq/qfq/report/SendMail.php | 33 +++++++++++++++++--- extension/qfq/tests/phpunit/SendMailTest.php | 13 ++++++-- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/extension/Documentation/Manual.rst b/extension/Documentation/Manual.rst index 5e1d19771..a4461b86d 100644 --- a/extension/Documentation/Manual.rst +++ b/extension/Documentation/Manual.rst @@ -5506,7 +5506,7 @@ Column: _sendmail t:<TO:email[,email]>|f:<FROM:email>|s:<subject>|b:<body> [|c:<CC:email[,email]]>[|B:<BCC:email[,email]]>[|r:<REPLY-TO:email>] - [|A:<flag autosubmit: on/off>][|g:<grId>][|x:<xId>][|y:<xId2>][|z:<xId3>] + [|A:<flag autosubmit: on/off>][|g:<grId>][|x:<xId>][|y:<xId2>][|z:<xId3>][|h:<mail header>] [|e:<subject encode: encode/decode/none>][E:<body encode: encode/decode/none>] [|C][d:<filename of the attachment>][|F:<file to attach>][|u:<url>][|p:<T3 uri>] @@ -5516,7 +5516,7 @@ The following parameters can also be written as complete words for ease of use: to:<email[,email]>|from:<email>|subject:<subject>|body:<body> [|cc:<email[,email]]>[|bcc:<email[,email]]>[|reply-to:<email>] - [|autosubmit:<on/off>][|grid:<grid>][|xid:<xId>][|xid2:<xId2>][|xid3:<xId3>] + [|autosubmit:<on/off>][|grid:<grid>][|xid:<xId>][|xid2:<xId2>][|xid3:<xId3>][|header:<mail header>] Send emails. Every mail will be logged in the table `mailLog`. Attachments are supported. @@ -5554,6 +5554,7 @@ Send emails. Every mail will be logged in the table `mailLog`. Attachments are s | body | | | yes | +------------+----------------------------------------+--------------------------------------------------------------------------------------------------+------------+ | h | Mail header |**Custom mail header**: Separate multiple header with \\r\\n | | +| header | | | yes | +------------+----------------------------------------+--------------------------------------------------------------------------------------------------+------------+ | F | Attach file |**Attachment**: File to attach to the mail. Repeatable. | | +------------+----------------------------------------+--------------------------------------------------------------------------------------------------+------------+ diff --git a/extension/qfq/qfq/Constants.php b/extension/qfq/qfq/Constants.php index e3a8c64e0..2ccb7903c 100644 --- a/extension/qfq/qfq/Constants.php +++ b/extension/qfq/qfq/Constants.php @@ -1128,6 +1128,10 @@ const SENDMAIL_TOKEN_X_ID2 = 'y'; const SENDMAIL_TOKEN_X_ID2_LONG = 'xid2'; const SENDMAIL_TOKEN_X_ID3 = 'z'; const SENDMAIL_TOKEN_X_ID3_LONG = 'xid3'; +const SENDMAIL_TOKEN_HEADER = 'h'; +const SENDMAIL_TOKEN_HEADER_LONG = 'header'; +const SENDMAIL_TOKEN_SRC = 'S'; +const SENDMAIL_TOKEN_SRC_LONG = 'source'; const SENDMAIL_TOKEN_RECEIVER_CC = 'c'; const SENDMAIL_TOKEN_RECEIVER_CC_LONG = 'cc'; const SENDMAIL_TOKEN_RECEIVER_BCC = 'B'; @@ -1135,10 +1139,8 @@ const SENDMAIL_TOKEN_RECEIVER_BCC_LONG = 'bcc'; const SENDMAIL_TOKEN_ATTACHMENT = 'attachment'; const SENDMAIL_TOKEN_ATTACHMENT_FILE = 'F'; const SENDMAIL_TOKEN_ATTACHMENT_FILE_DEPRECATED = 'a'; // since 5.12.17 -const SENDMAIL_TOKEN_HEADER = 'h'; const SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY = 'e'; const SENDMAIL_TOKEN_BODY_HTML_ENTITY = 'E'; -const SENDMAIL_TOKEN_SRC = 'S'; const SENDMAIL_TOKEN_CONCAT = 'C'; const SENDMAIL_TOKEN_DOWNLOAD_FILENAME = 'd'; diff --git a/extension/qfq/qfq/report/SendMail.php b/extension/qfq/qfq/report/SendMail.php index c5bc04ef3..854c96e1a 100644 --- a/extension/qfq/qfq/report/SendMail.php +++ b/extension/qfq/qfq/report/SendMail.php @@ -355,6 +355,23 @@ class SendMail { $flagSource = false; $flagFilename = false; + $convertToShorthandToken = [ + SENDMAIL_TOKEN_RECEIVER_LONG => SENDMAIL_TOKEN_RECEIVER, + SENDMAIL_TOKEN_SENDER_LONG => SENDMAIL_TOKEN_SENDER, + SENDMAIL_TOKEN_SUBJECT_LONG => SENDMAIL_TOKEN_SUBJECT, + SENDMAIL_TOKEN_BODY_LONG => SENDMAIL_TOKEN_BODY, + SENDMAIL_TOKEN_RECEIVER_CC_LONG => SENDMAIL_TOKEN_RECEIVER_CC, + SENDMAIL_TOKEN_RECEIVER_BCC_LONG => SENDMAIL_TOKEN_RECEIVER_BCC, + SENDMAIL_TOKEN_REPLY_TO_LONG => SENDMAIL_TOKEN_REPLY_TO, + SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT_LONG => SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT, + SENDMAIL_TOKEN_GR_ID_LONG => SENDMAIL_TOKEN_GR_ID, + SENDMAIL_TOKEN_X_ID_LONG => SENDMAIL_TOKEN_X_ID, + SENDMAIL_TOKEN_X_ID2_LONG => SENDMAIL_TOKEN_X_ID2, + SENDMAIL_TOKEN_X_ID3_LONG => SENDMAIL_TOKEN_X_ID3, + SENDMAIL_TOKEN_HEADER_LONG => SENDMAIL_TOKEN_HEADER, + SENDMAIL_TOKEN_SRC_LONG => SENDMAIL_TOKEN_SRC, + ]; + $param = explode(PARAM_DELIMITER, $data); // Iterate over all parameter: use token as key. Collect corresponding attachments arguments in separate array elements @@ -365,12 +382,20 @@ class SendMail { } $tokenAndValue = explode(PARAM_TOKEN_DELIMITER, $line, 2); - if (count($tokenAndValue) < 2) { + if (count($tokenAndValue) < 2 && $tokenAndValue[0] !== SENDMAIL_TOKEN_CONCAT) { throw new UserFormException('Missing token delimiter "' . PARAM_TOKEN_DELIMITER . '" in: ' . $line, ERROR_UNKNOWN_TOKEN); } - // speaking word tokens are all lowercase - $token = strlen($tokenAndValue[0]) > 1 ? strtolower($tokenAndValue[0]) : $tokenAndValue[0]; + $token = $tokenAndValue[0]; + // convert speaking word tokens to shorthand + if (strlen($token) > 1) { + $token = strtolower($token); // speaking word tokens are all lowercase + if (isset($convertToShorthandToken[$token])) { + $token = $convertToShorthandToken[$token]; + } else { + throw new UserFormException('Unknown token "' . $token . '" in: ' . $line, ERROR_UNKNOWN_TOKEN); + } + } // Check for deprecated token. if ($token == SENDMAIL_TOKEN_ATTACHMENT_FILE_DEPRECATED) { @@ -410,7 +435,7 @@ class SendMail { break; default: - $args[$token] = substr($line, 2); + $args[$token] = $tokenAndValue[1]; break; } } diff --git a/extension/qfq/tests/phpunit/SendMailTest.php b/extension/qfq/tests/phpunit/SendMailTest.php index aa49feb6d..1a8be64f3 100644 --- a/extension/qfq/tests/phpunit/SendMailTest.php +++ b/extension/qfq/tests/phpunit/SendMailTest.php @@ -30,16 +30,22 @@ class SendMailTest extends \PHPUnit_Framework_TestCase { $expect = []; $this->assertEquals($expect, $result); - // Simple 'fixed position' DEPRECATED - not sure if it's ok that the array is not fillled up to the maximum. + // Simple 'fixed position' DEPRECATED - not sure if it's ok that the array is not filled up to the maximum. // $result = $this->sendMail->parseStringToArray('john@doe.com|jane@miller.com|Latest|Dear John'); // $expect = ['john@doe.com', 'jane@miller.com', 'Latest', 'Dear John']; // $this->assertEquals($expect, $result); - // Simple 'token based' + // Simple 'shorthand token based' $result = $this->sendMail->parseStringToArray('t:john@doe.com|f:jane@miller.com|s:Latest|b:Dear John'); $expect = [SENDMAIL_TOKEN_RECEIVER => 'john@doe.com', SENDMAIL_TOKEN_SENDER => 'jane@miller.com', SENDMAIL_TOKEN_SUBJECT => 'Latest', SENDMAIL_TOKEN_BODY => 'Dear John']; $this->assertEquals($expect, $result); + // Simple 'speaking word based' + $result = $this->sendMail->parseStringToArray('to:john@doe.com|from:jane@miller.com|subject:Latest|body:Dear John'); + $this->assertEquals($expect, $result); + // Simple 'speaking word / shorthand' mixed tokens + $result = $this->sendMail->parseStringToArray('to:john@doe.com|f:jane@miller.com|s:Latest|body:Dear John'); + $this->assertEquals($expect, $result); // All (but attachment) 'token based' $result = $this->sendMail->parseStringToArray('t:john@doe.com|f:jane@miller.com|s:Latest|b:Dear John|r:reply@doe.com|A:on|g:123|x:234|c:july@doe.com,steve@doe.com|B:ceo@doe.com|h:Auto-Submit: fake|y:345|z:456|S:test.php'); @@ -49,6 +55,9 @@ class SendMailTest extends \PHPUnit_Framework_TestCase { SENDMAIL_TOKEN_RECEIVER_CC => 'july@doe.com,steve@doe.com', SENDMAIL_TOKEN_RECEIVER_BCC => 'ceo@doe.com', SENDMAIL_TOKEN_HEADER => 'Auto-Submit: fake', SENDMAIL_TOKEN_X_ID2 => '345', SENDMAIL_TOKEN_X_ID3 => '456', SENDMAIL_TOKEN_SRC => 'test.php']; $this->assertEquals($expect, $result); + // All (but attachment) 'speaking word based' + $result = $this->sendMail->parseStringToArray('to:john@doe.com|from:jane@miller.com|subject:Latest|body:Dear John|reply-to:reply@doe.com|autosubmit:on|grid:123|xid:234|cc:july@doe.com,steve@doe.com|bcc:ceo@doe.com|header:Auto-Submit: fake|xid2:345|xid3:456|source:test.php'); + $this->assertEquals($expect, $result); // Single attachment 'token based' $attach = [['F:fileadmin/test1.pdf']]; -- GitLab