d220ad75467d61095fa3a4f0cf7dd07946bdee64
[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($tpl)
32 {
33 global $globals;
34 $this->tpl = $tpl;
35 $this->caching = false;
36 $this->compile_check = true;
37
38 $this->template_dir = $globals->spoolroot . "/templates/";
39 $this->compile_dir = $globals->spoolroot . "/spool/mails_c/";
40 $this->config_dir = $globals->spoolroot . "/configs/";
41
42
43 $this->register_outputfilter(Array($this, 'mail_format'));
44 $this->register_function('from', Array($this, 'setFrom'));
45 $this->register_function('to', Array($this, 'addTo'));
46 $this->register_function('cc', Array($this, 'addCc'));
47 $this->register_function('bcc', Array($this, 'addBcc'));
48 $this->register_function('subject', Array($this, 'setSubject'));
49 $this->register_function('add_header', Array($this, 'addHeader'));
50 }
51
52 public static function &get(&$mailer, $tpl)
53 {
54 static $plmail;
55 if (!isset($plmail) || $plmail->tpl != $tpl) {
56 $plmail = new PlMail($tpl);
57 }
58 $plmail->mailer =& $mailer;
59 return $plmail;
60 }
61
62 public function run($html)
63 {
64 $this->assign('html_version', $html);
65 $text = $this->fetch($this->tpl);
66 return $text;
67 }
68
69 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
70 static public function mail_format($output, &$smarty)
71 {
72 return wordwrap("\n".trim($output)."\n",75);
73 }
74
75 static protected function format_addr(&$params)
76 {
77 if (isset($params['full'])) {
78 return $params['full'];
79 } elseif (empty($params['text'])) {
80 return $params['addr'];
81 } else {
82 return $params['text'].' <'.$params['addr'].'>';
83 }
84 }
85
86 /** template function : from.
87 * {from full=...} for an already formatted address
88 * {from addr=... [text=...]} else
89 */
90 public function setFrom($params, &$smarty)
91 {
92 $smarty->mailer->setFrom(PlMail::format_addr($params));
93 }
94
95 /** template function : to.
96 * {to full=...} for an already formatted address
97 * {to addr=... [text=...]} else
98 */
99 public function addTo($params, &$smarty)
100 {
101 $smarty->mailer->addTo(PlMail::format_addr($params));
102 }
103
104 /** template function : cc.
105 * {cc full=...} for an already formatted address
106 * {cc addr=... [text=...]} else
107 */
108 public function addCc($params, &$smarty)
109 {
110 $smarty->mailer->addCc(PlMail::format_addr($params));
111 }
112
113 /** template function : bcc.
114 * {bcc full=...} for an already formatted address
115 * {bcc addr=... [text=...]} else
116 */
117 public function addBcc($params, &$smarty)
118 {
119 $smarty->mailer->addBcc(PlMail::format_addr($params));
120 }
121
122 /** template function : subject.
123 * {subject text=...}
124 */
125 public function setSubject($params, &$smarty)
126 {
127 $smarty->mailer->setSubject($params['text']);
128 }
129
130 /** template function : add_header.
131 * {add_header name=... value=...}
132 */
133 public function addHeader($params, &$smarty)
134 {
135 $smarty->mailer->addHeader($params['name'], $params['value']);
136 }
137 }
138
139 require_once('Mail.php');
140 require_once('Mail/mime.php');
141
142 /** Class for sending inline or multipart-emails.
143 * Based on Diogenes' HermesMailer
144 */
145 class PlMailer extends Mail_Mime {
146
147 private $mail;
148 private $page = null;
149 private $charset;
150
151 function __construct($tpl = null, $charset = "UTF-8")
152 {
153 $this->charset = $charset;
154 $this->Mail_Mime("\n");
155 $this->mail = Mail::factory('sendmail', Array('sendmail_args' => '-oi'));
156 if (!is_null($tpl)) {
157 $this->page =& PlMail::get($this, $tpl);
158 }
159 }
160
161 /**
162 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
163 */
164 private function correct_emails($email)
165 {
166 return preg_replace('!(^|, *)([^<"]+?) *(<[^>]*>)!u', '\1"\2" \3', $email);
167 }
168
169 public function addTo($email)
170 {
171 $email = $this->correct_emails($email);
172 if (isset($this->_headers['To'])) {
173 $this->_headers['To'] .= ", $email";
174 } else {
175 $this->_headers['To'] = $email;
176 }
177 }
178
179 public function addCc($email)
180 {
181 return parent::addCc($this->correct_emails($email));
182 }
183
184 public function addBcc($email)
185 {
186 return parent::addBcc($this->correct_emails($email));
187 }
188
189 public function setFrom($email)
190 {
191 return parent::setFrom($this->correct_emails($email));
192 }
193
194 public function addHeader($hdr,$val)
195 {
196 switch($hdr) {
197 case 'From':
198 $this->setFrom($val);
199 break;
200
201 case 'To':
202 unset($this->_headers[$hdr]);
203 $this->addTo($val);
204 break;
205
206 case 'Cc':
207 unset($this->_headers[$hdr]);
208 $this->addCc($val);
209 break;
210
211 case 'Bcc':
212 unset($this->_headers[$hdr]);
213 $this->addBcc($val);
214 break;
215
216 default:
217 $this->headers(Array($hdr=>$val));
218 }
219 }
220
221 public function addUploadAttachment(PlUpload &$upload, $name)
222 {
223 $encoding = $upload->isType('text') ? 'quoted-printable' : 'base64';
224 $this->addAttachment($upload->getContents(), $upload->contentType(), $name, false, $encoding);
225 }
226
227 public function assign($var, $value)
228 {
229 if (!is_null($this->page)) {
230 $this->page->assign($var, $value);
231 }
232 }
233
234 public function assign_by_ref($var, &$value)
235 {
236 if (!is_null($this->page)) {
237 $this->page->assign_by_ref($var, $value);
238 }
239 }
240
241 public function register_modifier($var, $callback)
242 {
243 if (!is_null($this->page)) {
244 $this->page->register_modifier($var, $callback);
245 }
246 }
247
248 public function register_function($var, $callback)
249 {
250 if (!is_null($this->page)) {
251 $this->page->register_function($var, $callback);
252 }
253 }
254
255 private function processPage($with_html = true)
256 {
257 $level = error_reporting(0);
258 if (!is_null($this->page)) {
259 $this->setTxtBody($this->page->run(false));
260 if ($with_html) {
261 $html = trim($this->page->run(true));
262 if (!empty($html)) {
263 $this->setHtmlBody($html);
264 }
265 }
266 }
267 error_reporting($level);
268 }
269
270 public function send($with_html = true)
271 {
272 $this->processPage($with_html);
273 if (S::v('forlife')) {
274 global $globals;
275 $this->addHeader('X-Org-Mail', S::v('forlife') . '@' . $globals->mail->domain);
276 }
277 $addrs = Array();
278 foreach(Array('To', 'Cc', 'Bcc') as $hdr) {
279 if(isset($this->_headers[$hdr])) {
280 require_once 'Mail/RFC822.php';
281 $parsed = @Mail_RFC822::parseAddressList($this->_headers[$hdr]);
282 if (is_array($parsed)) {
283 $addrs = array_merge($addrs, $parsed);
284 }
285 }
286 }
287 if(empty($addrs)) {
288 return false;
289 }
290
291 $dests = Array();
292 foreach($addrs as $a) {
293 $dests[] = "{$a->mailbox}@{$a->host}";
294 }
295
296 // very important to do it in THIS order very precisely.
297 $body = $this->get(array('text_charset' => $this->charset,
298 'text_encoding' => '8bit',
299 'html_charset' => $this->charset,
300 'head_charset' => $this->charset));
301 $hdrs = $this->headers();
302 if (empty($hdrs['From'])) {
303 trigger_error('Empty "From", mail not sent', E_USER_WARNING);
304 return false;
305 }
306 return $this->mail->send($dests, $hdrs, $body);
307 }
308 }
309
310 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
311 ?>