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