From 619ac550706ac4cb3e68f01a6eda60da3d038180 Mon Sep 17 00:00:00 2001 From: mattab Date: Tue, 26 Jan 2016 19:06:26 +1300 Subject: [PATCH 1/2] Replace some characters in Email subject and Email attachments filename Fixes #9631 --- core/Mail.php | 26 +++++++++++++++++++++ tests/PHPUnit/Integration/MailTest.php | 31 ++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 tests/PHPUnit/Integration/MailTest.php diff --git a/core/Mail.php b/core/Mail.php index 454fcd377c8..192c5a94e5c 100644 --- a/core/Mail.php +++ b/core/Mail.php @@ -127,6 +127,18 @@ public function send($transport = null) } } + public function createAttachment($body, $mimeType = null, $disposition = null, $encoding = null, $filename = null) + { + $filename = self::sanitiseString($filename); + return parent::createAttachment($body, $mimeType, $disposition, $encoding, $filename); + } + + public function setSubject($subject) + { + $subject = self::sanitiseString($subject); + return parent::setSubject($subject); + } + /** * @param string $email * @return string @@ -154,4 +166,18 @@ protected function isHostDefinedAndNotLocal($url) { return isset($url['host']) && !Url::isLocalHost($url['host']); } + + /** + * Replaces characters known to appear incorrectly in some email clients + * + * @param $string + * @return mixed + */ + static public function sanitiseString($string) + { + $search = array('–', '’'); + $replace = array('-', '\''); + $string = str_replace($search, $replace, $string); + return $string; + } } diff --git a/tests/PHPUnit/Integration/MailTest.php b/tests/PHPUnit/Integration/MailTest.php new file mode 100644 index 00000000000..f112948a422 --- /dev/null +++ b/tests/PHPUnit/Integration/MailTest.php @@ -0,0 +1,31 @@ +<< ’s Coves - week January 18 – 24, 2016', 'Report << \'s Coves - week January 18 - 24, 2016'), + ); + } + + /** + * @dataProvider getEmailFilenames + */ + public function test_EmailFilenamesAreSanitised($raw, $expected) + { + $this->assertEquals($expected, Mail::sanitiseString($raw)); + } +} From 5a08487b6170bb32cec9ae4acb7c1f94dc599f03 Mon Sep 17 00:00:00 2001 From: mattab Date: Wed, 27 Jan 2016 13:05:56 +1300 Subject: [PATCH 2/2] Removed static method --- core/Mail.php | 6 +++--- tests/PHPUnit/Integration/MailTest.php | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/core/Mail.php b/core/Mail.php index 192c5a94e5c..c6c8623cfd2 100644 --- a/core/Mail.php +++ b/core/Mail.php @@ -129,13 +129,13 @@ public function send($transport = null) public function createAttachment($body, $mimeType = null, $disposition = null, $encoding = null, $filename = null) { - $filename = self::sanitiseString($filename); + $filename = $this->sanitiseString($filename); return parent::createAttachment($body, $mimeType, $disposition, $encoding, $filename); } public function setSubject($subject) { - $subject = self::sanitiseString($subject); + $subject = $this->sanitiseString($subject); return parent::setSubject($subject); } @@ -173,7 +173,7 @@ protected function isHostDefinedAndNotLocal($url) * @param $string * @return mixed */ - static public function sanitiseString($string) + function sanitiseString($string) { $search = array('–', '’'); $replace = array('-', '\''); diff --git a/tests/PHPUnit/Integration/MailTest.php b/tests/PHPUnit/Integration/MailTest.php index f112948a422..876804c52f6 100644 --- a/tests/PHPUnit/Integration/MailTest.php +++ b/tests/PHPUnit/Integration/MailTest.php @@ -26,6 +26,7 @@ public function getEmailFilenames() */ public function test_EmailFilenamesAreSanitised($raw, $expected) { - $this->assertEquals($expected, Mail::sanitiseString($raw)); + $mail = new Mail; + $this->assertEquals($expected, $mail->sanitiseString($raw)); } }