altocompta/include/lib/sendmail_core.class.php
sparkyx 60bf53262e Bug 2458: Si on envoie un email depuis Linux ,
le return-path doit être spécifié
2026-04-01 17:21:50 +02:00

286 lines
8.6 KiB
PHP

<?php
/*
* This file is part of NOALYSS.
*
* NOALYSS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* NOALYSS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NOALYSS; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Copyright Author Dany De Bontridder danydb@noalyss.eu
/**
*@file
*@brief API for sending email
*/
/**
*@class Sendmail_Core
*@brief API for sending email
*/
/**
* Description of Sendmail
*
* @author dany
*/
class Sendmail_Core
{
protected $mailto;
protected $afile;
protected $subject;
protected $message;
protected $from;
protected $content;
protected $header;
protected $format;
protected $supplemental_header; //!< $supplemental_header(string) supplemental header to add
protected $supplemental_param; //!< $supplemental_param (string) 5th parameter for mail()
// for postfix, it should be "-f {$this->from}" for the Return-Path
function __construct()
{
$this->format='PLAIN';
$this->supplemental_header="";
$this->supplemental_param=MAIL_EXTRA_PARAM;
}
public function getSupplemental_param()
{
return $this->supplemental_param;
}
public function setSupplemental_param($supplemental_param)
{
$this->supplemental_param = $supplemental_param;
return $this;
}
public function getSupplemental_header()
{
return $this->supplemental_header;
}
public function setSupplemental_header($supplemental_header)
{
$this->supplemental_header = $supplemental_header;
return $this;
}
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($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>
*/
function set_from($p_from)
{
$this->from = $p_from;
}
/**
*
* @param $p_subject set the subject
*/
function set_subject($p_subject)
{
$this->subject = $p_subject;
}
/**
* set the recipient
* @param type $p_mailto has the form name <email@email.com>
*/
function mailto($p_mailto)
{
$this->mailto = $p_mailto;
}
/**
* @brief body of the message (utf8)
* @param type $p_message
*/
function set_message($p_message)
{
// $this->message =wordwrap($p_message,70,"\r\n");
$this->message =$p_message;
}
/**
*@brief Add file to the message
* @param FileToSend $file file to add to the message
*/
function add_file(FileToSend $file)
{
$this->afile[] = $file;
}
/**
* @brief verify that the message is ready to go
* @throws Exception
*/
function verify()
{
$array = explode(",", "from,subject,mailto,message");
for ($i = 0; $i < count($array); $i++)
{
$name = $array[$i];
if (trim($this->$name??"") == "")
{
throw new Exception( sprintf(_("%s est vide"),$name),EXC_INVALID);
}
}
}
/**
*
* @brief Function to override if supplemental header are needed
* @return string
*/
function add_supplemental_header()
{
return $this->supplemental_header;
}
/**
*@brief create the message before sending
*/
function compose()
{
$this->verify();
$this->header="";
$this->content="";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// main header (multipart mandatory)
$this->header = "From: " . $this->from . $eol;
$this->header .= "Reply-To: " . $this->from . $eol;
$this->header .= "MIME-Version: 1.0" . $eol;
$this->header .= $this->add_supplemental_header();
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') {
$separator_second=md5(rand());
$this->header .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" .$eol ;
// message PLAIN
$this->content .= "--" . $separator . $eol;
$this->content .= "Content-Type: multipart/alternative; boundary=\"" . $separator_second . "\"".$eol ;
$this->content.= "--$separator_second".$eol;
$this->content .= "Content-Type: text/plain; charset=UTF-8; format=flowed".$eol;
$this->content .= "Content-Transfer-Encoding: 8bit" . $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 ) )
{
// attachment
for ($i = 0; $i < count($this->afile); $i++)
{
$file = $this->afile[$i];
$file_size = filesize($file->full_name);
$mimetype=( $file->type=="")?mime_content_type($file->full_name):$file->type;
$handle = fopen($file->full_name, "r");
if ( $handle == false ){
\record_log("SC159 ".var_export($file,true));
throw new Exception ('SC159 email not send file not added'.$file->full_name);
}
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$this->content .= "--" . $separator . $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;
$this->content .= $content . $eol ;
}
}
if ( empty ($this->afile) ) $this->content.=$eol;
$this->content .= "--" . $separator . "--";
$this->supplemental_param= str_replace("[FROM]", $this->from, $this->supplemental_param);
}
/**
*@brief Send email
* @throws Exception
*/
function send()
{
try {
$this->verify();
} catch (Exception $e) {
throw $e;
}
$encoded_subject = mb_encode_mimeheader( $this->subject, 'UTF-8', 'B');
if (!mail($this->mailto,$encoded_subject, $this->content,$this->header,$this->supplemental_param))
{
throw new Exception('send failed');
}
}
}