3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /** Converts and HTML message into plaintext.
24 * @param $html the HTML code to convert
26 function html2plain($html) {
27 $text = html_entity_decode($html);
28 return trim(strip_tags($text));
32 /** Converts a plaintext message into an HTML message with clickable hyperlinks.
34 * @param $text the plain text to convert
36 function plain2html($text) {
37 $html = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
38 "<a href=\"\\0\">\\0</a>", $text);
40 return "<HTML><BODY>\n$html\n</BODY></HTML>";
44 /** Class for sending inline or multipart-emails.
46 class DiogenesMailer
{
47 /** The header of the email. */
49 /** The body of the email. */
51 /** The sender of the email. */
53 /** The recipient of the email. */
55 /** Carbon copy for the email. */
57 /** Blind carbon copy for the email. */
59 /** Subject of the email. */
61 /** The boundary used to separate the email's body parts. */
63 /** Do we have a "From:" header? If none is explicitly set, just before
64 * sending it will be constructed from the sender's email address. */
68 /** The constructor. Initialises header & body.
70 * @param from the sender of the email
71 * @param to the recipient of the email
72 * @param subject the subject of the email
73 * @param multipart boolean indicating whether we have a multipart email
74 * @param cc carbon copy for the email
75 * @param bcc blind carbon copy for the email
77 function DiogenesMailer($from, $to, $subject, $multipart=false
, $cc="", $bcc="") {
78 trigger_error("DiogenesMailer class is obsolete, use HermesMailer instead !", E_USER_NOTICE
);
80 $this->from_present
= false
;
81 $this->to
= ($to == '' ?
$from : $to);
84 $this->subject
= $subject;
86 $this->header
= "X-Mailer: PHP/" . phpversion()."\n".
87 "Mime-Version: 1.0\n";
89 $this->boundary
="-=partie_suivante_(".uniqid("").")=-";
91 "Content-Type: multipart/alternative;\n".
92 " boundary=\"{$this->boundary}\"\n";
97 "Content-Type: text/plain; charset=iso-8859-1\n".
98 "Content-Disposition: inline\n".
99 "Content-Transfer-Encoding: 8bit\n";
104 /** Adds a part to the email's body.
106 * @param type the MIME type of this part (e.g. "text/plain; charset=iso-8859-1")
107 * @param encoding the content encoding for this part (e.g "8bit")
108 * @param value the contents of this part
110 function addPart($type,$encoding,$value)
112 if ($this->boundary
) {
114 "--{$this->boundary}\n".
115 "Content-Type: $type\n".
116 "Content-Transfer-Encoding: $encoding\n\n";
117 $this->body
.= "$value\n";
119 echo "<b>Erreur : addPart s'applique uniquement aux messages multipart!</b>";
124 /** Adds a header to the email.
126 * @param text the contents of the header (without the final line feed)
128 function addHeader($text)
130 if (preg_match('/^From:/i', $text)) $this->from_present
= true
;
131 $this->header
.= "$text\n";
135 /** Adds a "text/plain" part to the email.
139 function addPartText($text)
141 $this->addPart("text/plain; charset=iso-8859-1",
146 /** Adds a "text/html" part to the email.
150 function addPartHtml($html)
152 $this->addPart("text/html; charset=iso-8859-1",
157 /** Sets the body of the email (only for inline messages!).
161 function setBody($text)
163 if (!$this->boundary
) {
166 die("Error : setBody only applies to inline messages!");
171 /** Sends the email using a pipe to sendmail.
175 if(!$this->from_present
)
176 $this->header
.= "From: {$this->from}\n";
178 $this->header
.= "To: {$this->to}\n";
180 $this->header
.= "Cc: {$this->cc}\n";
182 $this->header
.= "Subject: {$this->subject}\n";
183 $this->header
.= "\n";
186 $this->body
.= "--{$this->boundary}--\n";
188 $fp = popen('/usr/sbin/sendmail -oi -f '.escapeshellarg($this->from
).' '.escapeshellarg($this->to
).' '.escapeshellarg($this->cc
).' '.escapeshellarg($this->bcc
),'w');
190 if(fwrite($fp, $this->header
) == -1) return false
;
191 if(fwrite($fp, $this->body
) == -1) return false
;
192 if (pclose($fp) == 0) return true
;