Browse Source

Merge pull request #11488 from LifeOrYou/issue-11485

Add email config for transferEncoding #11485
Mark Story 8 years ago
parent
commit
e7f5da521b
2 changed files with 94 additions and 1 deletions
  1. 60 1
      src/Mailer/Email.php
  2. 34 0
      tests/TestCase/Mailer/EmailTest.php

+ 60 - 1
src/Mailer/Email.php

@@ -242,6 +242,27 @@ class Email implements JsonSerializable, Serializable
     public $headerCharset;
 
     /**
+     * The email transfer encoding used.
+     * If null, the $charset property is used for determined the transfer encoding.
+     *
+     * @var string|null
+     */
+    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
@@ -828,6 +849,38 @@ class Email implements JsonSerializable, Serializable
     }
 
     /**
+     * TransferEncoding setter.
+     *
+     * @param string|null $encoding Encoding set.
+     * @return $this
+     */
+    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;
+    }
+
+    /**
+     * TransferEncoding getter.
+     *
+     * @return string|null Encoding
+     */
+    public function getTransferEncoding()
+    {
+        return $this->transferEncoding;
+    }
+
+    /**
      * EmailPattern setter/getter
      *
      * @param string|null $regex The pattern to use for email address validation,
@@ -2224,6 +2277,7 @@ class Email implements JsonSerializable, Serializable
         $this->_priority = null;
         $this->charset = 'utf-8';
         $this->headerCharset = null;
+        $this->transferEncoding = null;
         $this->_attachments = [];
         $this->_profile = [];
         $this->_emailPattern = self::EMAIL_PATTERN;
@@ -2655,12 +2709,17 @@ class Email implements JsonSerializable, Serializable
     }
 
     /**
-     * Return the Content-Transfer Encoding value based on the set charset
+     * Return the Content-Transfer Encoding value based
+     * on the set transferEncoding or set charset.
      *
      * @return string
      */
     protected function _getContentTransferEncoding()
     {
+        if ($this->transferEncoding) {
+            return $this->transferEncoding;
+        }
+
         $charset = strtoupper($this->charset);
         if (in_array($charset, $this->_charset8bit)) {
             return '8bit';

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

@@ -90,6 +90,16 @@ class TestEmail extends Email
     {
         return $this->_render($content);
     }
+
+    /**
+     * GetContentTransferEncoding to protected method
+     *
+     * @return string
+     */
+    public function getContentTransferEncoding()
+    {
+        return $this->_getContentTransferEncoding();
+    }
 }
 
 /**
@@ -2488,6 +2498,30 @@ class EmailTest extends TestCase
     }
 
     /**
+     * Test transferEncoding
+     *
+     * @return void
+     */
+    public function testTransferEncoding()
+    {
+        // Test new transfer encoding
+        $expected = 'quoted-printable';
+        $this->Email->setTransferEncoding($expected);
+        $this->assertSame($expected, $this->Email->getTransferEncoding());
+        $this->assertSame($expected, $this->Email->getContentTransferEncoding());
+
+        // Test default charset/encoding : utf8/8bit
+        $expected = '8bit';
+        $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');
+    }
+
+    /**
      * Tests for compatible check.
      *          charset property and       charset() method.
      *    headerCharset property and headerCharset() method.