Put mail smarty cache in a separated directory
[platal.git] / classes / plmailer.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
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
22 require_once('smarty/libs/Smarty.class.php');
23
24 /** Classe de mail avec corps en templates.
25 */
26 class PlMail extends Smarty
27 {
28 private $tpl;
29 private $mailer = null;
30
31 function __construct(&$mailer, $tpl)
32 {
33 global $globals;
34 $this->tpl = $tpl;
35 $this->mailer =& $mailer;
36 $this->caching = false;
37 $this->compile_check = true;
38
39 $this->template_dir = $globals->spoolroot . "/templates/";
40 $this->compile_dir = $globals->spoolroot . "/spool/templates_c/mails/";
41 $this->config_dir = $globals->spoolroot . "/configs/";
42
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'));
50 $this->register_function('add_header', Array($this, 'addHeader'));
51 }
52
53 public function run($html)
54 {
55 $this->assign('html_version', $html);
56 return $this->fetch($this->tpl);
57 }
58
59 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
60 static public function mail_format($output, &$smarty)
61 {
62 return wordwrap("\n".trim($output)."\n",75);
63 }
64
65 static protected function format_addr(&$params)
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
76 /** template function : from.
77 * {from full=...} for an already formatted address
78 * {from addr=... [text=...]} else
79 */
80 public function setFrom($params, &$smarty)
81 {
82 $smarty->mailer->setFrom(PlMail::format_addr($params));
83 }
84
85 /** template function : to.
86 * {to full=...} for an already formatted address
87 * {to addr=... [text=...]} else
88 */
89 public function addTo($params, &$smarty)
90 {
91 $smarty->mailer->addTo(PlMail::format_addr($params));
92 }
93
94 /** template function : cc.
95 * {cc full=...} for an already formatted address
96 * {cc addr=... [text=...]} else
97 */
98 public function addCc($params, &$smarty)
99 {
100 $smarty->mailer->addCc(PlMail::format_addr($params));
101 }
102
103 /** template function : bcc.
104 * {bcc full=...} for an already formatted address
105 * {bcc addr=... [text=...]} else
106 */
107 public function addBcc($params, &$smarty)
108 {
109 $smarty->mailer->addBcc(PlMail::format_addr($params));
110 }
111
112 /** template function : subject.
113 * {subject text=...}
114 */
115 public function setSubject($params, &$smarty)
116 {
117 $smarty->mailer->setSubject($params['text']);
118 }
119
120 /** template function : add_header.
121 * {add_header name=... value=...}
122 */
123 public function addHeader($params, &$smarty)
124 {
125 $smarty->mailer->addHeader($params['name'], $params['value']);
126 }
127 }
128
129 require_once('Mail.php');
130 require_once('Mail/mime.php');
131
132 /** Class for sending inline or multipart-emails.
133 * Based on Diogenes' HermesMailer
134 */
135 class PlMailer extends Mail_Mime {
136
137 private $mail;
138 private $page = null;
139 private $charset;
140
141 function __construct($tpl = null, $charset = "ISO-8859-15")
142 {
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);
148 }
149 }
150
151 /**
152 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
153 */
154 private function correct_emails($email)
155 {
156 return preg_replace('!(^|, *)([^<"]+?) *(<[^>]*>)!', '\1"\2" \3', $email);
157 }
158
159 public function addTo($email)
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
169 public function addCc($email)
170 {
171 return parent::addCc($this->correct_emails($email));
172 }
173
174 public function addBcc($email)
175 {
176 return parent::addBcc($this->correct_emails($email));
177 }
178
179 public function setFrom($email)
180 {
181 return parent::setFrom($this->correct_emails($email));
182 }
183
184 public function addHeader($hdr,$val)
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
211 public function assign($var, $value)
212 {
213 if (!is_null($this->page)) {
214 $this->page->assign($var, $value);
215 }
216 }
217
218 public function assign_by_ref($var, &$value)
219 {
220 if (!is_null($this->page)) {
221 $this->page->assign_by_ref($var, $value);
222 }
223 }
224
225 public function register_modifier($var, $callback)
226 {
227 if (!is_null($this->page)) {
228 $this->page->register_modifier($var, $callback);
229 }
230 }
231
232 public function register_function($var, $callback)
233 {
234 if (!is_null($this->page)) {
235 $this->page->register_function($var, $callback);
236 }
237 }
238
239 private function processPage($with_html = true)
240 {
241 $level = error_reporting(0);
242 if (!is_null($this->page)) {
243 $this->setTxtBody($this->page->run(false));
244 if ($with_html) {
245 $html = trim($this->page->run(true));
246 if (!empty($html)) {
247 $this->setHtmlBody($html);
248 }
249 }
250 }
251 error_reporting($level);
252 }
253
254 public function send($with_html = true)
255 {
256 $this->processPage($with_html);
257 if (S::v('forlife')) {
258 global $globals;
259 $this->addHeader('X-Org-Mail', S::v('forlife') . '@' . $globals->mail->domain);
260 }
261 $addrs = Array();
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]));
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();
282 if (empty($hdrs['From'])) {
283 trigger_error('Empty "From", mail not sent', E_USER_WARNING);
284 return false;
285 }
286 return $this->mail->send($dests, $hdrs, $body);
287 }
288 }
289
290 ?>