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