Fix email mimetype for attachment and email in HTML

This commit is contained in:
sparkyx 2025-09-01 13:29:24 +02:00
parent 00c206cb7e
commit 28c372d74d
2 changed files with 160 additions and 10 deletions

View file

@ -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 <info@phpcompta.eu>
*/
@ -141,13 +158,51 @@ 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;
$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 .=<<<eof
<!DOCTYPE html>{$eol}
<html>{$eol}
<head>{$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 ;
<meta http-equiv="content-type" content="text/html; charset=UTF-8">{$eol}
</head>{$eol}
<body>
{$eol}
{$eol}
{$eol}
eof;
$this->content .= $this->message. $eol ;
$this->content .=" </body> </html>".$eol;
$this->content .= "--" . $separator_second."--" . $eol;
}else {
throw new \Exception('SC172 : unknow format ');
}
if ( ! empty($this->afile ) )
{
// attachment
@ -156,6 +211,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 +221,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;

View file

@ -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
<p>Ceci est un message en HTML</p>
<h2>GRAS</h2>
<p> Ceci est un message en HTML</p>
<h2>ITALIQUE</h2>
<p><i>Ceci est un message en HTML</i></p>
<h2>COULEUR <br>
</h2>
<p style=\"color:blue\">couleur bleue</p>
");
$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);
}
}