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(&$mailer, $tpl)
35 $this->mailer
=& $mailer;
36 $this->caching
= false
;
37 $this->compile_check
= true
;
39 $this->template_dir
= $globals->spoolroot
. "/templates/";
40 $this->compile_dir
= $globals->spoolroot
. "/spool/mails_c/";
41 $this->config_dir
= $globals->spoolroot
. "/configs/";
44 $this->register_outputfilter(Array($this, 'mail_format'));
45 $this->register_function('from', Array($this, 'setFrom'));
46 $this->register_function('to', Array($this, 'addTo'));
47 $this->register_function('cc', Array($this, 'addCc'));
48 $this->register_function('bcc', Array($this, 'addBcc'));
49 $this->register_function('subject', Array($this, 'setSubject'));
50 $this->register_function('add_header', Array($this, 'addHeader'));
53 public function run($html)
55 $this->assign('html_version', $html);
56 $text = $this->fetch($this->tpl
);
61 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
62 static public function mail_format($output, &$smarty)
64 return wordwrap("\n".trim($output)."\n",75);
67 static protected function format_addr(&$params)
69 if (isset($params['full'])) {
70 return $params['full'];
71 } elseif (empty($params['text'])) {
72 return $params['addr'];
74 return $params['text'].' <'.$params['addr'].'>';
78 /** template function : from.
79 * {from full=...} for an already formatted address
80 * {from addr=... [text=...]} else
82 public function setFrom($params, &$smarty)
84 $smarty->mailer
->setFrom(PlMail
::format_addr($params));
87 /** template function : to.
88 * {to full=...} for an already formatted address
89 * {to addr=... [text=...]} else
91 public function addTo($params, &$smarty)
93 $smarty->mailer
->addTo(PlMail
::format_addr($params));
96 /** template function : cc.
97 * {cc full=...} for an already formatted address
98 * {cc addr=... [text=...]} else
100 public function addCc($params, &$smarty)
102 $smarty->mailer
->addCc(PlMail
::format_addr($params));
105 /** template function : bcc.
106 * {bcc full=...} for an already formatted address
107 * {bcc addr=... [text=...]} else
109 public function addBcc($params, &$smarty)
111 $smarty->mailer
->addBcc(PlMail
::format_addr($params));
114 /** template function : subject.
117 public function setSubject($params, &$smarty)
119 $smarty->mailer
->setSubject($params['text']);
122 /** template function : add_header.
123 * {add_header name=... value=...}
125 public function addHeader($params, &$smarty)
127 $smarty->mailer
->addHeader($params['name'], $params['value']);
131 require_once('Mail.php');
132 require_once('Mail/mime.php');
134 /** Class for sending inline or multipart-emails.
135 * Based on Diogenes' HermesMailer
137 class PlMailer
extends Mail_Mime
{
140 private $page = null
;
143 function __construct($tpl = null
, $charset = "ISO-8859-15")
145 $this->charset
= $charset;
146 $this->Mail_Mime("\n");
147 $this->mail
= @Mail
::factory('sendmail', Array('sendmail_args' => '-oi'));
148 if (!is_null($tpl)) {
149 $this->page
= new PlMail($this, $tpl);
154 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
156 private function correct_emails($email)
158 return preg_replace('!(^|, *)([^<"]+?) *(<[^>]*>)!', '\1"\2" \3', $email);
161 public function addTo($email)
163 $email = $this->correct_emails($email);
164 if (isset($this->_headers
['To'])) {
165 $this->_headers
['To'] .= ", $email";
167 $this->_headers
['To'] = $email;
171 public function addCc($email)
173 return parent
::addCc($this->correct_emails($email));
176 public function addBcc($email)
178 return parent
::addBcc($this->correct_emails($email));
181 public function setFrom($email)
183 return parent
::setFrom($this->correct_emails($email));
186 public function addHeader($hdr,$val)
190 $this->setFrom($val);
194 unset($this->_headers
[$hdr]);
199 unset($this->_headers
[$hdr]);
204 unset($this->_headers
[$hdr]);
209 $this->headers(Array($hdr=>$val));
213 public function assign($var, $value)
215 if (!is_null($this->page
)) {
216 $this->page
->assign($var, $value);
220 public function assign_by_ref($var, &$value)
222 if (!is_null($this->page
)) {
223 $this->page
->assign_by_ref($var, $value);
227 public function register_modifier($var, $callback)
229 if (!is_null($this->page
)) {
230 $this->page
->register_modifier($var, $callback);
234 public function register_function($var, $callback)
236 if (!is_null($this->page
)) {
237 $this->page
->register_function($var, $callback);
241 private function processPage($with_html = true
)
243 $level = error_reporting(0);
244 if (!is_null($this->page
)) {
245 $this->setTxtBody($this->page
->run(false
));
247 $html = trim($this->page
->run(true
));
249 $this->setHtmlBody($html);
253 error_reporting($level);
256 public function send($with_html = true
)
258 $this->processPage($with_html);
259 if (S
::v('forlife')) {
261 $this->addHeader('X-Org-Mail', S
::v('forlife') . '@' . $globals->mail
->domain
);
264 foreach(Array('To', 'Cc', 'Bcc') as $hdr) {
265 if(isset($this->_headers
[$hdr])) {
266 require_once 'Mail/RFC822.php';
267 $parsed = @Mail_RFC822
::parseAddressList($this->_headers
[$hdr]);
268 if (is_array($parsed)) {
269 $addrs = array_merge($addrs, $parsed);
278 foreach($addrs as $a) {
279 $dests[] = "{$a->mailbox}@{$a->host}";
282 // very important to do it in THIS order very precisely.
283 $body = $this->get(array('text_charset' => $this->charset
,
284 'html_charset' => $this->charset
,
285 'head_charset' => $this->charset
));
286 $hdrs = $this->headers();
287 if (empty($hdrs['From'])) {
288 trigger_error('Empty "From", mail not sent', E_USER_WARNING
);
291 return $this->mail
->send($dests, $hdrs, $body);