diff --git a/inmail_collect/src/Plugin/collect/Model/InmailMessage.php b/inmail_collect/src/Plugin/collect/Model/InmailMessage.php index 9c2f6e6..7e4f182 100644 --- a/inmail_collect/src/Plugin/collect/Model/InmailMessage.php +++ b/inmail_collect/src/Plugin/collect/Model/InmailMessage.php @@ -100,7 +100,7 @@ class InmailMessage extends ModelPluginBase implements ContainerFactoryPluginInt $output['to'] = array( '#type' => 'item', '#title' => $this->t('To'), - '#markup' => htmlentities($parsed_data->getTo()), + '#markup' => $parsed_data->getTo(), ); return $output; diff --git a/inmail_collect/src/Plugin/inmail/Handler/CollectHandler.php b/inmail_collect/src/Plugin/inmail/Handler/CollectHandler.php index e458318..165298f 100644 --- a/inmail_collect/src/Plugin/inmail/Handler/CollectHandler.php +++ b/inmail_collect/src/Plugin/inmail/Handler/CollectHandler.php @@ -47,7 +47,7 @@ class CollectHandler extends HandlerBase { public function invoke(MessageInterface $message, ProcessorResultInterface $processor_result) { // For successful processing, a message needs to follow the standards. // Some aspects are critical. Check them and cancel otherwise and log. - if (!$message->getReceivedDate() || !$message->getFrom() || !$message->getTo() || $message->getSubject() === NULL) { + if (!$message->getReceivedDate() || !$message->getFrom() || $message->getSubject() === NULL) { \Drupal::logger('inmail')->info('Not creating container from message missing necessary header fields.'); return; } diff --git a/src/MIME/MessageInterface.php b/src/MIME/MessageInterface.php index b3573d5..8be72da 100644 --- a/src/MIME/MessageInterface.php +++ b/src/MIME/MessageInterface.php @@ -37,9 +37,8 @@ interface MessageInterface extends EntityInterface { * @param bool $decode * Optional value to indicate if header is in punycode form. * - * @return string|null - * The content of the 'To' header field, or null if that field does not - * exist. + * @return array + * List of recipient addresses. */ public function getTo($decode = FALSE); @@ -56,6 +55,17 @@ interface MessageInterface extends EntityInterface { public function getFrom($decode = FALSE); /** + * Returns the addresses of other recipients. + * + * @param bool $decode + * Optional value to indicate if header is in punycode form. + * + * @return array + * The list of other recipients. + */ + public function getCC($decode = FALSE); + + /** * Returns the date when the message was received by the recipient. * * @return \Drupal\Component\DateTime\DateTimePlus diff --git a/src/MIME/MessageTrait.php b/src/MIME/MessageTrait.php index ca921a0..4d2624b 100644 --- a/src/MIME/MessageTrait.php +++ b/src/MIME/MessageTrait.php @@ -12,14 +12,7 @@ trait MessageTrait { */ public function getTo($decode = FALSE) { $body = $this->getHeader()->getFieldBody('To'); - if ($decode) { - if (strpos($body, '@') !== FALSE) { - // Extracting body after '@' sign for proper IDN decoding. - $body = explode('@', $body, 2)[0] . '@' . idn_to_utf8(explode('@', $body, 2)[1]); - } - //@todo Properly parse Mail Address https://www.drupal.org/node/2800585 - } - return $body; + return ($decode) ? ($this->getDecodedAddress($body)) : ($body); } /** @@ -27,13 +20,7 @@ trait MessageTrait { */ public function getFrom($decode = FALSE) { $body = $this->getHeader()->getFieldBody('From'); - if ($decode) { - if (strpos($body, '@') !== FALSE) { - $body = explode('@', $body, 2)[0] . '@' . idn_to_utf8(explode('@', $body, 2)[1]); - } - //@todo Properly parse Mail Address https://www.drupal.org/node/2800585 - } - return $body; + return ($decode) ? ($this->getDecodedAddress($body)) : ($body); } /** @@ -46,4 +33,30 @@ trait MessageTrait { */ abstract public function getHeader(); + /** + * {@inheritdoc} + */ + public function getCC($decode = FALSE) { + $body = $this->getHeader()->getFieldBody('Cc'); + return ($decode) ? ([$this->getDecodedAddress($body)]) : ([$body]); + } + + /** + * Helper method to convert body from Puny-code to UTF8. + * + * @param string $body + * Field body to be decoded. + * + * @return string|null + * Decoded UTF8 address if successful decoding, otherwise NULL. + */ + protected function getDecodedAddress($body) { + if (strpos($body, '@') !== FALSE) { + // Extracting body after '@' sign for proper IDN decoding. + $body = explode('@', $body, 2)[0] . '@' . idn_to_utf8(explode('@', $body, 2)[1]); + } + //@todo Properly parse Mail Address https://www.drupal.org/node/2800585. + return $body; + } + } diff --git a/tests/src/Unit/MIME/MessageTest.php b/tests/src/Unit/MIME/MessageTest.php index 4d9afcf..def9256 100644 --- a/tests/src/Unit/MIME/MessageTest.php +++ b/tests/src/Unit/MIME/MessageTest.php @@ -77,6 +77,17 @@ class MessageTest extends UnitTestCase { } /** + * Tests the other recipients getter. + * + * @covers ::getCC + */ + public function testGetCC() { + $message = new Message(new Header([['name' => 'Cc', 'body' => 'sun_is_shinning@example.com']]), 'I am a body'); + $cc_field = $message->getCC(); + $this->assertEquals('sun_is_shinning@example.com', reset($cc_field)); + } + + /** * Tests the message is valid and contains necessary fields. */ public function testValidation() {