noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
sendmail_core.class.php
Go to the documentation of this file.
1<?php
2/*
3 * This file is part of NOALYSS.
4 *
5 * NOALYSS is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * NOALYSS is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NOALYSS; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19// Copyright Author Dany De Bontridder danydb@noalyss.eu
20
21/**
22 *@file
23 *@brief API for sending email
24 */
25
26
27/**
28 *@class Sendmail_Core
29 *@brief API for sending email
30 */
31
32/**
33 * Description of Sendmail
34 *
35 * @author dany
36 */
37
39{
40
41 protected $mailto;
42 protected $afile;
43 protected $subject;
44 protected $message;
45 protected $from;
46 protected $content;
47 protected $header;
48 protected $format;
49 function __construct()
50 {
51 $this->format='PLAIN';
52 }
53
54 public function get_format() {
55 return $this->format;
56 }
57 /**
58 * @brief format is either HTML or PLAIN
59 * @param type $format HTML or PLAIN
60 * @return Sendmail_Core
61 */
62 public function set_format($format) {
63 if ( in_array($format,['PLAIN','HTML'] ) == false) {
64 throw new \Exception('SC64 : unknow format ');
65 }
66 $this->format = $format;
67 return $this;
68 }
69
70 /**
71 * set the from
72 * @param $p_from has the form name <info@phpcompta.eu>
73 */
74 function set_from($p_from)
75 {
76 $this->from = $p_from;
77 }
78
79 /**
80 *
81 * @param $p_subject set the subject
82 */
83 function set_subject($p_subject)
84 {
85 $this->subject = $p_subject;
86 }
87
88 /**
89 * set the recipient
90 * @param type $p_mailto has the form name <email@email.com>
91 */
92 function mailto($p_mailto)
93 {
94 $this->mailto = $p_mailto;
95 }
96
97 /**
98 * @brief body of the message (utf8)
99 * @param type $p_message
100 */
101 function set_message($p_message)
102 {
103 // $this->message =wordwrap($p_message,70,"\r\n");
104 $this->message =$p_message;
105 }
106
107 /**
108 *@brief Add file to the message
109 * @param FileToSend $file file to add to the message
110 */
112 {
113 $this->afile[] = $file;
114 }
115
116 /**
117 * @brief verify that the message is ready to go
118 * @throws Exception
119 */
120 function verify()
121 {
122 $array = explode(",", "from,subject,mailto,message");
123 for ($i = 0; $i < count($array); $i++)
124 {
125 $name = $array[$i];
126 if (trim($this->$name??"") == "")
127 {
128 throw new Exception( sprintf(_("%s est vide"),$name),EXC_INVALID);
129 }
130 }
131 }
132
133 /**
134 *
135 * @brief Function to override if supplemental header are needed
136 * @return string
137 */
139 {
140 return '';
141 }
142 /**
143 *@brief create the message before sending
144 */
145 function compose()
146 {
147 $this->verify();
148 $this->header="";
149 $this->content="";
150
151 // a random hash will be necessary to send mixed content
152 $separator = md5(time());
153
154 // carriage return type (we use a PHP end of line constant)
155 $eol = PHP_EOL;
156
157 // main header (multipart mandatory)
158 $this->header = "From: " . $this->from . $eol;
159 $this->header .= "MIME-Version: 1.0" . $eol;
160 $this->header .= $this->add_supplemental_header();
161 if ($this->format == 'PLAIN')
162 {
163 $this->header .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" ;
164 // message PLAIN
165 $this->content .= "--" . $separator . $eol;
166 $this->content .= "Content-Type: text/plain; charset=UTF-8" . $eol;
167 $this->content .= "Content-Transfer-Encoding: 8bit" . $eol.$eol ;
168 $this->content .= $this->message . $eol ;
169 } elseif ($this->format == 'HTML') {
170 $separator_second=md5(rand());
171
172 $this->header .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" .$eol ;
173 // message PLAIN
174 $this->content .= "--" . $separator . $eol;
175 $this->content .= "Content-Type: multipart/alternative; boundary=\"" . $separator_second . "\"".$eol ;
176 $this->content.= "--$separator_second".$eol;
177 $this->content .= "Content-Type: text/plain; charset=UTF-8; format=flowed".$eol;
178 $this->content .= "Content-Transfer-Encoding: 8bit" . $eol.$eol ;
179 $this->content .= strip_tags($this->message) . $eol ;
180 $this->content.=$eol;
181 $this->content.=$eol;
182 $this->content.=$eol;
183 // message HTML
184 $this->content .= "--" . $separator_second.$eol;
185 $this->content .= "Content-Type: text/html; charset=UTF-8" . $eol;
186 $this->content .= "Content-Transfer-Encoding: 8bit" . $eol.$eol ;
187 $this->content .=<<<eof
188<!DOCTYPE html>{$eol}
189<html>{$eol}
190<head>{$eol}
191
192<meta http-equiv="content-type" content="text/html; charset=UTF-8">{$eol}
193</head>{$eol}
194<body>
195{$eol}
196{$eol}
197{$eol}
198eof;
199 $this->content .= $this->message. $eol ;
200 $this->content .=" </body> </html>".$eol;
201 $this->content .= "--" . $separator_second."--" . $eol;
202
203 }else {
204 throw new \Exception('SC172 : unknow format ');
205 }
206
207 if ( ! empty($this->afile ) )
208 {
209 // attachment
210 for ($i = 0; $i < count($this->afile); $i++)
211 {
212
213 $file = $this->afile[$i];
214 $file_size = filesize($file->full_name);
215 $mimetype=( $file->type=="")?mime_content_type($file->full_name):$file->type;
216 $handle = fopen($file->full_name, "r");
217 if ( $handle == false ){
218 \record_log("SC159 ".var_export($file,true));
219 throw new Exception ('SC159 email not send file not added'.$file->full_name);
220 }
221 $content = fread($handle, $file_size);
222 fclose($handle);
223 $content = chunk_split(base64_encode($content));
224 $this->content .= "--" . $separator . $eol;
225 $this->content .= "Content-Type: " . $mimetype . "; name=\"" . $file->filename . "\"" . $eol;
226 $this->content .= "Content-Disposition: attachment; filename=\"" . $file->filename . "\"" . $eol;
227 $this->content .= "Content-Transfer-Encoding: base64" . $eol;
228 $this->content.=$eol;
229 $this->content .= $content . $eol ;
230 }
231 }
232 if ( empty ($this->afile) ) $this->content.=$eol;
233
234 $this->content .= "--" . $separator . "--";
235 }
236
237 /**
238 *@brief Send email
239 * @throws Exception
240 */
241 function send()
242 {
243 try {
244 $this->verify();
245
246 } catch (Exception $e) {
247 throw $e;
248 }
249
250 if (!mail($this->mailto, $this->subject, $this->content,$this->header))
251 {
252 throw new Exception('send failed');
253 }
254 }
255}
record_log($p_message)
Record an error message into the log file of the server or in the log folder of NOALYSS Record also t...
$anc_grandlivre from
_("actif, passif,charge,...")
else $card content[$j]['j_montant']
file to add to a message
Description of Sendmail.
compose()
create the message before sending
add_supplemental_header()
Function to override if supplemental header are needed.
mailto($p_mailto)
set the recipient
verify()
verify that the message is ready to go
set_subject($p_subject)
add_file(FileToSend $file)
Add file to the message.
set_message($p_message)
body of the message (utf8)
set_from($p_from)
set the from
set_format($format)
format is either HTML or PLAIN
const EXC_INVALID
Definition constant.php:358
if( $delta< 0) elseif( $delta==0)