2 /***************************************************************************
3 * Copyright (C) 2003-2006 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');
26 /** Classe de mail avec corps en templates.
28 class PlMail
extends Smarty
31 private $mailer = null
;
35 function PlMail($mailer, $tpl)
39 $this->mailer
= $mailer;
40 $this->caching
= false
;
41 $this->compile_check
= true
;
43 $this->template_dir
= $globals->spoolroot
. "/templates/";
44 $this->compile_dir
= $globals->spoolroot
. "/spool/templates_c/";
45 $this->config_dir
= $globals->spoolroot
. "/configs/";
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'));
60 $this->assign('html_version', $html);
61 return $this->fetch($this->tpl
);
65 // {{{ function mail_format()
67 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
68 static function mail_format($output, &$smarty)
70 return wordwrap("\n".trim($output)."\n",75);
74 // {{{ function format_addr()
76 static function format_addr(&$params)
78 if (isset($params['full'])) {
79 return $params['full'];
80 } elseif (empty($params['text'])) {
81 return $params['addr'];
83 return $params['text'].' <'.$params['addr'].'>';
88 // {{{ function setFrom()
90 /** template function : from.
91 * {from full=...} for an already formatted address
92 * {from addr=... [text=...]} else
94 function setFrom($params, &$smarty)
96 $smarty->mailer
->setFrom($this->format_addr($params));
100 // {{{ function setTo()
102 /** template function : to.
103 * {to full=...} for an already formatted address
104 * {to addr=... [text=...]} else
106 function addTo($params, &$smarty)
108 $smarty->mailer
->addTo($this->format_addr($params));
112 // {{{ function setCc()
114 /** template function : cc.
115 * {cc full=...} for an already formatted address
116 * {cc addr=... [text=...]} else
118 function addCc($params, &$smarty)
120 $smarty->mailer
->addCc($this->format_addr($params));
124 // {{{ function setBcc()
126 /** template function : bcc.
127 * {bcc full=...} for an already formatted address
128 * {bcc addr=... [text=...]} else
130 function addBcc($params, &$smarty)
132 $smarty->mailer
->addBcc($this->format_addr($params));
136 // {{{ function setSubject()
138 /** template function : subject.
141 function setSubject($params, &$smarty)
143 $smarty->mailer
->setSubject($params['text']);
151 require_once('Mail.php');
152 require_once('Mail/mime.php');
154 // {{{ class PlMailer
155 /** Class for sending inline or multipart-emails.
157 class PlMailer
extends Mail_Mime
{
160 private $page = null
;
164 function PlMailer($tpl = null
, $charset = "ISO-8859-15")
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);
175 // {{{ function correct_emails()
178 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
181 private function correct_emails($email)
183 return preg_replace('!(^|, *)([^<"][^<"]*[^< "]) *(<[^>]*>)!', '\1"\2" \3', $email);
187 // {{{ function addTo()
189 function addTo($email)
191 $email = $this->correct_emails($email);
192 if (isset($this->_headers
['To'])) {
193 $this->_headers
['To'] .= ", $email";
195 $this->_headers
['To'] = $email;
200 // {{{ function addCc()
202 function addCc($email)
204 return parent
::addCc($this->correct_emails($email));
208 // {{{ function addBcc()
210 function addBcc($email)
212 return parent
::addBcc($this->correct_emails($email));
216 // {{{ function setFrom()
218 function setFrom($email)
220 return parent
::setFrom($this->correct_emails($email));
224 // {{{ function addHeader()
226 function addHeader($hdr,$val)
230 $this->setFrom($val);
234 unset($this->_headers
[$hdr]);
239 unset($this->_headers
[$hdr]);
244 unset($this->_headers
[$hdr]);
249 $this->headers(Array($hdr=>$val));
254 // {{{ function assign()
256 function assign($var, $value)
258 if (!is_null($this->page
)) {
259 $this->page
->assign($var, $value);
264 // {{{ function processPage()
266 private function processPage()
268 if (!is_null($this->page
)) {
269 $this->setTxtBody($this->page
->run(false
));
270 $html = trim($this->page
->run(true
));
272 $this->setHtmlBody($html);
278 // {{{ function send()
282 $this->processPage();
283 if (S
::v('forlife')) {
284 $this->addHeader('X-Org-Mail', S
::v('forlife') . '@polytechnique.org');
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]));
298 foreach($addrs as $a) {
299 $dests[] = "{$a->mailbox}@{$a->host}";
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);