2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 require_once('smarty/libs/Smarty.class.php');
24 /** Classe de mail avec corps en templates.
26 class PlMail
extends Smarty
29 private $mailer = null
;
31 function __construct($tpl)
35 $this->caching
= false
;
36 $this->compile_check
= true
;
38 $this->template_dir
= $globals->spoolroot
. "/templates/";
39 $this->compile_dir
= $globals->spoolroot
. "/spool/mails_c/";
40 $this->config_dir
= $globals->spoolroot
. "/configs/";
41 array_unshift($this->plugins_dir
, $globals->spoolroot
."/plugins/");
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'));
49 $this->register_function('add_header', Array($this, 'addHeader'));
50 $this->assign_by_ref('globals', $globals);
53 public static function &get(&$mailer, $tpl)
56 if (!isset($plmail) ||
$plmail->tpl
!= $tpl) {
57 $plmail = new PlMail($tpl);
59 $plmail->mailer
=& $mailer;
63 public function run($version)
65 $this->assign('mail_part', $version);
66 $text = $this->fetch($this->tpl
);
67 if ($version == 'text') {
68 return wordwrap($text, 78);
73 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
74 static public function mail_format($output, &$smarty)
76 return "\n".trim($output)."\n";
79 static protected function format_addr(&$params)
81 if (isset($params['full'])) {
82 return $params['full'];
83 } elseif (empty($params['text'])) {
84 return $params['addr'];
86 return $params['text'].' <'.$params['addr'].'>';
90 /** template function : from.
91 * {from full=...} for an already formatted address
92 * {from addr=... [text=...]} else
94 public function setFrom($params, &$smarty)
96 $smarty->mailer
->setFrom(PlMail
::format_addr($params));
99 /** template function : to.
100 * {to full=...} for an already formatted address
101 * {to addr=... [text=...]} else
103 public function addTo($params, &$smarty)
105 $smarty->mailer
->addTo(PlMail
::format_addr($params));
108 /** template function : cc.
109 * {cc full=...} for an already formatted address
110 * {cc addr=... [text=...]} else
112 public function addCc($params, &$smarty)
114 $smarty->mailer
->addCc(PlMail
::format_addr($params));
117 /** template function : bcc.
118 * {bcc full=...} for an already formatted address
119 * {bcc addr=... [text=...]} else
121 public function addBcc($params, &$smarty)
123 $smarty->mailer
->addBcc(PlMail
::format_addr($params));
126 /** template function : subject.
129 public function setSubject($params, &$smarty)
131 $smarty->mailer
->setSubject($params['text']);
134 /** template function : add_header.
135 * {add_header name=... value=...}
137 public function addHeader($params, &$smarty)
139 $smarty->mailer
->addHeader($params['name'], $params['value']);
143 require_once('Mail.php');
144 require_once('Mail/mime.php');
146 /** Class for sending inline or multipart-emails.
147 * Based on Diogenes' HermesMailer
149 class PlMailer
extends Mail_Mime
{
152 private $page = null
;
154 private $wiki = null
;
156 function __construct($tpl = null
, $charset = "UTF-8")
158 $this->charset
= $charset;
159 $this->Mail_Mime("\n");
160 $this->mail
= Mail
::factory('sendmail', Array('sendmail_args' => '-oi'));
161 if (!is_null($tpl)) {
162 $this->page
=& PlMail
::get($this, $tpl);
166 static private function formatUser(PlUser
$user)
168 return '"' . $user->fullName() . '" <' . $user->bestEmail() . '>';
172 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
174 private function correct_emails($email)
176 if ($email instanceof PlUser
) {
177 $email = self
::formatUser($email);
179 $email = preg_replace('!(^|, *)([^<"]+?) *(<[^>]*>)!ue',
180 '\1 "\2" \3', $email);
181 return preg_replace('/"([^<]+)"/e',
182 '"\\"" . PlMailer::encodeStringQP("\1") . "\\""',
186 public function addTo($email)
188 $email = $this->correct_emails($email);
189 if (isset($this->_headers
['To'])) {
190 $this->_headers
['To'] .= ", $email";
192 $this->_headers
['To'] = $email;
196 public function setTo($email)
198 $email = $this->correct_emails($email);
199 $this->_headers
['To'] = $email;
202 public function addCc($email)
204 return parent
::addCc($this->correct_emails($email));
207 public function addBcc($email)
209 return parent
::addBcc($this->correct_emails($email));
212 public function setFrom($email)
214 return parent
::setFrom($this->correct_emails($email));
217 static function encodeStringQP($string)
219 if (!preg_match('/^[\x20-\x7e]*$/', $string)) {
220 $string = '=?UTF-8?Q?' . preg_replace('/[^\x21-\x3C\x3e\x40-\x7e]/e', 'PlMailer::encodeQP("\0")', $string)
227 static function encodeQP($char)
229 return sprintf('=%02X', ord($char));
232 public function setSubject($subject)
234 return parent
::setSubject(self
::encodeStringQP($subject));
237 public function addHeader($hdr,$val)
241 $this->setFrom($val);
245 unset($this->_headers
[$hdr]);
250 unset($this->_headers
[$hdr]);
255 unset($this->_headers
[$hdr]);
260 $this->headers(Array($hdr=>$val));
264 public function addUploadAttachment(PlUpload
&$upload, $name)
266 $encoding = $upload->isType('text') ?
'quoted-printable' : 'base64';
267 $this->addAttachment($upload->getContents(), $upload->contentType(), $name, false
, $encoding);
270 public function assign($var, $value)
272 if (!is_null($this->page
)) {
273 $this->page
->assign($var, $value);
277 public function assign_by_ref($var, &$value)
279 if (!is_null($this->page
)) {
280 $this->page
->assign_by_ref($var, $value);
284 public function register_modifier($var, $callback)
286 if (!is_null($this->page
)) {
287 $this->page
->register_modifier($var, $callback);
291 public function register_function($var, $callback)
293 if (!is_null($this->page
)) {
294 $this->page
->register_function($var, $callback);
298 public function setWikiBody($wiki)
303 private function processPage($with_html = true
)
305 if (!is_null($this->page
)) {
307 if (!($globals->debug
& DEBUG_SMARTY
)) {
308 $level = error_reporting(0);
310 $this->page
->assign_by_ref('globals', $globals);
311 $this->page
->run('head'); // process page headers
312 $this->wiki
= trim($this->page
->run('wiki')); // get wiki
314 $this->setTxtBody($this->page
->run('text'));
316 $html = trim($this->page
->run('html'));
318 $this->setHtmlBody($html);
322 if (!($globals->debug
& DEBUG_SMARTY
)) {
323 error_reporting($level);
327 $this->setTxtBody(MiniWiki
::WikiToText($this->wiki
, false
, 0, 78));
329 $this->setHtmlBody('<html><body>' . MiniWiki
::WikiToHtml($this->wiki
, true
) . '</body></html>');
334 public function sendTo(PlUser
&$user)
337 $this->assign_by_ref('user', $user);
338 return $this->send($user->isEmailFormatHtml());
341 public function send($with_html = true
)
343 $this->processPage($with_html);
345 $this->addHeader('X-Org-Mail', S
::user()->forlifeEmail());
348 foreach (Array('To', 'Cc', 'Bcc') as $hdr) {
349 if (isset($this->_headers
[$hdr])) {
350 require_once 'Mail/RFC822.php';
351 $parsed = @Mail_RFC822
::parseAddressList($this->_headers
[$hdr]);
352 if (is_array($parsed)) {
353 $addrs = array_merge($addrs, $parsed);
362 foreach ($addrs as $a) {
363 $dests[] = "{$a->mailbox}@{$a->host}";
366 // Support for a "catch-all" email address, to be used by developers.
367 // This mode can only be activated when the working copy is in restricted
368 // mode, to ensure that production plat/al copies are never affected.
370 if ($globals->email_catchall
&& $globals->core
->restricted_platal
) {
371 require_once 'Mail/RFC822.php';
372 if (@Mail_RFC822
::isValidInetAddress($globals->email_catchall
)) {
373 $dests = array($globals->email_catchall
);
377 // very important to do it in THIS order very precisely.
378 $body = $this->get(array('text_charset' => $this->charset
,
379 'text_encoding' => '8bit',
380 'html_charset' => $this->charset
,
381 'head_charset' => $this->charset
));
382 $hdrs = $this->headers();
383 if (empty($hdrs['From'])) {
384 trigger_error('Empty "From", mail not sent', E_USER_WARNING
);
387 return $this->mail
->send($dests, $hdrs, $body);
391 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: