Commit | Line | Data |
---|---|---|
6855525e JL |
1 | <?php |
2 | /* | |
3 | * Copyright (C) 2003-2004 Polytechnique.org | |
4 | * http://opensource.polytechnique.org/ | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | */ | |
20 | ||
21 | ||
22 | /** Converts and HTML message into plaintext. | |
23 | * | |
24 | * @param $html the HTML code to convert | |
25 | */ | |
26 | function html2plain($html) { | |
27 | $text = html_entity_decode($html); | |
28 | return trim(strip_tags($text)); | |
29 | } | |
30 | ||
31 | ||
32 | /** Converts a plaintext message into an HTML message with clickable hyperlinks. | |
33 | * | |
34 | * @param $text the plain text to convert | |
35 | */ | |
36 | function plain2html($text) { | |
37 | $html = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", | |
38 | "<a href=\"\\0\">\\0</a>", $text); | |
39 | $html = nl2br($html); | |
40 | return "<HTML><BODY>\n$html\n</BODY></HTML>"; | |
41 | } | |
42 | ||
43 | ||
44 | /** Class for sending inline or multipart-emails. | |
45 | */ | |
46 | class DiogenesMailer { | |
47 | /** The header of the email. */ | |
48 | var $header; | |
49 | /** The body of the email. */ | |
50 | var $body; | |
51 | /** The sender of the email. */ | |
52 | var $from; | |
53 | /** The recipient of the email. */ | |
54 | var $to; | |
55 | /** Carbon copy for the email. */ | |
56 | var $cc; | |
57 | /** Blind carbon copy for the email. */ | |
58 | var $bcc; | |
59 | /** Subject of the email. */ | |
60 | var $subject; | |
61 | /** The boundary used to separate the email's body parts. */ | |
62 | var $boundary; | |
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. */ | |
65 | var $from_present; | |
66 | ||
67 | ||
68 | /** The constructor. Initialises header & body. | |
69 | * | |
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 | |
76 | */ | |
77 | function DiogenesMailer($from, $to, $subject, $multipart=false, $cc="", $bcc="") { | |
78 | trigger_error("DiogenesMailer class is obsolete, use HermesMailer instead !", E_USER_NOTICE); | |
79 | $this->from = $from; | |
80 | $this->from_present = false; | |
81 | $this->to = ($to == '' ? $from : $to); | |
82 | $this->cc = $cc; | |
83 | $this->bcc = $bcc; | |
84 | $this->subject = $subject; | |
85 | $this->body = ""; | |
86 | $this->header = "X-Mailer: PHP/" . phpversion()."\n". | |
87 | "Mime-Version: 1.0\n"; | |
88 | if ($multipart) { | |
89 | $this->boundary="-=partie_suivante_(".uniqid("").")=-"; | |
90 | $this->header .= | |
91 | "Content-Type: multipart/alternative;\n". | |
92 | " boundary=\"{$this->boundary}\"\n"; | |
93 | ||
94 | } else { | |
95 | $this->boundary=""; | |
96 | $this->header .= | |
97 | "Content-Type: text/plain; charset=iso-8859-1\n". | |
98 | "Content-Disposition: inline\n". | |
99 | "Content-Transfer-Encoding: 8bit\n"; | |
100 | } | |
101 | } | |
102 | ||
103 | ||
104 | /** Adds a part to the email's body. | |
105 | * | |
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 | |
109 | */ | |
110 | function addPart($type,$encoding,$value) | |
111 | { | |
112 | if ($this->boundary) { | |
113 | $this->body.= | |
114 | "--{$this->boundary}\n". | |
115 | "Content-Type: $type\n". | |
116 | "Content-Transfer-Encoding: $encoding\n\n"; | |
117 | $this->body .= "$value\n"; | |
118 | } else { | |
119 | echo "<b>Erreur : addPart s'applique uniquement aux messages multipart!</b>"; | |
120 | } | |
121 | } | |
122 | ||
123 | ||
124 | /** Adds a header to the email. | |
125 | * | |
126 | * @param text the contents of the header (without the final line feed) | |
127 | */ | |
128 | function addHeader($text) | |
129 | { | |
130 | if (preg_match('/^From:/i', $text)) $this->from_present = true; | |
131 | $this->header .= "$text\n"; | |
132 | } | |
133 | ||
134 | ||
135 | /** Adds a "text/plain" part to the email. | |
136 | * | |
137 | * @param text | |
138 | */ | |
139 | function addPartText($text) | |
140 | { | |
141 | $this->addPart("text/plain; charset=iso-8859-1", | |
142 | "8bit", $text); | |
143 | } | |
144 | ||
145 | ||
146 | /** Adds a "text/html" part to the email. | |
147 | * | |
148 | * @param html | |
149 | */ | |
150 | function addPartHtml($html) | |
151 | { | |
152 | $this->addPart("text/html; charset=iso-8859-1", | |
153 | "8bit", $html); | |
154 | } | |
155 | ||
156 | ||
157 | /** Sets the body of the email (only for inline messages!). | |
158 | * | |
159 | * @param text | |
160 | */ | |
161 | function setBody($text) | |
162 | { | |
163 | if (!$this->boundary) { | |
164 | $this->body = $text; | |
165 | } else { | |
166 | die("Error : setBody only applies to inline messages!"); | |
167 | } | |
168 | } | |
169 | ||
170 | ||
171 | /** Sends the email using a pipe to sendmail. | |
172 | */ | |
173 | function send() | |
174 | { | |
175 | if(!$this->from_present) | |
176 | $this->header .= "From: {$this->from}\n"; | |
177 | if ($this->to) | |
178 | $this->header .= "To: {$this->to}\n"; | |
179 | if ($this->cc) | |
180 | $this->header .= "Cc: {$this->cc}\n"; | |
181 | ||
182 | $this->header .= "Subject: {$this->subject}\n"; | |
183 | $this->header .= "\n"; | |
184 | ||
185 | if ($this->boundary) | |
186 | $this->body .= "--{$this->boundary}--\n"; | |
187 | ||
188 | $fp = popen('/usr/sbin/sendmail -oi -f '.escapeshellarg($this->from).' '.escapeshellarg($this->to).' '.escapeshellarg($this->cc).' '.escapeshellarg($this->bcc),'w'); | |
189 | if ($fp) { | |
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; | |
193 | } | |
194 | return false; | |
195 | } | |
196 | ||
197 | } | |
198 | ||
199 | ?> |