Browse Source

Add check valid transfer-encoding

LifeOrYou 8 years ago
parent
commit
3a75517f98
2 changed files with 26 additions and 0 deletions
  1. 22 0
      src/Mailer/Email.php
  2. 4 0
      tests/TestCase/Mailer/EmailTest.php

+ 22 - 0
src/Mailer/Email.php

@@ -250,6 +250,19 @@ class Email implements JsonSerializable, Serializable
     protected $transferEncoding;
 
     /**
+     * Available encoding to be set for transfer.
+     *
+     * @var array
+     */
+    protected $_transferEncodingAvailable = [
+        '7bit',
+        '8bit',
+        'base64',
+        'binary',
+        'quoted-printable'
+    ];
+
+    /**
      * The application wide charset, used to encode headers and body
      *
      * @var string|null
@@ -843,6 +856,15 @@ class Email implements JsonSerializable, Serializable
      */
     public function setTransferEncoding($encoding)
     {
+        $encoding = strtolower($encoding);
+        if (!in_array($encoding, $this->_transferEncodingAvailable)) {
+            throw new InvalidArgumentException(
+                sprintf(
+                    'Transfer encoding not available. Can be : %s.',
+                    implode(', ', $this->_transferEncodingAvailable)
+                )
+            );
+        }
         $this->transferEncoding = $encoding;
 
         return $this;

+ 4 - 0
tests/TestCase/Mailer/EmailTest.php

@@ -2515,6 +2515,10 @@ class EmailTest extends TestCase
         $this->Email->reset();
         $this->assertNull($this->Email->getTransferEncoding());
         $this->assertSame($expected, $this->Email->getContentTransferEncoding());
+
+        //Test wrong encoding
+        $this->expectException(\InvalidArgumentException::class);
+        $this->Email->setTransferEncoding('invalid');
     }
 
     /**