Browse Source

Add email config for transferEncoding #11485

LifeOrYou 8 years ago
parent
commit
2023b4ea69
2 changed files with 59 additions and 0 deletions
  1. 34 0
      src/Mailer/Email.php
  2. 25 0
      tests/TestCase/Mailer/EmailTest.php

+ 34 - 0
src/Mailer/Email.php

@@ -242,6 +242,14 @@ 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
+     */
+    public $transferEncoding;
+
+    /**
      * The application wide charset, used to encode headers and body
      *
      * @var string|null
@@ -828,6 +836,27 @@ class Email implements JsonSerializable, Serializable
     }
 
     /**
+     * TransportCharset setter.
+     *
+     * @param string|null $encoding Character set.
+     * @return $this
+     */
+    public function setTransferEncoding($encoding) {
+        $this->transferEncoding = $encoding;
+
+        return $this;
+    }
+
+    /**
+     * TransportCharset 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 +2253,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;
@@ -2661,6 +2691,10 @@ class Email implements JsonSerializable, Serializable
      */
     protected function _getContentTransferEncoding()
     {
+        if ($this->transferEncoding) {
+            return $this->transferEncoding;
+        }
+
         $charset = strtoupper($this->charset);
         if (in_array($charset, $this->_charset8bit)) {
             return '8bit';

+ 25 - 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,21 @@ class EmailTest extends TestCase
     }
 
     /**
+     * Test transferEncoding
+     */
+    public function testTransferEncoding(){
+        // Test new transport encoding
+        $this->Email->setTransferEncoding('quoted-printable');
+        $this->assertSame($this->Email->getTransferEncoding(), 'quoted-printable');
+        $this->assertSame($this->Email->getContentTransferEncoding(), 'quoted-printable');
+
+        // Test default charset/encoding : utf8/8bit
+        $this->Email->reset();
+        $this->assertNull($this->Email->getTransferEncoding());
+        $this->assertSame($this->Email->getContentTransferEncoding(), '8bit');
+    }
+
+    /**
      * Tests for compatible check.
      *          charset property and       charset() method.
      *    headerCharset property and headerCharset() method.