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