Email - HTML
Fix email mimetype for attachment
This commit is contained in:
parent
c760989646
commit
2e82aac707
3 changed files with 169 additions and 13 deletions
|
|
@ -19,10 +19,14 @@
|
||||||
*/
|
*/
|
||||||
// Copyright Author Dany De Bontridder danydb@aevalys.eu
|
// 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
|
||||||
*/
|
*/
|
||||||
/*! \brief Class Document corresponds to the table document
|
/*!
|
||||||
|
* \class
|
||||||
|
* \brief
|
||||||
|
* Class Document corresponds to the table DOCUMENT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Document
|
class Document
|
||||||
|
|
@ -38,7 +42,7 @@ class Document
|
||||||
var $d_number; /*!< $d_number number of the document */
|
var $d_number; /*!< $d_number number of the document */
|
||||||
var $md_id; /*!< $md_id document's template */
|
var $md_id; /*!< $md_id document's template */
|
||||||
var $f_id; /*!< fiche.f_id */
|
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 $d_name; /*!< document name */
|
||||||
var $md_type ; /*!< Type of document */
|
var $md_type ; /*!< Type of document */
|
||||||
/*!
|
/*!
|
||||||
|
|
|
||||||
|
|
@ -45,12 +45,29 @@ class Sendmail_Core
|
||||||
protected $from;
|
protected $from;
|
||||||
protected $content;
|
protected $content;
|
||||||
protected $header;
|
protected $header;
|
||||||
|
protected $format;
|
||||||
function __construct()
|
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
|
* set the from
|
||||||
* @param $p_from has the form name <info@phpcompta.eu>
|
* @param $p_from has the form name <info@phpcompta.eu>
|
||||||
*/
|
*/
|
||||||
|
|
@ -141,13 +158,53 @@ class Sendmail_Core
|
||||||
$this->header = "From: " . $this->from . $eol;
|
$this->header = "From: " . $this->from . $eol;
|
||||||
$this->header .= "MIME-Version: 1.0" . $eol;
|
$this->header .= "MIME-Version: 1.0" . $eol;
|
||||||
$this->header .= $this->add_supplemental_header();
|
$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 .= "Content-Transfer-Encoding: 8bit" . $eol.$eol ;
|
||||||
$this->content .= "--" . $separator . $eol;
|
$this->content .= $this->message . $eol ;
|
||||||
$this->content .= "Content-Type: text/plain; charset=\"utf-8\"" . $eol;
|
} elseif ($this->format == 'HTML') {
|
||||||
$this->content .= "Content-Transfer-Encoding: 7bit" . $eol.$eol ;
|
|
||||||
$this->content .= $this->message . $eol ;
|
$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}
|
||||||
|
|
||||||
|
<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 ) )
|
if ( ! empty($this->afile ) )
|
||||||
{
|
{
|
||||||
// attachment
|
// attachment
|
||||||
|
|
@ -156,6 +213,7 @@ class Sendmail_Core
|
||||||
|
|
||||||
$file = $this->afile[$i];
|
$file = $this->afile[$i];
|
||||||
$file_size = filesize($file->full_name);
|
$file_size = filesize($file->full_name);
|
||||||
|
$mimetype= mime_content_type($file->full_name);
|
||||||
$handle = fopen($file->full_name, "r");
|
$handle = fopen($file->full_name, "r");
|
||||||
if ( $handle == false ){
|
if ( $handle == false ){
|
||||||
\record_log("SC159 ".var_export($file,true));
|
\record_log("SC159 ".var_export($file,true));
|
||||||
|
|
@ -165,7 +223,7 @@ class Sendmail_Core
|
||||||
fclose($handle);
|
fclose($handle);
|
||||||
$content = chunk_split(base64_encode($content));
|
$content = chunk_split(base64_encode($content));
|
||||||
$this->content .= "--" . $separator . $eol;
|
$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-Disposition: attachment; filename=\"" . $file->filename . "\"" . $eol;
|
||||||
$this->content .= "Content-Transfer-Encoding: base64" . $eol;
|
$this->content .= "Content-Transfer-Encoding: base64" . $eol;
|
||||||
$this->content.=$eol;
|
$this->content.=$eol;
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,8 @@ use PHPUnit\Framework\TestCase;
|
||||||
require DIRTEST . '/global.php';
|
require DIRTEST . '/global.php';
|
||||||
|
|
||||||
define ("ALLOWED_EMAIL_DOMAIN","linux.org,localhost");
|
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 ...
|
* @testdox Class SendmailTest : used for ...
|
||||||
* @backupGlobals enabled
|
* @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'");
|
$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);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue