diff --git a/src/MIME/MessageInterface.php b/src/MIME/MessageInterface.php index b3573d5..c706f93 100644 --- a/src/MIME/MessageInterface.php +++ b/src/MIME/MessageInterface.php @@ -56,6 +56,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..ddc2325 100644 --- a/src/MIME/MessageTrait.php +++ b/src/MIME/MessageTrait.php @@ -46,4 +46,19 @@ trait MessageTrait { */ abstract public function getHeader(); + /** + * {@inheritdoc} + */ + public function getCC($decode = FALSE) { + $body = $this->getHeader()->getFieldBody('Cc'); + 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]; + } + } diff --git a/tests/src/Unit/MIME/MessageTest.php b/tests/src/Unit/MIME/MessageTest.php index 4d9afcf..4558ee4 100644 --- a/tests/src/Unit/MIME/MessageTest.php +++ b/tests/src/Unit/MIME/MessageTest.php @@ -77,6 +77,18 @@ 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'); + $this->assertEquals('sun_is_shinning@example.com', current($message->getCC())); + $message = new Message(new Header([['name' => 'Cc', 'body' => 'bob_schwammkopf@xn--spuvabob-o4b.com']]), 'I am a body'); + $this->assertEquals('bob_schwammkopf@spužvabob.com', current($message->getCC(TRUE))); + } + + /** * Tests the message is valid and contains necessary fields. */ public function testValidation() {