Backports
[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/mails_c/";
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 $text = $this->fetch($this->tpl);
57 fix_encoding($text);
58 return $text;
59 }
60
61 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
62 static public function mail_format($output, &$smarty)
63 {
64 return wordwrap("\n".trim($output)."\n",75);
65 }
66
67 static protected function format_addr(&$params)
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
78 /** template function : from.
79 * {from full=...} for an already formatted address
80 * {from addr=... [text=...]} else
81 */
82 public function setFrom($params, &$smarty)
83 {
84 $smarty->mailer->setFrom(PlMail::format_addr($params));
85 }
86
87 /** template function : to.
88 * {to full=...} for an already formatted address
89 * {to addr=... [text=...]} else
90 */
91 public function addTo($params, &$smarty)
92 {
93 $smarty->mailer->addTo(PlMail::format_addr($params));
94 }
95
96 /** template function : cc.
97 * {cc full=...} for an already formatted address
98 * {cc addr=... [text=...]} else
99 */
100 public function addCc($params, &$smarty)
101 {
102 $smarty->mailer->addCc(PlMail::format_addr($params));
103 }
104
105 /** template function : bcc.
106 * {bcc full=...} for an already formatted address
107 * {bcc addr=... [text=...]} else
108 */
109 public function addBcc($params, &$smarty)
110 {
111 $smarty->mailer->addBcc(PlMail::format_addr($params));
112 }
113
114 /** template function : subject.
115 * {subject text=...}
116 */
117 public function setSubject($params, &$smarty)
118 {
119 $smarty->mailer->setSubject($params['text']);
120 }
121
122 /** template function : add_header.
123 * {add_header name=... value=...}
124 */
125 public function addHeader($params, &$smarty)
126 {
127 $smarty->mailer->addHeader($params['name'], $params['value']);
128 }
129 }
130
131 require_once('Mail.php');
132 require_once('Mail/mime.php');
133
134 /** Class for sending inline or multipart-emails.
135 * Based on Diogenes' HermesMailer
136 */
137 class PlMailer extends Mail_Mime {
138
139 private $mail;
140 private $page = null;
141 private $charset;
142
143 function __construct($tpl = null, $charset = "ISO-8859-15")
144 {
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);
150 }
151 }
152
153 /**
154 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
155 */
156 private function correct_emails($email)
157 {
158 return preg_replace('!(^|, *)([^<"]+?) *(<[^>]*>)!', '\1"\2" \3', $email);
159 }
160
161 public function addTo($email)
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
171 public function addCc($email)
172 {
173 return parent::addCc($this->correct_emails($email));
174 }
175
176 public function addBcc($email)
177 {
178 return parent::addBcc($this->correct_emails($email));
179 }
180
181 public function setFrom($email)
182 {
183 return parent::setFrom($this->correct_emails($email));
184 }
185
186 public function addHeader($hdr,$val)
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
213 public function assign($var, $value)
214 {
215 if (!is_null($this->page)) {
216 $this->page->assign($var, $value);
217 }
218 }
219
220 public function assign_by_ref($var, &$value)
221 {
222 if (!is_null($this->page)) {
223 $this->page->assign_by_ref($var, $value);
224 }
225 }
226
227 public function register_modifier($var, $callback)
228 {
229 if (!is_null($this->page)) {
230 $this->page->register_modifier($var, $callback);
231 }
232 }
233
234 public function register_function($var, $callback)
235 {
236 if (!is_null($this->page)) {
237 $this->page->register_function($var, $callback);
238 }
239 }
240
241 private function processPage($with_html = true)
242 {
243 $level = error_reporting(0);
244 if (!is_null($this->page)) {
245 $this->setTxtBody($this->page->run(false));
246 if ($with_html) {
247 $html = trim($this->page->run(true));
248 if (!empty($html)) {
249 $this->setHtmlBody($html);
250 }
251 }
252 }
253 error_reporting($level);
254 }
255
256 public function send($with_html = true)
257 {
258 $this->processPage($with_html);
259 if (S::v('forlife')) {
260 global $globals;
261 $this->addHeader('X-Org-Mail', S::v('forlife') . '@' . $globals->mail->domain);
262 }
263 $addrs = Array();
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);
270 }
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();
287 if (empty($hdrs['From'])) {
288 trigger_error('Empty "From", mail not sent', E_USER_WARNING);
289 return false;
290 }
291 return $this->mail->send($dests, $hdrs, $body);
292 }
293 }
294
295 ?>