diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php index e0a1fd35092..dc2b00c586c 100644 --- a/htdocs/core/class/CMailFile.class.php +++ b/htdocs/core/class/CMailFile.class.php @@ -82,15 +82,15 @@ class CMailFile public $headers; public $message; /** - * @var array fullfilenames list + * @var array fullfilenames list (full path of filename on file system) */ public $filename_list = array(); /** - * @var array mimetypes of files list + * @var array mimetypes of files list (List of MIME type of attached files) */ public $mimetype_list = array(); /** - * @var array filenames list + * @var array filenames list (List of attached file name in message) */ public $mimefilename_list = array(); @@ -137,7 +137,14 @@ class CMailFile { global $conf, $dolibarr_main_data_root; - $this->subject = $subject; + // Clean values of $mimefilename_list + if (is_array($mimefilename_list)) { + foreach($mimefilename_list as $key => $val) { + $mimefilename_list[$key] = dol_string_unaccent($mimefilename_list[$key]); + } + } + + $this->subject = $subject; $this->addr_to = $to; $this->addr_from = $from; $this->msg = $msg; @@ -156,7 +163,6 @@ class CMailFile $this->mimetype_list = $mimetype_list; $this->mimefilename_list = $mimefilename_list; - // Define this->sendmode $this->sendmode = ''; if ($this->sendcontext == 'emailing' && !empty($conf->global->MAIN_MAIL_SENDMODE_EMAILING) && $conf->global->MAIN_MAIL_SENDMODE_EMAILING != 'default') @@ -323,7 +329,13 @@ class CMailFile $smtps = new SMTPs(); $smtps->setCharSet($conf->file->character_set_client); - $smtps->setSubject($this->encodetorfc2822($subject)); + // Encode subject if required. + $subjecttouse = $subject; + if (! ascii_check($subjecttouse)) { + $subjecttouse = $this->encodetorfc2822($subjecttouse); + } + + $smtps->setSubject($subjecttouse); $smtps->setTO($this->getValidAddress($to, 0, 1)); $smtps->setFrom($this->getValidAddress($from, 0, 1)); $smtps->setTrackId($trackid); @@ -669,8 +681,14 @@ class CMailFile if (!empty($conf->global->MAIN_MAIL_DEBUG)) $this->dump_mail(); - if (!empty($additionnalparam)) $res = mail($dest, $this->encodetorfc2822($this->subject), $this->message, $this->headers, $additionnalparam); - else $res = mail($dest, $this->encodetorfc2822($this->subject), $this->message, $this->headers); + // Encode subject if required. + $subjecttouse = $this->subject; + if (! ascii_check($subjecttouse)) { + $subjecttouse = $this->encodetorfc2822($subjecttouse); + } + + if (!empty($additionnalparam)) $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam); + else $res = mail($dest, $subjecttouse, $this->message, $this->headers); if (!$res) { diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 1c4a55e6c09..da82a81ea83 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -6911,6 +6911,24 @@ function utf8_check($str) return true; } +/** + * Check if a string is in ASCII + * + * @param string $str String to check + * @return boolean True if string is ASCII, False if not (byte value > 0x7F) + */ +function ascii_check($str) +{ + if (function_exists('mb_check_encoding')) { + //if (mb_detect_encoding($str, 'ASCII', true) return false; + if (! mb_check_encoding($str, 'ASCII')) return false; + } else { + if (preg_match('/[^\x00-\x7f]/', $str)) return false; // Contains a byte > 7f + } + + return true; +} + /** * Return a string encoded into OS filesystem encoding. This function is used to define diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index 12126d9cf40..dff6b3a7d06 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -679,6 +679,26 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase $this->assertFalse($result); } + /** + * testDolAsciiCheck + * + * @return void + */ + public function testDolAsciiCheck() + { + // True + $result=ascii_check('azerty'); + $this->assertTrue($result); + + $result=ascii_check('é'); + $this->assertFalse($result); + + $file=dirname(__FILE__).'/textutf8.txt'; + $filecontent=file_get_contents($file); + $result=ascii_check($filecontent); + $this->assertFalse($result); + } + /** * testDolTrunc *