2 /***************************************************************************
3 * Copyright (C) 2003-2007 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/";
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'));
52 public static function &get(&$mailer, $tpl)
55 if (!isset($plmail) ||
$plmail->tpl
!= $tpl) {
56 $plmail = new PlMail($tpl);
58 $plmail->mailer
=& $mailer;
62 public function run($version)
64 $this->assign('mail_part', $version);
65 $text = $this->fetch($this->tpl
);
66 if ($version == 'text') {
67 return wordwrap($text, 78);
72 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
73 static public function mail_format($output, &$smarty)
75 return "\n".trim($output)."\n";
78 static protected function format_addr(&$params)
80 if (isset($params['full'])) {
81 return $params['full'];
82 } elseif (empty($params['text'])) {
83 return $params['addr'];
85 return $params['text'].' <'.$params['addr'].'>';
89 /** template function : from.
90 * {from full=...} for an already formatted address
91 * {from addr=... [text=...]} else
93 public function setFrom($params, &$smarty)
95 $smarty->mailer
->setFrom(PlMail
::format_addr($params));
98 /** template function : to.
99 * {to full=...} for an already formatted address
100 * {to addr=... [text=...]} else
102 public function addTo($params, &$smarty)
104 $smarty->mailer
->addTo(PlMail
::format_addr($params));
107 /** template function : cc.
108 * {cc full=...} for an already formatted address
109 * {cc addr=... [text=...]} else
111 public function addCc($params, &$smarty)
113 $smarty->mailer
->addCc(PlMail
::format_addr($params));
116 /** template function : bcc.
117 * {bcc full=...} for an already formatted address
118 * {bcc addr=... [text=...]} else
120 public function addBcc($params, &$smarty)
122 $smarty->mailer
->addBcc(PlMail
::format_addr($params));
125 /** template function : subject.
128 public function setSubject($params, &$smarty)
130 $smarty->mailer
->setSubject($params['text']);
133 /** template function : add_header.
134 * {add_header name=... value=...}
136 public function addHeader($params, &$smarty)
138 $smarty->mailer
->addHeader($params['name'], $params['value']);
142 require_once('Mail.php');
143 require_once('Mail/mime.php');
145 /** Class for sending inline or multipart-emails.
146 * Based on Diogenes' HermesMailer
148 class PlMailer
extends Mail_Mime
{
151 private $page = null
;
153 private $wiki = null
;
155 function __construct($tpl = null
, $charset = "UTF-8")
157 $this->charset
= $charset;
158 $this->Mail_Mime("\n");
159 $this->mail
= Mail
::factory('sendmail', Array('sendmail_args' => '-oi'));
160 if (!is_null($tpl)) {
161 $this->page
=& PlMail
::get($this, $tpl);
166 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
168 private function correct_emails($email)
170 return preg_replace('!(^|, *)([^<"]+?) *(<[^>]*>)!u', '\1"\2" \3', $email);
173 public function addTo($email)
175 $email = $this->correct_emails($email);
176 if (isset($this->_headers
['To'])) {
177 $this->_headers
['To'] .= ", $email";
179 $this->_headers
['To'] = $email;
183 public function addCc($email)
185 return parent
::addCc($this->correct_emails($email));
188 public function addBcc($email)
190 return parent
::addBcc($this->correct_emails($email));
193 public function setFrom($email)
195 return parent
::setFrom($this->correct_emails($email));
198 public function addHeader($hdr,$val)
202 $this->setFrom($val);
206 unset($this->_headers
[$hdr]);
211 unset($this->_headers
[$hdr]);
216 unset($this->_headers
[$hdr]);
221 $this->headers(Array($hdr=>$val));
225 public function addUploadAttachment(PlUpload
&$upload, $name)
227 $encoding = $upload->isType('text') ?
'quoted-printable' : 'base64';
228 $this->addAttachment($upload->getContents(), $upload->contentType(), $name, false
, $encoding);
231 public function assign($var, $value)
233 if (!is_null($this->page
)) {
234 $this->page
->assign($var, $value);
238 public function assign_by_ref($var, &$value)
240 if (!is_null($this->page
)) {
241 $this->page
->assign_by_ref($var, $value);
245 public function register_modifier($var, $callback)
247 if (!is_null($this->page
)) {
248 $this->page
->register_modifier($var, $callback);
252 public function register_function($var, $callback)
254 if (!is_null($this->page
)) {
255 $this->page
->register_function($var, $callback);
259 public function setWikiBody($wiki)
264 private function processPage($with_html = true
)
266 $level = error_reporting(0);
267 if (!is_null($this->page
)) {
268 $level = error_reporting(0);
269 $this->page
->run('head'); // process page headers
270 $this->wiki
= trim($this->page
->run('wiki')); // get wiki
272 $this->setTxtBody($this->page
->run('text'));
274 $html = trim($this->page
->run('html'));
276 $this->setHtmlBody($html);
280 error_reporting($level);
283 $this->setTxtBody(MiniWiki
::WikiToText($this->wiki
, true
, 0, 78));
285 $this->setHtmlBody(MiniWiki
::WikiToHtml($this->wiki
, true
));
290 public function send($with_html = true
)
292 $this->processPage($with_html);
293 if (S
::v('forlife')) {
295 $this->addHeader('X-Org-Mail', S
::v('forlife') . '@' . $globals->mail
->domain
);
298 foreach(Array('To', 'Cc', 'Bcc') as $hdr) {
299 if(isset($this->_headers
[$hdr])) {
300 require_once 'Mail/RFC822.php';
301 $parsed = @Mail_RFC822
::parseAddressList($this->_headers
[$hdr]);
302 if (is_array($parsed)) {
303 $addrs = array_merge($addrs, $parsed);
312 foreach($addrs as $a) {
313 $dests[] = "{$a->mailbox}@{$a->host}";
316 // very important to do it in THIS order very precisely.
317 $body = $this->get(array('text_charset' => $this->charset
,
318 'text_encoding' => '8bit',
319 'html_charset' => $this->charset
,
320 'head_charset' => $this->charset
));
321 $hdrs = $this->headers();
322 if (empty($hdrs['From'])) {
323 trigger_error('Empty "From", mail not sent', E_USER_WARNING
);
326 return $this->mail
->send($dests, $hdrs, $body);
330 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: