diff options
-rw-r--r-- | core/Mail.php | 26 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/MailTest.php | 31 |
2 files changed, 57 insertions, 0 deletions
diff --git a/core/Mail.php b/core/Mail.php index 454fcd377c..192c5a94e5 100644 --- a/core/Mail.php +++ b/core/Mail.php @@ -127,6 +127,18 @@ class Mail extends Zend_Mail } } + 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 @@ class Mail extends Zend_Mail { 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 0000000000..f112948a42 --- /dev/null +++ b/tests/PHPUnit/Integration/MailTest.php @@ -0,0 +1,31 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Tests\Integration; + +use Piwik\Mail; + +class MailTest extends \PHPUnit_Framework_TestCase +{ + + public function getEmailFilenames() + { + return array( + array('January 3 – 9, 2010', 'January 3 - 9, 2010'), + array('Report <The><< ’s Coves - week January 18 – 24, 2016', 'Report <The><< \'s Coves - week January 18 - 24, 2016'), + ); + } + + /** + * @dataProvider getEmailFilenames + */ + public function test_EmailFilenamesAreSanitised($raw, $expected) + { + $this->assertEquals($expected, Mail::sanitiseString($raw)); + } +} |