noalyss Version-9
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
49 function __construct()
50 {
51 }
52
53 /**
54 * set the from
55 * @param $p_from has the form name <info@phpcompta.eu>
56 */
57 function set_from($p_from)
58 {
59 $this->from = $p_from;
60 }
61
62 /**
63 *
64 * @param $p_subject set the subject
65 */
66 function set_subject($p_subject)
67 {
68 $this->subject = $p_subject;
69 }
70
71 /**
72 * set the recipient
73 * @param type $p_mailto has the form name <email@email.com>
74 */
75 function mailto($p_mailto)
76 {
77 $this->mailto = $p_mailto;
78 }
79
80 /**
81 * @brief body of the message (utf8)
82 * @param type $p_message
83 */
84 function set_message($p_message)
85 {
86 // $this->message =wordwrap($p_message,70,"\r\n");
87 $this->message =$p_message;
88 }
89
90 /**
91 *@brief Add file to the message
92 * @param FileToSend $file file to add to the message
93 */
95 {
96 $this->afile[] = $file;
97 }
98
99 /**
100 * @brief verify that the message is ready to go
101 * @throws Exception
102 */
103 function verify()
104 {
105 $array = explode(",", "from,subject,mailto,message");
106 for ($i = 0; $i < count($array); $i++)
107 {
108 $name = $array[$i];
109 if (trim($this->$name??"") == "")
110 {
111 throw new Exception( sprintf(_("%s est vide"),$name),EXC_INVALID);
112 }
113 }
114 }
115
116 /**
117 *
118 * @brief Function to override if supplemental header are needed
119 * @return string
120 */
122 {
123 return '';
124 }
125 /**
126 *@brief create the message before sending
127 */
128 function compose()
129 {
130 $this->verify();
131 $this->header="";
132 $this->content="";
133
134 // a random hash will be necessary to send mixed content
135 $separator = md5(time());
136
137 // carriage return type (we use a PHP end of line constant)
138 $eol = PHP_EOL;
139
140 // main header (multipart mandatory)
141 $this->header = "From: " . $this->from . $eol;
142 $this->header .= "MIME-Version: 1.0" . $eol;
143 $this->header .= $this->add_supplemental_header();
144 $this->header .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" ;
145
146 // message
147 $this->content .= "--" . $separator . $eol;
148 $this->content .= "Content-Type: text/plain; charset=\"utf-8\"" . $eol;
149 $this->content .= "Content-Transfer-Encoding: 7bit" . $eol.$eol ;
150 $this->content .= $this->message . $eol ;
151 if ( ! empty($this->afile ) )
152 {
153 // attachment
154 for ($i = 0; $i < count($this->afile); $i++)
155 {
156
157 $file = $this->afile[$i];
158 $file_size = filesize($file->full_name);
159 $handle = fopen($file->full_name, "r");
160 if ( $handle == false ){
161 \record_log("SC159 ".var_export($file,true));
162 throw new Exception ('SC159 email not send file not added'.$file->full_name);
163 }
164 $content = fread($handle, $file_size);
165 fclose($handle);
166 $content = chunk_split(base64_encode($content));
167 $this->content .= "--" . $separator . $eol;
168 $this->content .= "Content-Type: " . $file->type . "; name=\"" . $file->filename . "\"" . $eol;
169 $this->content .= "Content-Disposition: attachment; filename=\"" . $file->filename . "\"" . $eol;
170 $this->content .= "Content-Transfer-Encoding: base64" . $eol;
171 $this->content.=$eol;
172 $this->content .= $content . $eol ;
173 }
174 }
175 if ( empty ($this->afile) ) $this->content.=$eol;
176
177 $this->content .= "--" . $separator . "--";
178 }
179
180 /**
181 *@brief Send email
182 * @throws Exception
183 */
184 function send()
185 {
186 try {
187 $this->verify();
188
189 } catch (Exception $e) {
190 throw $e;
191 }
192
193 if (!mail($this->mailto, $this->subject, $this->content,$this->header))
194 {
195 throw new Exception('send failed');
196 }
197 }
198}
record_log($p_message)
Record an error message into the log file of the server.
Definition: ac_common.php:1342
$anc_grandlivre from
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
const EXC_INVALID
Definition: constant.php:346