From 2e82aac707a2c80391c417f9e4fb1af391466d22 Mon Sep 17 00:00:00 2001 From: sparkyx Date: Mon, 1 Sep 2025 10:54:41 +0200 Subject: [PATCH] Email - HTML Fix email mimetype for attachment --- include/class/document.class.php | 10 ++- include/lib/sendmail_core.class.php | 76 ++++++++++++++++--- unit-test/include/class/sendmailTest.php | 96 +++++++++++++++++++++++- 3 files changed, 169 insertions(+), 13 deletions(-) diff --git a/include/class/document.class.php b/include/class/document.class.php index 815e20fcb..cb8a57a73 100644 --- a/include/class/document.class.php +++ b/include/class/document.class.php @@ -19,10 +19,14 @@ */ // Copyright Author Dany De Bontridder danydb@aevalys.eu -/*! \file +/*! + * \file * \brief Class Document corresponds to the table document */ -/*! \brief Class Document corresponds to the table document +/*! + * \class + * \brief + * Class Document corresponds to the table DOCUMENT */ class Document @@ -38,7 +42,7 @@ class Document var $d_number; /*!< $d_number number of the document */ var $md_id; /*!< $md_id document's template */ var $f_id; /*!< fiche.f_id */ - private $counter; /*!< counter for the items ( goods ) */ + protected $counter; /*!< counter for the items ( goods ) */ var $d_name; /*!< document name */ var $md_type ; /*!< Type of document */ /*! diff --git a/include/lib/sendmail_core.class.php b/include/lib/sendmail_core.class.php index 4620adbde..88b9862c4 100644 --- a/include/lib/sendmail_core.class.php +++ b/include/lib/sendmail_core.class.php @@ -45,12 +45,29 @@ class Sendmail_Core protected $from; protected $content; protected $header; - + protected $format; function __construct() { + $this->format='PLAIN'; + } + + public function get_format() { + return $this->format; + } + /** + * @brief format is either HTML or PLAIN + * @param type $format HTML or PLAIN + * @return Sendmail_Core + */ + public function set_format($format) { + if ( in_array($this->format,['PLAIN','HTML'] ) == false) { + throw new \Exception('SC64 : unknow format '); + } + $this->format = $format; + return $this; } - /** + /** * set the from * @param $p_from has the form name */ @@ -141,13 +158,53 @@ class Sendmail_Core $this->header = "From: " . $this->from . $eol; $this->header .= "MIME-Version: 1.0" . $eol; $this->header .= $this->add_supplemental_header(); - $this->header .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" ; + if ($this->format == 'PLAIN') + { + $this->header .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" ; + // message PLAIN + $this->content .= "--" . $separator . $eol; + $this->content .= "Content-Type: text/plain; charset=UTF-8" . $eol; - // message - $this->content .= "--" . $separator . $eol; - $this->content .= "Content-Type: text/plain; charset=\"utf-8\"" . $eol; - $this->content .= "Content-Transfer-Encoding: 7bit" . $eol.$eol ; - $this->content .= $this->message . $eol ; + $this->content .= "Content-Transfer-Encoding: 8bit" . $eol.$eol ; + $this->content .= $this->message . $eol ; + } elseif ($this->format == 'HTML') { + + $this->header .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" .$eol ; + // message PLAIN + $this->content .= "--" . $separator . $eol; + $separator_second=md5(rand()); + $this->content .= "Content-Type: multipart/alternative; boundary=\"" . $separator_second . "\"".$eol ; + $this->content .= "Content-Type: text/plain; charset=UTF-8; format=flowed".$eol; + $this->content .= "Content-Transfer-Encoding: 7bit" . $eol.$eol ; + $this->content .= strip_tags($this->message) . $eol ; + $this->content.=$eol; + $this->content.=$eol; + $this->content.=$eol; + // message HTML + $this->content .= "--" . $separator_second.$eol; + $this->content .= "Content-Type: text/html; charset=UTF-8" . $eol; + $this->content .= "Content-Transfer-Encoding: 8bit" . $eol.$eol ; + $this->content .=<<{$eol} +{$eol} +{$eol} + +{$eol} +{$eol} + +{$eol} +{$eol} +{$eol} +eof; + $this->content .= $this->message. $eol ; + $this->content .=" ".$eol; + $this->content .= "--" . $separator_second."--" . $eol; + + + }else { + throw new \Exception('SC172 : unknow format '); + } + if ( ! empty($this->afile ) ) { // attachment @@ -156,6 +213,7 @@ class Sendmail_Core $file = $this->afile[$i]; $file_size = filesize($file->full_name); + $mimetype= mime_content_type($file->full_name); $handle = fopen($file->full_name, "r"); if ( $handle == false ){ \record_log("SC159 ".var_export($file,true)); @@ -165,7 +223,7 @@ class Sendmail_Core fclose($handle); $content = chunk_split(base64_encode($content)); $this->content .= "--" . $separator . $eol; - $this->content .= "Content-Type: " . $file->type . "; name=\"" . $file->filename . "\"" . $eol; + $this->content .= "Content-Type: " . $mimetype . "; name=\"" . $file->filename . "\"" . $eol; $this->content .= "Content-Disposition: attachment; filename=\"" . $file->filename . "\"" . $eol; $this->content .= "Content-Transfer-Encoding: base64" . $eol; $this->content.=$eol; diff --git a/unit-test/include/class/sendmailTest.php b/unit-test/include/class/sendmailTest.php index 1b327c82f..aa2f10758 100644 --- a/unit-test/include/class/sendmailTest.php +++ b/unit-test/include/class/sendmailTest.php @@ -31,7 +31,8 @@ use PHPUnit\Framework\TestCase; require DIRTEST . '/global.php'; define ("ALLOWED_EMAIL_DOMAIN","linux.org,localhost"); - +// change it by your own email +define('EMAIL_RECIPIENT','dany@galactee.intra-alchimerys.be'); /** * @testdox Class SendmailTest : used for ... * @backupGlobals enabled @@ -184,4 +185,97 @@ class SendmailTest extends TestCase $cn->exec_sql("delete from dossier_sent_email where dos_id=$dos_id and de_date='20100101'"); } + /** + *@testdox send email in PLAIN Text + */ + function testEmailPLAIN() + { + + $sendmail=new \Sendmail(); + $sendmail->mailto(EMAIL_RECIPIENT); + $sendmail->set_from("dany@localhost"); + $sendmail->set_subject("Test envoi PLAIN ".date('d.m.Y h:i')); + $sendmail->set_format('PLAIN'); + $sendmail->set_message("corps du message + +Shouldéré pariaturà turçhducken labore esse. Pancetta burgdoggen ground round nulla commodo cillum rump occaecat leberkas pork loin dolore buffalo tempor. Voluptate flank veniam deserunt chicken, buffalo kielbasa adipisicing short ribs venison non ullamco enim fatback. Ham pariatur lorem turducken consequat. Chicken burgdoggen doner ground round rump officia kielbasa eiusmod meatball, sed jerky mollit. Ut kielbasa t-bone ipsum ex esse, andouille porchetta consectetur do quis ut. Ea andouille sausage, ut lorem ball tip salami esse. + +Consectetur ut cow, non in ipsum brisket dolore short loin. Burgdoggen deserunt sausage, leberkas pork chop cow corned beef. Leberkas brisket dolore, ad ham hock picanha in. Jerky lorem eiusmod meatloaf rump bresaola nostrud andouille. Jowl shank aute, consectetur veniam labore porchetta minim adipisicing. Picanha spare ribs sausage lorem. + + + + + + + + + + + +"); + $sendmail->compose(); + $sendmail->send(); + // if not exception thrown , so it works + $this->assertTrue(true); + } + /** + *@testdox send email in HTML + */ + function testEmailHTML() + { + + $sendmail=new \Sendmail(); + $sendmail->mailto(EMAIL_RECIPIENT); + $sendmail->set_from("dany@localhost"); + $sendmail->set_subject("Test envoi HTML ".date('d.m.Y h:i')); + $sendmail->set_format('HTML'); + $sendmail->set_message("corps du message + +Message en HTML et texte normal + + +

