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 return $this->fetch($this->tpl
);
59 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
60 static public function mail_format($output, &$smarty)
62 return wordwrap("\n".trim($output)."\n",75);
65 static protected function format_addr(&$params)
67 if (isset($params['full'])) {
68 return $params['full'];
69 } elseif (empty($params['text'])) {
70 return $params['addr'];
72 return $params['text'].' <'.$params['addr'].'>';
76 /** template function : from.
77 * {from full=...} for an already formatted address
78 * {from addr=... [text=...]} else
80 public function setFrom($params, &$smarty)
82 $smarty->mailer
->setFrom(PlMail
::format_addr($params));
85 /** template function : to.
86 * {to full=...} for an already formatted address
87 * {to addr=... [text=...]} else
89 public function addTo($params, &$smarty)
91 $smarty->mailer
->addTo(PlMail
::format_addr($params));
94 /** template function : cc.
95 * {cc full=...} for an already formatted address
96 * {cc addr=... [text=...]} else
98 public function addCc($params, &$smarty)
100 $smarty->mailer
->addCc(PlMail
::format_addr($params));
103 /** template function : bcc.
104 * {bcc full=...} for an already formatted address
105 * {bcc addr=... [text=...]} else
107 public function addBcc($params, &$smarty)
109 $smarty->mailer
->addBcc(PlMail
::format_addr($params));
112 /** template function : subject.
115 public function setSubject($params, &$smarty)
117 $smarty->mailer
->setSubject($params['text']);
120 /** template function : add_header.
121 * {add_header name=... value=...}
123 public function addHeader($params, &$smarty)
125 $smarty->mailer
->addHeader($params['name'], $params['value']);
129 require_once('Mail.php');
130 require_once('Mail/mime.php');
132 /** Class for sending inline or multipart-emails.
133 * Based on Diogenes' HermesMailer
135 class PlMailer
extends Mail_Mime
{
138 private $page = null
;
141 function __construct($tpl = null
, $charset = "ISO-8859-15")
143 $this->charset
= $charset;
144 $this->Mail_Mime("\n");
145 $this->mail
= @Mail
::factory('sendmail', Array('sendmail_args' => '-oi'));
146 if (!is_null($tpl)) {
147 $this->page
= new PlMail($this, $tpl);
152 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
154 private function correct_emails($email)
156 return preg_replace('!(^|, *)([^<"]+?) *(<[^>]*>)!', '\1"\2" \3', $email);
159 public function addTo($email)
161 $email = $this->correct_emails($email);
162 if (isset($this->_headers
['To'])) {
163 $this->_headers
['To'] .= ", $email";
165 $this->_headers
['To'] = $email;
169 public function addCc($email)
171 return parent
::addCc($this->correct_emails($email));
174 public function addBcc($email)
176 return parent
::addBcc($this->correct_emails($email));
179 public function setFrom($email)
181 return parent
::setFrom($this->correct_emails($email));
184 public function addHeader($hdr,$val)
188 $this->setFrom($val);
192 unset($this->_headers
[$hdr]);
197 unset($this->_headers
[$hdr]);
202 unset($this->_headers
[$hdr]);
207 $this->headers(Array($hdr=>$val));
211 public function assign($var, $value)
213 if (!is_null($this->page
)) {
214 $this->page
->assign($var, $value);
218 public function assign_by_ref($var, &$value)
220 if (!is_null($this->page
)) {
221 $this->page
->assign_by_ref($var, $value);
225 public function register_modifier($var, $callback)
227 if (!is_null($this->page
)) {
228 $this->page
->register_modifier($var, $callback);
232 public function register_function($var, $callback)
234 if (!is_null($this->page
)) {
235 $this->page
->register_function($var, $callback);
239 private function processPage($with_html = true
)
241 $level = error_reporting(0);
242 if (!is_null($this->page
)) {
243 $this->setTxtBody($this->page
->run(false
));
245 $html = trim($this->page
->run(true
));
247 $this->setHtmlBody($html);
251 error_reporting($level);
254 public function send($with_html = true
)
256 $this->processPage($with_html);
257 if (S
::v('forlife')) {
259 $this->addHeader('X-Org-Mail', S
::v('forlife') . '@' . $globals->mail
->domain
);
262 foreach(Array('To', 'Cc', 'Bcc') as $hdr) {
263 if(isset($this->_headers
[$hdr])) {
264 require_once 'Mail/RFC822.php';
265 $addrs = array_merge($addrs, @Mail_RFC822
::parseAddressList($this->_headers
[$hdr]));
273 foreach($addrs as $a) {
274 $dests[] = "{$a->mailbox}@{$a->host}";
277 // very important to do it in THIS order very precisely.
278 $body = $this->get(array('text_charset' => $this->charset
,
279 'html_charset' => $this->charset
,
280 'head_charset' => $this->charset
));
281 $hdrs = $this->headers();
282 if (empty($hdrs['From'])) {
283 trigger_error('Empty "From", mail not sent', E_USER_WARNING
);
286 return $this->mail
->send($dests, $hdrs, $body);