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