FIX Filenames must not contains non ascii char or we will get non ascii

char into the SMTP header.
FIX Do not encode subject if it is full ascii (not required)
This commit is contained in:
Laurent Destailleur 2020-02-06 12:58:02 +01:00
parent 48e6db17bd
commit 05e08b3596
3 changed files with 64 additions and 8 deletions

View File

@ -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)
{

View File

@ -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

View File

@ -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
*