Remove comment from CSS before including it in the NL
[platal.git] / classes / plmailer.php
CommitLineData
b98794ca 1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2006 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., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22require_once('smarty/libs/Smarty.class.php');
23
24// {{{ class PlMail
25
26/** Classe de mail avec corps en templates.
27 */
28class PlMail extends Smarty
29{
30 private $tpl;
31 private $mailer = null;
32
33 // {{{ constructor
34
35 function PlMail($mailer, $tpl)
36 {
37 global $globals;
38 $this->tpl = $tpl;
39 $this->mailer = $mailer;
40 $this->caching = false;
41 $this->compile_check = true;
42
43 $this->template_dir = $globals->spoolroot . "/templates/";
44 $this->compile_dir = $globals->spoolroot . "/spool/templates_c/";
45 $this->config_dir = $globals->spoolroot . "/configs/";
46
47 $this->register_outputfilter(Array($this, 'mail_format'));
48 $this->register_function('from', Array($this, 'setFrom'));
49 $this->register_function('to', Array($this, 'addTo'));
50 $this->register_function('cc', Array($this, 'addCc'));
51 $this->register_function('bcc', Array($this, 'addBcc'));
52 $this->register_function('subject', Array($this, 'setSubject'));
11f44323 53 $this->register_function('add_header', Array($this, 'addHeader'));
b98794ca 54 }
55
56 // }}}
57 // {{{ function run()
58
59 function run($html)
60 {
61 $this->assign('html_version', $html);
62 return $this->fetch($this->tpl);
63 }
64
65 // }}}
66 // {{{ function mail_format()
67
68 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
69 static function mail_format($output, &$smarty)
70 {
71 return wordwrap("\n".trim($output)."\n",75);
72 }
73
74 // }}}
75 // {{{ function format_addr()
76
77 static function format_addr(&$params)
78 {
79 if (isset($params['full'])) {
80 return $params['full'];
81 } elseif (empty($params['text'])) {
82 return $params['addr'];
83 } else {
84 return $params['text'].' <'.$params['addr'].'>';
85 }
86 }
87
88 // }}}
89 // {{{ function setFrom()
90
91 /** template function : from.
92 * {from full=...} for an already formatted address
93 * {from addr=... [text=...]} else
94 */
95 function setFrom($params, &$smarty)
96 {
97 $smarty->mailer->setFrom($this->format_addr($params));
98 }
99
100 // }}}
101 // {{{ function setTo()
102
103 /** template function : to.
104 * {to full=...} for an already formatted address
105 * {to addr=... [text=...]} else
106 */
107 function addTo($params, &$smarty)
108 {
109 $smarty->mailer->addTo($this->format_addr($params));
110 }
111
112 // }}}
113 // {{{ function setCc()
114
115 /** template function : cc.
116 * {cc full=...} for an already formatted address
117 * {cc addr=... [text=...]} else
118 */
119 function addCc($params, &$smarty)
120 {
121 $smarty->mailer->addCc($this->format_addr($params));
122 }
123
124 // }}}
125 // {{{ function setBcc()
126
127 /** template function : bcc.
128 * {bcc full=...} for an already formatted address
129 * {bcc addr=... [text=...]} else
130 */
131 function addBcc($params, &$smarty)
132 {
133 $smarty->mailer->addBcc($this->format_addr($params));
134 }
135
136 // }}}
137 // {{{ function setSubject()
138
139 /** template function : subject.
140 * {subject text=...}
141 */
142 function setSubject($params, &$smarty)
143 {
144 $smarty->mailer->setSubject($params['text']);
145 }
146
147 // }}}
11f44323 148 // {{{ function addHeader()
149
150 /** template function : add_header.
151 * {add_header name=... value=...}
152 */
153 function addHeader($params, &$smarty)
154 {
155 $smarty->mailer->addHeader($params['name'], $params['value']);
156 }
157
158 // }}}
b98794ca 159}
160// }}}
161
162
163require_once('Mail.php');
164require_once('Mail/mime.php');
165
166// {{{ class PlMailer
167/** Class for sending inline or multipart-emails.
168 */
169class PlMailer extends Mail_Mime {
170
171 private $mail;
172 private $page = null;
173 private $charset;
174 // {{{ constructor
175
176 function PlMailer($tpl = null, $charset = "ISO-8859-15")
177 {
178 $this->charset = $charset;
179 $this->Mail_Mime("\n");
180 $this->mail =& Mail::factory('sendmail', Array('sendmail_args' => '-oi'));
181 if (!is_null($tpl)) {
182 $this->page = new PlMail($this, $tpl);
183 }
184 }
185
186 // }}}
187 // {{{ function correct_emails()
188
189 /**
190 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
191 */
192
193 private function correct_emails($email)
194 {
195 return preg_replace('!(^|, *)([^<"][^<"]*[^< "]) *(<[^>]*>)!', '\1"\2" \3', $email);
196 }
197
198 // }}}
199 // {{{ function addTo()
200
201 function addTo($email)
202 {
203 $email = $this->correct_emails($email);
204 if (isset($this->_headers['To'])) {
205 $this->_headers['To'] .= ", $email";
206 } else {
207 $this->_headers['To'] = $email;
208 }
209 }
210
211 // }}}
212 // {{{ function addCc()
213
214 function addCc($email)
215 {
216 return parent::addCc($this->correct_emails($email));
217 }
218
219 // }}}
220 // {{{ function addBcc()
221
222 function addBcc($email)
223 {
224 return parent::addBcc($this->correct_emails($email));
225 }
226
227 // }}}
228 // {{{ function setFrom()
229
230 function setFrom($email)
231 {
232 return parent::setFrom($this->correct_emails($email));
233 }
234
235 // }}}
236 // {{{ function addHeader()
237
238 function addHeader($hdr,$val)
239 {
240 switch($hdr) {
241 case 'From':
242 $this->setFrom($val);
243 break;
244
245 case 'To':
246 unset($this->_headers[$hdr]);
247 $this->addTo($val);
248 break;
249
250 case 'Cc':
251 unset($this->_headers[$hdr]);
252 $this->addCc($val);
253 break;
254
255 case 'Bcc':
256 unset($this->_headers[$hdr]);
257 $this->addBcc($val);
258 break;
259
260 default:
261 $this->headers(Array($hdr=>$val));
262 }
263 }
264
265 // }}}
266 // {{{ function assign()
267
268 function assign($var, $value)
269 {
270 if (!is_null($this->page)) {
271 $this->page->assign($var, $value);
272 }
273 }
274
275 // }}}
11f44323 276 // {{{ function assign_by_ref()
277
278 function assign_by_ref($var, &$value)
279 {
280 if (!is_null($this->page)) {
281 $this->page->assign_by_ref($var, $value);
282 }
283 }
284
285 // }}}
286 // {{{ function register_modifier()
287
288 function register_modifier($var, $callback)
289 {
290 if (!is_null($this->page)) {
291 $this->page->register_modifier($var, $callback);
292 }
293 }
294
295 // }}}
296 // {{{ function register_function()
297
298 function register_function($var, $callback)
299 {
300 if (!is_null($this->page)) {
301 $this->page->register_function($var, $callback);
302 }
303 }
304
305 // }}}
b98794ca 306 // {{{ function processPage()
307
11f44323 308 private function processPage($with_html = true)
b98794ca 309 {
310 if (!is_null($this->page)) {
311 $this->setTxtBody($this->page->run(false));
11f44323 312 if ($with_html) {
313 $html = trim($this->page->run(true));
314 if (!empty($html)) {
315 $this->setHtmlBody($html);
316 }
b98794ca 317 }
318 }
319 }
320
321 // }}}
322 // {{{ function send()
323
11f44323 324 function send($with_html = true)
b98794ca 325 {
11f44323 326 $this->processPage($with_html);
b98794ca 327 if (S::v('forlife')) {
328 $this->addHeader('X-Org-Mail', S::v('forlife') . '@polytechnique.org');
329 }
330 $addrs = Array();
331 foreach(Array('To', 'Cc', 'Bcc') as $hdr) {
332 if(isset($this->_headers[$hdr])) {
333 require_once 'Mail/RFC822.php';
334 $addrs = array_merge($addrs, Mail_RFC822::parseAddressList($this->_headers[$hdr]));
335 }
336 }
337 if(empty($addrs)) {
338 return false;
339 }
340
341 $dests = Array();
342 foreach($addrs as $a) {
343 $dests[] = "{$a->mailbox}@{$a->host}";
344 }
345
346 // very important to do it in THIS order very precisely.
347 $body = $this->get(array('text_charset' => $this->charset,
348 'html_charset' => $this->charset,
349 'head_charset' => $this->charset));
350 $hdrs = $this->headers();
351 return $this->mail->send($dests, $hdrs, $body);
352 }
353
354 // }}}
355}
356
357// }}}
358
359?>