Ceci est un message en HTML

+

GRAS

+

Ceci est un message en HTML

+

ITALIQUE

+

Ceci est un message en HTML

+

COULEUR
+

+

couleur bleue

+ + +"); + $sendmail->compose(); + $sendmail->send(); + // if not exception thrown , so it works + $this->assertTrue(true); + + } + /** + *@testdox send email in HTML no formatting + */ + function testEmailHTML2() + { + + $sendmail=new \Sendmail(); + $sendmail->mailto(EMAIL_RECIPIENT); + $sendmail->set_from("dany@localhost"); + $sendmail->set_subject("Test envoi HTML no format ".date('d.m.Y h:i')); + $sendmail->set_format('HTML'); + $sendmail->set_message(" + Shouldéré pariaturà turçhducken labore esse. Pancetta burgdoggen ground round nulla commodo cillum rump occaecat leberkas pork loin dolore buffalo tempor. Voluptate flank veniam deserunt chicken, buffalo kielbasa adipisicing short ribs venison non ullamco enim fatback. Ham pariatur lorem turducken consequat. Chicken burgdoggen doner ground round rump officia kielbasa eiusmod meatball, sed jerky mollit. Ut kielbasa t-bone ipsum ex esse, andouille porchetta consectetur do quis ut. Ea andouille sausage, ut lorem ball tip salami esse. + +Consectetur ut cow, non in ipsum brisket dolore short loin. Burgdoggen deserunt sausage, leberkas pork chop cow corned beef. Leberkas brisket dolore, ad ham hock picanha in. Jerky lorem eiusmod meatloaf rump bresaola nostrud andouille. Jowl shank aute, consectetur veniam labore porchetta minim adipisicing. Picanha spare ribs sausage lorem. + + + + + +"); + $sendmail->compose(); + $sendmail->send(); + // if not exception thrown , so it works + $this->assertTrue(true); + + } } \ No newline at end of file