From 7b7b8fda24a3fa8af51c9de1bb3ba6a4464345c5 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 25 Feb 2023 04:05:18 +0100 Subject: [PATCH] Fix sending emails with data:image inside --- htdocs/comm/mailing/card.php | 10 ++++----- htdocs/core/class/CMailFile.class.php | 29 +++++++++++++++++---------- htdocs/langs/en_US/mails.lang | 1 + 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/htdocs/comm/mailing/card.php b/htdocs/comm/mailing/card.php index 5da75c22fce..2ae78628ba3 100644 --- a/htdocs/comm/mailing/card.php +++ b/htdocs/comm/mailing/card.php @@ -201,9 +201,9 @@ if (empty($reshook)) { $thirdpartystatic = new Societe($db); // Loop on each email and send it - $i = 0; + $iforemailloop = 0; - while ($i < $num && $i < $conf->global->MAILING_LIMIT_SENDBYWEB) { + while ($iforemailloop < $num && $iforemailloop < $conf->global->MAILING_LIMIT_SENDBYWEB) { // Here code is common with same loop ino mailing-send.php $res = 1; $now = dol_now(); @@ -366,7 +366,7 @@ if (empty($reshook)) { // Mail successful $nbok++; - dol_syslog("comm/mailing/card.php: ok for #".$i.($mail->error ? ' - '.$mail->error : ''), LOG_DEBUG); + dol_syslog("comm/mailing/card.php: ok for #".$iforemailloop.($mail->error ? ' - '.$mail->error : ''), LOG_DEBUG); $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles"; $sql .= " SET statut=1, date_envoi = '".$db->idate($now)."' WHERE rowid=".((int) $obj->rowid); @@ -405,7 +405,7 @@ if (empty($reshook)) { // Mail failed $nbko++; - dol_syslog("comm/mailing/card.php: error for #".$i.($mail->error ? ' - '.$mail->error : ''), LOG_WARNING); + dol_syslog("comm/mailing/card.php: error for #".$iforemailloop.($mail->error ? ' - '.$mail->error : ''), LOG_WARNING); $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles"; $sql .= " SET statut=-1, error_text='".$db->escape($mail->error)."', date_envoi='".$db->idate($now)."' WHERE rowid=".((int) $obj->rowid); @@ -415,7 +415,7 @@ if (empty($reshook)) { } } - $i++; + $iforemailloop++; } } else { setEventMessages($langs->transnoentitiesnoconv("NoMoreRecipientToSendTo"), null, 'mesgs'); diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php index ecfe968bd02..d6b1a275d87 100644 --- a/htdocs/core/class/CMailFile.class.php +++ b/htdocs/core/class/CMailFile.class.php @@ -255,12 +255,14 @@ class CMailFile $this->html = $msg; $findimg = 0; - if (!empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS)) { + if (!empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS)) { // Off by default // Search into the body for image_types[$ext]; } // cid - $this->html_images[$i]["cid"] = dol_hash(uniqid(time()), 3); // Force md5 hash (does not contains special chars) + $this->html_images[$i]["cid"] = dol_hash($this->html_images[$i]["fullpath"], 'md5'); // Force md5 hash (does not contains special chars) // type $this->html_images[$i]["type"] = 'cidfromurl'; @@ -1800,17 +1802,22 @@ class CMailFile foreach ($matches[1] as $key => $ext) { // We save the image to send in disk $filecontent = $matches[2][$key]; - $cid = 'cid000'.dol_hash($this->html, 'md5'); // The id must not change if image is same + + $cid = 'cid000'.dol_hash($filecontent, 'md5'); // The id must not change if image is same + $destfiletmp = $images_dir.'/'.$cid.'.'.$ext; - $fhandle = @fopen($destfiletmp, 'w'); - if ($fhandle) { - $nbofbyteswrote = fwrite($fhandle, base64_decode($filecontent)); - fclose($fhandle); - @chmod($destfiletmp, octdec($conf->global->MAIN_UMASK)); - } else { - $this->errors[] = "Failed to open file '".$destfiletmp."' for write"; - return -1; + if (!dol_is_file($destfiletmp)) { // If file does not exist yet (this is the case for the first email sent with a data:image inside) + dol_syslog("write the cid file ".$destfiletmp); + $fhandle = @fopen($destfiletmp, 'w'); + if ($fhandle) { + $nbofbyteswrote = fwrite($fhandle, base64_decode($filecontent)); + fclose($fhandle); + @chmod($destfiletmp, octdec($conf->global->MAIN_UMASK)); + } else { + $this->errors[] = "Failed to open file '".$destfiletmp."' for write"; + return -1; + } } if (file_exists($destfiletmp)) { diff --git a/htdocs/langs/en_US/mails.lang b/htdocs/langs/en_US/mails.lang index 4d98f0e7f49..0414854a3f4 100644 --- a/htdocs/langs/en_US/mails.lang +++ b/htdocs/langs/en_US/mails.lang @@ -179,3 +179,4 @@ RecordCreatedByEmailCollector=Record created by the Email Collector %s from emai DefaultBlacklistMailingStatus=Default value for field '%s' when creating a new contact DefaultStatusEmptyMandatory=Empty but mandatory WarningLimitSendByDay=WARNING: The setup or contract of your instance limits your number of emails per day to %s. Trying to send more may result in having your instance slow down or suspended. Please contact your support if you need a higher quota. +NoMoreRecipientToSendTo=No more recipient to send the email to \ No newline at end of file