Put mail smarty cache in a separated directory
[platal.git] / classes / plmailer.php
CommitLineData
b98794ca 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 Polytechnique.org *
b98794ca 4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22require_once('smarty/libs/Smarty.class.php');
23
b98794ca 24/** Classe de mail avec corps en templates.
25 */
26class PlMail extends Smarty
27{
28 private $tpl;
29 private $mailer = null;
30
5bc6e9e6 31 function __construct(&$mailer, $tpl)
b98794ca 32 {
33 global $globals;
34 $this->tpl = $tpl;
5bc6e9e6 35 $this->mailer =& $mailer;
b98794ca 36 $this->caching = false;
37 $this->compile_check = true;
38
39 $this->template_dir = $globals->spoolroot . "/templates/";
98441260 40 $this->compile_dir = $globals->spoolroot . "/spool/templates_c/mails/";
b98794ca 41 $this->config_dir = $globals->spoolroot . "/configs/";
98441260 42
b98794ca 43
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'));
11f44323 50 $this->register_function('add_header', Array($this, 'addHeader'));
b98794ca 51 }
52
5bc6e9e6 53 public function run($html)
b98794ca 54 {
55 $this->assign('html_version', $html);
56 return $this->fetch($this->tpl);
57 }
58
b98794ca 59 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
5bc6e9e6 60 static public function mail_format($output, &$smarty)
b98794ca 61 {
62 return wordwrap("\n".trim($output)."\n",75);
63 }
64
5bc6e9e6 65 static protected function format_addr(&$params)
b98794ca 66 {
67 if (isset($params['full'])) {
68 return $params['full'];
69 } elseif (empty($params['text'])) {
70 return $params['addr'];
71 } else {
72 return $params['text'].' <'.$params['addr'].'>';
73 }
74 }
75
b98794ca 76 /** template function : from.
77 * {from full=...} for an already formatted address
78 * {from addr=... [text=...]} else
79 */
5bc6e9e6 80 public function setFrom($params, &$smarty)
b98794ca 81 {
5bc6e9e6 82 $smarty->mailer->setFrom(PlMail::format_addr($params));
b98794ca 83 }
84
b98794ca 85 /** template function : to.
86 * {to full=...} for an already formatted address
87 * {to addr=... [text=...]} else
88 */
5bc6e9e6 89 public function addTo($params, &$smarty)
b98794ca 90 {
5bc6e9e6 91 $smarty->mailer->addTo(PlMail::format_addr($params));
b98794ca 92 }
93
b98794ca 94 /** template function : cc.
95 * {cc full=...} for an already formatted address
96 * {cc addr=... [text=...]} else
97 */
5bc6e9e6 98 public function addCc($params, &$smarty)
b98794ca 99 {
5bc6e9e6 100 $smarty->mailer->addCc(PlMail::format_addr($params));
b98794ca 101 }
102
b98794ca 103 /** template function : bcc.
104 * {bcc full=...} for an already formatted address
105 * {bcc addr=... [text=...]} else
106 */
5bc6e9e6 107 public function addBcc($params, &$smarty)
b98794ca 108 {
5bc6e9e6 109 $smarty->mailer->addBcc(PlMail::format_addr($params));
b98794ca 110 }
111
b98794ca 112 /** template function : subject.
113 * {subject text=...}
114 */
5bc6e9e6 115 public function setSubject($params, &$smarty)
b98794ca 116 {
117 $smarty->mailer->setSubject($params['text']);
118 }
119
11f44323 120 /** template function : add_header.
121 * {add_header name=... value=...}
122 */
5bc6e9e6 123 public function addHeader($params, &$smarty)
11f44323 124 {
125 $smarty->mailer->addHeader($params['name'], $params['value']);
126 }
b98794ca 127}
b98794ca 128
129require_once('Mail.php');
130require_once('Mail/mime.php');
131
b98794ca 132/** Class for sending inline or multipart-emails.
5bc6e9e6 133 * Based on Diogenes' HermesMailer
b98794ca 134 */
135class PlMailer extends Mail_Mime {
136
137 private $mail;
138 private $page = null;
139 private $charset;
b98794ca 140
5bc6e9e6 141 function __construct($tpl = null, $charset = "ISO-8859-15")
b98794ca 142 {
143 $this->charset = $charset;
144 $this->Mail_Mime("\n");
5bc6e9e6 145 $this->mail = @Mail::factory('sendmail', Array('sendmail_args' => '-oi'));
b98794ca 146 if (!is_null($tpl)) {
147 $this->page = new PlMail($this, $tpl);
148 }
149 }
150
b98794ca 151 /**
152 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
153 */
b98794ca 154 private function correct_emails($email)
155 {
5bc6e9e6 156 return preg_replace('!(^|, *)([^<"]+?) *(<[^>]*>)!', '\1"\2" \3', $email);
b98794ca 157 }
158
5bc6e9e6 159 public function addTo($email)
b98794ca 160 {
161 $email = $this->correct_emails($email);
162 if (isset($this->_headers['To'])) {
163 $this->_headers['To'] .= ", $email";
164 } else {
165 $this->_headers['To'] = $email;
166 }
167 }
168
5bc6e9e6 169 public function addCc($email)
b98794ca 170 {
171 return parent::addCc($this->correct_emails($email));
172 }
173
5bc6e9e6 174 public function addBcc($email)
b98794ca 175 {
176 return parent::addBcc($this->correct_emails($email));
177 }
178
5bc6e9e6 179 public function setFrom($email)
b98794ca 180 {
181 return parent::setFrom($this->correct_emails($email));
182 }
183
5bc6e9e6 184 public function addHeader($hdr,$val)
b98794ca 185 {
186 switch($hdr) {
187 case 'From':
188 $this->setFrom($val);
189 break;
190
191 case 'To':
192 unset($this->_headers[$hdr]);
193 $this->addTo($val);
194 break;
195
196 case 'Cc':
197 unset($this->_headers[$hdr]);
198 $this->addCc($val);
199 break;
200
201 case 'Bcc':
202 unset($this->_headers[$hdr]);
203 $this->addBcc($val);
204 break;
205
206 default:
207 $this->headers(Array($hdr=>$val));
208 }
209 }
210
5bc6e9e6 211 public function assign($var, $value)
b98794ca 212 {
213 if (!is_null($this->page)) {
214 $this->page->assign($var, $value);
215 }
216 }
217
5bc6e9e6 218 public function assign_by_ref($var, &$value)
11f44323 219 {
220 if (!is_null($this->page)) {
221 $this->page->assign_by_ref($var, $value);
222 }
223 }
224
5bc6e9e6 225 public function register_modifier($var, $callback)
11f44323 226 {
227 if (!is_null($this->page)) {
228 $this->page->register_modifier($var, $callback);
229 }
230 }
231
5bc6e9e6 232 public function register_function($var, $callback)
11f44323 233 {
234 if (!is_null($this->page)) {
235 $this->page->register_function($var, $callback);
236 }
237 }
238
11f44323 239 private function processPage($with_html = true)
b98794ca 240 {
98441260 241 $level = error_reporting(0);
b98794ca 242 if (!is_null($this->page)) {
243 $this->setTxtBody($this->page->run(false));
11f44323 244 if ($with_html) {
245 $html = trim($this->page->run(true));
246 if (!empty($html)) {
247 $this->setHtmlBody($html);
248 }
b98794ca 249 }
250 }
98441260 251 error_reporting($level);
b98794ca 252 }
253
5bc6e9e6 254 public function send($with_html = true)
b98794ca 255 {
11f44323 256 $this->processPage($with_html);
b98794ca 257 if (S::v('forlife')) {
315fa6ae 258 global $globals;
259 $this->addHeader('X-Org-Mail', S::v('forlife') . '@' . $globals->mail->domain);
b98794ca 260 }
261 $addrs = Array();
262 foreach(Array('To', 'Cc', 'Bcc') as $hdr) {
263 if(isset($this->_headers[$hdr])) {
264 require_once 'Mail/RFC822.php';
5bc6e9e6 265 $addrs = array_merge($addrs, @Mail_RFC822::parseAddressList($this->_headers[$hdr]));
b98794ca 266 }
267 }
268 if(empty($addrs)) {
269 return false;
270 }
271
272 $dests = Array();
273 foreach($addrs as $a) {
274 $dests[] = "{$a->mailbox}@{$a->host}";
275 }
276
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();
434cd6dd 282 if (empty($hdrs['From'])) {
283 trigger_error('Empty "From", mail not sent', E_USER_WARNING);
284 return false;
285 }
b98794ca 286 return $this->mail->send($dests, $hdrs, $body);
287 }
b98794ca 288}
289
b98794ca 290?>