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
;
33 function __construct(&$mailer, $tpl)
37 $this->mailer
=& $mailer;
38 $this->caching
= false
;
39 $this->compile_check
= true
;
41 $this->template_dir
= $globals->spoolroot
. "/templates/";
42 $this->compile_dir
= $globals->spoolroot
. "/spool/templates_c/";
43 $this->config_dir
= $globals->spoolroot
. "/configs/";
45 $this->register_outputfilter(Array($this, 'mail_format'));
46 $this->register_function('from', Array($this, 'setFrom'));
47 $this->register_function('to', Array($this, 'addTo'));
48 $this->register_function('cc', Array($this, 'addCc'));
49 $this->register_function('bcc', Array($this, 'addBcc'));
50 $this->register_function('subject', Array($this, 'setSubject'));
51 $this->register_function('add_header', Array($this, 'addHeader'));
54 public function run($html)
56 $this->assign('html_version', $html);
57 return $this->fetch($this->tpl
);
60 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
61 static public function mail_format($output, &$smarty)
63 return wordwrap("\n".trim($output)."\n",75);
66 static protected function format_addr(&$params)
68 if (isset($params['full'])) {
69 return $params['full'];
70 } elseif (empty($params['text'])) {
71 return $params['addr'];
73 return $params['text'].' <'.$params['addr'].'>';
77 /** template function : from.
78 * {from full=...} for an already formatted address
79 * {from addr=... [text=...]} else
81 public function setFrom($params, &$smarty)
83 $smarty->mailer
->setFrom(PlMail
::format_addr($params));
86 /** template function : to.
87 * {to full=...} for an already formatted address
88 * {to addr=... [text=...]} else
90 public function addTo($params, &$smarty)
92 $smarty->mailer
->addTo(PlMail
::format_addr($params));
95 /** template function : cc.
96 * {cc full=...} for an already formatted address
97 * {cc addr=... [text=...]} else
99 public function addCc($params, &$smarty)
101 $smarty->mailer
->addCc(PlMail
::format_addr($params));
104 /** template function : bcc.
105 * {bcc full=...} for an already formatted address
106 * {bcc addr=... [text=...]} else
108 public function addBcc($params, &$smarty)
110 $smarty->mailer
->addBcc(PlMail
::format_addr($params));
113 /** template function : subject.
116 public function setSubject($params, &$smarty)
118 $smarty->mailer
->setSubject($params['text']);
121 /** template function : add_header.
122 * {add_header name=... value=...}
124 public function addHeader($params, &$smarty)
126 $smarty->mailer
->addHeader($params['name'], $params['value']);
130 require_once('Mail.php');
131 require_once('Mail/mime.php');
133 /** Class for sending inline or multipart-emails.
134 * Based on Diogenes' HermesMailer
136 class PlMailer
extends Mail_Mime
{
139 private $page = null
;
142 function __construct($tpl = null
, $charset = "ISO-8859-15")
144 $this->charset
= $charset;
145 $this->Mail_Mime("\n");
146 $this->mail
= @Mail
::factory('sendmail', Array('sendmail_args' => '-oi'));
147 if (!is_null($tpl)) {
148 $this->page
= new PlMail($this, $tpl);
153 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
155 private function correct_emails($email)
157 return preg_replace('!(^|, *)([^<"]+?) *(<[^>]*>)!', '\1"\2" \3', $email);
160 public function addTo($email)
162 $email = $this->correct_emails($email);
163 if (isset($this->_headers
['To'])) {
164 $this->_headers
['To'] .= ", $email";
166 $this->_headers
['To'] = $email;
170 public function addCc($email)
172 return parent
::addCc($this->correct_emails($email));
175 public function addBcc($email)
177 return parent
::addBcc($this->correct_emails($email));
180 public function setFrom($email)
182 return parent
::setFrom($this->correct_emails($email));
185 public function addHeader($hdr,$val)
189 $this->setFrom($val);
193 unset($this->_headers
[$hdr]);
198 unset($this->_headers
[$hdr]);
203 unset($this->_headers
[$hdr]);
208 $this->headers(Array($hdr=>$val));
212 public function assign($var, $value)
214 if (!is_null($this->page
)) {
215 $this->page
->assign($var, $value);
219 public function assign_by_ref($var, &$value)
221 if (!is_null($this->page
)) {
222 $this->page
->assign_by_ref($var, $value);
226 public function register_modifier($var, $callback)
228 if (!is_null($this->page
)) {
229 $this->page
->register_modifier($var, $callback);
233 public function register_function($var, $callback)
235 if (!is_null($this->page
)) {
236 $this->page
->register_function($var, $callback);
240 private function processPage($with_html = true
)
242 if (!is_null($this->page
)) {
243 $this->setTxtBody($this->page
->run(false
));
245 $html = trim($this->page
->run(true
));
247 $this->setHtmlBody($html);
253 public function send($with_html = true
)
255 $this->processPage($with_html);
256 if (S
::v('forlife')) {
257 $this->addHeader('X-Org-Mail', S
::v('forlife') . '@polytechnique.org');
260 foreach(Array('To', 'Cc', 'Bcc') as $hdr) {
261 if(isset($this->_headers
[$hdr])) {
262 require_once 'Mail/RFC822.php';
263 $addrs = array_merge($addrs, @Mail_RFC822
::parseAddressList($this->_headers
[$hdr]));
271 foreach($addrs as $a) {
272 $dests[] = "{$a->mailbox}@{$a->host}";
275 // very important to do it in THIS order very precisely.
276 $body = $this->get(array('text_charset' => $this->charset
,
277 'html_charset' => $this->charset
,
278 'head_charset' => $this->charset
));
279 $hdrs = $this->headers();
280 if (empty($hdrs['From'])) {
281 trigger_error('Empty "From", mail not sent', E_USER_WARNING
);
284 return $this->mail
->send($dests, $hdrs, $body);