Should fix a long standing bug preventing mail with accentuated From to be
[platal.git] / classes / plmailer.php
CommitLineData
b98794ca 1<?php
2/***************************************************************************
2ab75571 3 * Copyright (C) 2003-2010 Polytechnique.org *
b98794ca 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
b98794ca 24/** Classe de mail avec corps en templates.
25 */
26class PlMail extends Smarty
27{
28 private $tpl;
29 private $mailer = null;
30
b2192733 31 function __construct($tpl)
b98794ca 32 {
33 global $globals;
34 $this->tpl = $tpl;
b98794ca 35 $this->caching = false;
36 $this->compile_check = true;
37
38 $this->template_dir = $globals->spoolroot . "/templates/";
d635addc 39 $this->compile_dir = $globals->spoolroot . "/spool/mails_c/";
b98794ca 40 $this->config_dir = $globals->spoolroot . "/configs/";
db143c0f 41 array_unshift($this->plugins_dir, $globals->spoolroot."/plugins/");
b98794ca 42
43 $this->register_outputfilter(Array($this, 'mail_format'));
44 $this->register_function('from', Array($this, 'setFrom'));
45 $this->register_function('to', Array($this, 'addTo'));
46 $this->register_function('cc', Array($this, 'addCc'));
47 $this->register_function('bcc', Array($this, 'addBcc'));
48 $this->register_function('subject', Array($this, 'setSubject'));
11f44323 49 $this->register_function('add_header', Array($this, 'addHeader'));
a99ff1fc 50 $this->assign_by_ref('globals', $globals);
b98794ca 51 }
52
b2192733 53 public static function &get(&$mailer, $tpl)
54 {
55 static $plmail;
56 if (!isset($plmail) || $plmail->tpl != $tpl) {
57 $plmail = new PlMail($tpl);
58 }
59 $plmail->mailer =& $mailer;
60 return $plmail;
61 }
62
5b21237d 63 public function run($version)
b98794ca 64 {
5b21237d 65 $this->assign('mail_part', $version);
a2446af5 66 $text = $this->fetch($this->tpl);
7cbf5d4b 67 if ($version == 'text') {
68 return wordwrap($text, 78);
69 }
a2446af5 70 return $text;
b98794ca 71 }
72
b98794ca 73 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
5bc6e9e6 74 static public function mail_format($output, &$smarty)
b98794ca 75 {
7cbf5d4b 76 return "\n".trim($output)."\n";
b98794ca 77 }
78
5bc6e9e6 79 static protected function format_addr(&$params)
b98794ca 80 {
81 if (isset($params['full'])) {
82 return $params['full'];
83 } elseif (empty($params['text'])) {
84 return $params['addr'];
85 } else {
86 return $params['text'].' <'.$params['addr'].'>';
87 }
88 }
89
b98794ca 90 /** template function : from.
91 * {from full=...} for an already formatted address
92 * {from addr=... [text=...]} else
93 */
5bc6e9e6 94 public function setFrom($params, &$smarty)
b98794ca 95 {
5bc6e9e6 96 $smarty->mailer->setFrom(PlMail::format_addr($params));
b98794ca 97 }
98
b98794ca 99 /** template function : to.
100 * {to full=...} for an already formatted address
101 * {to addr=... [text=...]} else
102 */
5bc6e9e6 103 public function addTo($params, &$smarty)
b98794ca 104 {
5bc6e9e6 105 $smarty->mailer->addTo(PlMail::format_addr($params));
b98794ca 106 }
107
b98794ca 108 /** template function : cc.
109 * {cc full=...} for an already formatted address
110 * {cc addr=... [text=...]} else
111 */
5bc6e9e6 112 public function addCc($params, &$smarty)
b98794ca 113 {
5bc6e9e6 114 $smarty->mailer->addCc(PlMail::format_addr($params));
b98794ca 115 }
116
b98794ca 117 /** template function : bcc.
118 * {bcc full=...} for an already formatted address
119 * {bcc addr=... [text=...]} else
120 */
5bc6e9e6 121 public function addBcc($params, &$smarty)
b98794ca 122 {
5bc6e9e6 123 $smarty->mailer->addBcc(PlMail::format_addr($params));
b98794ca 124 }
125
b98794ca 126 /** template function : subject.
eaf30d86 127 * {subject text=...}
b98794ca 128 */
5bc6e9e6 129 public function setSubject($params, &$smarty)
b98794ca 130 {
131 $smarty->mailer->setSubject($params['text']);
132 }
133
11f44323 134 /** template function : add_header.
135 * {add_header name=... value=...}
136 */
5bc6e9e6 137 public function addHeader($params, &$smarty)
11f44323 138 {
139 $smarty->mailer->addHeader($params['name'], $params['value']);
140 }
b98794ca 141}
b98794ca 142
143require_once('Mail.php');
144require_once('Mail/mime.php');
145
b98794ca 146/** Class for sending inline or multipart-emails.
5bc6e9e6 147 * Based on Diogenes' HermesMailer
b98794ca 148 */
149class PlMailer extends Mail_Mime {
150
151 private $mail;
152 private $page = null;
153 private $charset;
5b21237d 154 private $wiki = null;
b98794ca 155
a7de4ef7 156 function __construct($tpl = null, $charset = "UTF-8")
b98794ca 157 {
158 $this->charset = $charset;
159 $this->Mail_Mime("\n");
a14159bf 160 $this->mail = Mail::factory('sendmail', Array('sendmail_args' => '-oi'));
b98794ca 161 if (!is_null($tpl)) {
b2192733 162 $this->page =& PlMail::get($this, $tpl);
b98794ca 163 }
164 }
165
7b2bd261 166 static private function formatUser(PlUser $user)
e25ddad9
FB
167 {
168 return '"' . $user->fullName() . '" <' . $user->bestEmail() . '>';
169 }
170
b98794ca 171 /**
172 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
173 */
b98794ca 174 private function correct_emails($email)
175 {
e25ddad9
FB
176 if ($email instanceof PlUser) {
177 $email = self::formatUser($email);
178 }
47e54924
FB
179 $email = preg_replace('!(^|, *)([^<"]+?) *(<[^>]*>)!ue',
180 '\1 "\2" \3', $email);
181 return preg_replace('/"([^<]+)"/e',
182 '"\\"" . PlMailer::encodeStringQP("\1") . "\\""',
183 $email);
b98794ca 184 }
185
5bc6e9e6 186 public function addTo($email)
b98794ca 187 {
188 $email = $this->correct_emails($email);
189 if (isset($this->_headers['To'])) {
190 $this->_headers['To'] .= ", $email";
191 } else {
192 $this->_headers['To'] = $email;
193 }
194 }
195
5bc6e9e6 196 public function addCc($email)
b98794ca 197 {
198 return parent::addCc($this->correct_emails($email));
199 }
200
5bc6e9e6 201 public function addBcc($email)
b98794ca 202 {
203 return parent::addBcc($this->correct_emails($email));
204 }
205
5bc6e9e6 206 public function setFrom($email)
b98794ca 207 {
208 return parent::setFrom($this->correct_emails($email));
209 }
210
47e54924
FB
211 static function encodeStringQP($string)
212 {
213 if (!preg_match('/^[\x20-\x7e]*$/', $string)) {
214 $string = '=?UTF-8?Q?' . preg_replace('/[^\x21-\x3C\x3e\x40-\x7e]/e', 'PlMailer::encodeQP("\0")', $string)
215 . '?=';
216 }
217 return $string;
218 }
219
220
5ec9aef3
FB
221 static function encodeQP($char)
222 {
223 return sprintf('=%02X', ord($char));
224 }
225
226 public function setSubject($subject)
227 {
47e54924 228 return parent::setSubject(self::encodeStringQP($subject));
5ec9aef3
FB
229 }
230
5bc6e9e6 231 public function addHeader($hdr,$val)
b98794ca 232 {
233 switch($hdr) {
234 case 'From':
235 $this->setFrom($val);
236 break;
237
238 case 'To':
239 unset($this->_headers[$hdr]);
240 $this->addTo($val);
241 break;
242
243 case 'Cc':
244 unset($this->_headers[$hdr]);
245 $this->addCc($val);
246 break;
247
248 case 'Bcc':
249 unset($this->_headers[$hdr]);
250 $this->addBcc($val);
251 break;
252
253 default:
254 $this->headers(Array($hdr=>$val));
255 }
256 }
257
da419622 258 public function addUploadAttachment(PlUpload &$upload, $name)
259 {
260 $encoding = $upload->isType('text') ? 'quoted-printable' : 'base64';
261 $this->addAttachment($upload->getContents(), $upload->contentType(), $name, false, $encoding);
262 }
263
5bc6e9e6 264 public function assign($var, $value)
b98794ca 265 {
266 if (!is_null($this->page)) {
267 $this->page->assign($var, $value);
268 }
269 }
eaf30d86 270
5bc6e9e6 271 public function assign_by_ref($var, &$value)
11f44323 272 {
273 if (!is_null($this->page)) {
274 $this->page->assign_by_ref($var, $value);
275 }
276 }
277
5bc6e9e6 278 public function register_modifier($var, $callback)
11f44323 279 {
280 if (!is_null($this->page)) {
281 $this->page->register_modifier($var, $callback);
282 }
283 }
eaf30d86 284
5bc6e9e6 285 public function register_function($var, $callback)
11f44323 286 {
287 if (!is_null($this->page)) {
288 $this->page->register_function($var, $callback);
289 }
290 }
5b21237d 291
292 public function setWikiBody($wiki)
293 {
294 $this->wiki = $wiki;
295 }
ef42a9d6 296
11f44323 297 private function processPage($with_html = true)
b98794ca 298 {
299 if (!is_null($this->page)) {
22955259
FB
300 global $globals;
301 if (!($globals->debug & DEBUG_SMARTY)) {
302 $level = error_reporting(0);
303 }
402a0780 304 $this->page->assign_by_ref('globals', $globals);
5b21237d 305 $this->page->run('head'); // process page headers
306 $this->wiki = trim($this->page->run('wiki')); // get wiki
307 if (!$this->wiki) {
308 $this->setTxtBody($this->page->run('text'));
309 if ($with_html) {
310 $html = trim($this->page->run('html'));
311 if (!empty($html)) {
312 $this->setHtmlBody($html);
313 }
11f44323 314 }
b98794ca 315 }
22955259
FB
316 if (!($globals->debug & DEBUG_SMARTY)) {
317 error_reporting($level);
318 }
5b21237d 319 }
320 if ($this->wiki) {
0d624b5e 321 $this->setTxtBody(MiniWiki::WikiToText($this->wiki, false, 0, 78));
5b21237d 322 if ($with_html) {
402390ee 323 $this->setHtmlBody('<html><body>' . MiniWiki::WikiToHtml($this->wiki, true) . '</body></html>');
5b21237d 324 }
b98794ca 325 }
326 }
327
964cfce7
FB
328 public function sendTo(PlUser &$user)
329 {
e25ddad9 330 $this->addTo($user);
964cfce7
FB
331 $this->assign_by_ref('user', $user);
332 return $this->send($user->isEmailFormatHtml());
333 }
334
5bc6e9e6 335 public function send($with_html = true)
b98794ca 336 {
11f44323 337 $this->processPage($with_html);
4d19f60b
VZ
338 if (S::user()) {
339 $this->addHeader('X-Org-Mail', S::user()->forlifeEmail());
b98794ca 340 }
341 $addrs = Array();
a105f588
VZ
342 foreach (Array('To', 'Cc', 'Bcc') as $hdr) {
343 if (isset($this->_headers[$hdr])) {
b98794ca 344 require_once 'Mail/RFC822.php';
c77fed74 345 $parsed = @Mail_RFC822::parseAddressList($this->_headers[$hdr]);
346 if (is_array($parsed)) {
347 $addrs = array_merge($addrs, $parsed);
348 }
b98794ca 349 }
350 }
a105f588 351 if (empty($addrs)) {
b98794ca 352 return false;
353 }
eaf30d86 354
b98794ca 355 $dests = Array();
a105f588 356 foreach ($addrs as $a) {
b98794ca 357 $dests[] = "{$a->mailbox}@{$a->host}";
358 }
eaf30d86 359
a105f588
VZ
360 // Support for a "catch-all" email address, to be used by developers.
361 // This mode can only be activated when the working copy is in restricted
362 // mode, to ensure that production plat/al copies are never affected.
0d40ddec 363 global $globals;
a105f588 364 if ($globals->email_catchall && $globals->core->restricted_platal) {
0d40ddec 365 require_once 'Mail/RFC822.php';
a105f588
VZ
366 if (@Mail_RFC822::isValidInetAddress($globals->email_catchall)) {
367 $dests = array($globals->email_catchall);
0d40ddec
RB
368 }
369 }
370
b98794ca 371 // very important to do it in THIS order very precisely.
372 $body = $this->get(array('text_charset' => $this->charset,
d77ad560 373 'text_encoding' => '8bit',
b98794ca 374 'html_charset' => $this->charset,
375 'head_charset' => $this->charset));
376 $hdrs = $this->headers();
434cd6dd 377 if (empty($hdrs['From'])) {
378 trigger_error('Empty "From", mail not sent', E_USER_WARNING);
379 return false;
380 }
b98794ca 381 return $this->mail->send($dests, $hdrs, $body);
382 }
b98794ca 383}
384
a7de4ef7 385// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
b98794ca 386?>