source: temp/test-xoops.ec-cube.net/html/class/xoopsmailer.php @ 1312

Revision 1312, 13.9 KB checked in by uehara, 19 years ago (diff)
Line 
1<?php
2// $Id: xoopsmailer.php,v 1.3 2006/05/01 02:37:24 onokazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: Kazumi Ono (AKA onokazu)                                          //
28// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29// Project: The XOOPS Project                                                //
30// ------------------------------------------------------------------------- //
31if (isset($GLOBALS['xoopsConfig']['language']) && file_exists(XOOPS_ROOT_PATH.'/language/'.$GLOBALS['xoopsConfig']['language'].'/mail.php')) {
32    include_once XOOPS_ROOT_PATH.'/language/'.$GLOBALS['xoopsConfig']['language'].'/mail.php';
33} else {
34    include_once XOOPS_ROOT_PATH.'/language/english/mail.php';
35}
36
37/**
38 * The new Multimailer class that will carry out the actual sending and will later replace this class.
39 * If you're writing new code, please use that class instead.
40 */
41include_once(XOOPS_ROOT_PATH."/class/mail/xoopsmultimailer.php");
42
43
44/**
45 * Class for sending mail.
46 *
47 * Changed to use the facilities of  {@link XoopsMultiMailer}
48 *
49 * @deprecated  use {@link XoopsMultiMailer} instead.
50 *
51 * @package     class
52 * @subpackage  mail
53 *
54 * @author      Kazumi Ono  <[email protected]>
55 * @copyright   (c) 2000-2003 The Xoops Project - www.xoops.org
56 */
57class XoopsMailer
58{
59    /**
60     * reference to a {@link XoopsMultiMailer}
61     *
62     * @var     XoopsMultiMailer
63     * @access  private
64     * @since   21.02.2003 14:14:13
65     */
66    var $multimailer;
67
68    // sender email address
69    // private
70    var $fromEmail;
71
72    // sender name
73    // private
74    var $fromName;
75
76    // RMV-NOTIFY
77    // sender UID
78    // private
79    var $fromUser;
80
81    // array of user class objects
82    // private
83    var $toUsers;
84
85    // array of email addresses
86    // private
87    var $toEmails;
88
89    // custom headers
90    // private
91    var $headers;
92
93    // subjet of mail
94    // private
95    var $subject;
96
97    // body of mail
98    // private
99    var $body;
100
101    // error messages
102    // private
103    var $errors;
104
105    // messages upon success
106    // private
107    var $success;
108
109    // private
110    var $isMail;
111
112    // private
113    var $isPM;
114
115    // private
116    var $assignedTags;
117
118    // private
119    var $template;
120
121    // private
122    var $templatedir;
123
124    // protected
125    var $charSet = 'iso-8859-1';
126
127    // protected
128    var $encoding = '8bit';
129
130    function XoopsMailer()
131    {
132
133        $this->multimailer = new XoopsMultiMailer();
134        $this->reset();
135    }
136
137    // public
138    // reset all properties to default
139    function reset()
140    {
141        $this->fromEmail = "";
142        $this->fromName = "";
143        $this->fromUser = null; // RMV-NOTIFY
144        $this->priority = '';
145        $this->toUsers = array();
146        $this->toEmails = array();
147        $this->headers = array();
148        $this->subject = "";
149        $this->body = "";
150        $this->errors = array();
151        $this->success = array();
152        $this->isMail = false;
153        $this->isPM = false;
154        $this->assignedTags = array();
155        $this->template = "";
156        $this->templatedir = "";
157        // Change below to \r\n if you have problem sending mail
158        $this->LE ="\n";
159    }
160
161    // public
162    function setTemplateDir($value)
163    {
164        if ( substr($value, -1, 1) != "/" ) {
165            $value .= "/";
166        }
167        $this->templatedir = $value;
168    }
169
170    // public
171    function setTemplate($value)
172    {
173        $this->template = $value;
174    }
175
176    // pupblic
177    function setFromEmail($value)
178    {
179        if ($this->_checkValidEmail($value)) {
180            $this->fromEmail = trim($value);
181        }
182    }
183
184    // public
185    function setFromName($value)
186    {
187        if ($this->_checkNoneContorolChar($value)) {
188            $this->fromName = trim($value);
189        }
190    }
191
192    // RMV-NOTIFY
193    // public
194    function setFromUser(&$user)
195    {
196        if ( strtolower(get_class($user)) == "xoopsuser" ) {
197            $this->fromUser =& $user;
198        }
199    }
200
201    // public
202    function setPriority($value)
203    {
204        if ($this->_checkNoneContorolChar($value)) {
205            $this->priority = trim($value);
206        }
207    }
208
209
210    // public
211    function setSubject($value)
212    {
213        if ($this->_checkNoneContorolChar($value)) {
214            $this->subject = trim($value);
215        }
216    }
217
218    // public
219    function setBody($value)
220    {
221        $this->body = trim($value);
222    }
223
224    // public
225    function useMail()
226    {
227        $this->isMail = true;
228    }
229
230    // public
231    function usePM()
232    {
233        $this->isPM = true;
234    }
235
236    // public
237    function send($debug = false)
238    {
239        global $xoopsConfig;
240        if ( $this->body == "" && $this->template == "" ) {
241            if ($debug) {
242                $this->errors[] = _MAIL_MSGBODY;
243            }
244            return false;
245        } elseif ( $this->template != "" ) {
246            $path = ( $this->templatedir != "" ) ? $this->templatedir."".$this->template : (XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/mail_template/".$this->template);
247            if ( !($fd = @fopen($path, 'r')) ) {
248                if ($debug) {
249                    $this->errors[] = _MAIL_FAILOPTPL;
250                }
251                        return false;
252                }
253            $this->setBody(fread($fd, filesize($path)));
254        }
255
256        // for sending mail only
257        if ( $this->isMail  || !empty($this->toEmails) ) {
258            if (!empty($this->priority)) {
259                $this->headers[] = "X-Priority: " . $this->priority;
260            }
261            $this->headers[] = "X-Mailer: XOOPS Cube";
262            if (!empty($this->fromEmail)) {
263                $this->headers[] = "Return-Path: ".$this->fromEmail;
264            }
265            $headers = join($this->LE, $this->headers);
266        }
267
268// TODO: we should have an option of no-reply for private messages and emails
269// to which we do not accept replies.  e.g. the site admin doesn't want a
270// a lot of message from people trying to unsubscribe.  Just make sure to
271// give good instructions in the message.
272
273        // add some standard tags (user-dependent tags are included later)
274        global $xoopsConfig;
275        $this->assign ('X_ADMINMAIL', $xoopsConfig['adminmail']);
276        $this->assign ('X_SITENAME', $xoopsConfig['sitename']);
277        $this->assign ('X_SITEURL', XOOPS_URL);
278        // TODO: also X_ADMINNAME??
279        // TODO: X_SIGNATURE, X_DISCLAIMER ?? - these are probably best
280        //  done as includes if mail templates ever get this sophisticated
281
282        // replace tags with actual values
283        foreach ( $this->assignedTags as $k => $v ) {
284            $this->body = str_replace("{".$k."}", $v, $this->body);
285            $this->subject = str_replace("{".$k."}", $v, $this->subject);
286        }
287        $this->body = str_replace("\r\n", "\n", $this->body);
288        $this->body = str_replace("\r", "\n", $this->body);
289        $this->body = str_replace("\n", $this->LE, $this->body);
290
291        // send mail to specified mail addresses, if any
292        foreach ( $this->toEmails as $mailaddr ) {
293            if ( !$this->sendMail($mailaddr, $this->subject, $this->body, $headers) ) {
294                if ($debug) {
295                    $this->errors[] = sprintf(_MAIL_SENDMAILNG, $mailaddr);
296                }
297            } else {
298                if ($debug) {
299                    $this->success[] = sprintf(_MAIL_MAILGOOD, $mailaddr);
300                }
301            }
302        }
303
304        // send message to specified users, if any
305
306        // NOTE: we don't send to LIST of recipients, because the tags
307        // below are dependent on the user identity; i.e. each user
308        // receives (potentially) a different message
309
310        foreach ( $this->toUsers as $user ) {
311            // set some user specific variables
312            $subject = str_replace("{X_UNAME}", $user->getVar("uname"), $this->subject );
313            $text = str_replace("{X_UID}", $user->getVar("uid"), $this->body );
314            $text = str_replace("{X_UEMAIL}", $user->getVar("email"), $text );
315            $text = str_replace("{X_UNAME}", $user->getVar("uname"), $text );
316            $text = str_replace("{X_UACTLINK}", XOOPS_URL."/user.php?op=actv&id=".$user->getVar("uid")."&actkey=".$user->getVar('actkey'), $text );
317            // send mail
318            if ( $this->isMail ) {
319                if ( !$this->sendMail($user->getVar("email"), $subject, $text, $headers) ) {
320                    if ($debug) {
321                        $this->errors[] = sprintf(_MAIL_SENDMAILNG, $user->getVar("uname"));
322                    }
323                } else {
324                    if ($debug) {
325                        $this->success[] = sprintf(_MAIL_MAILGOOD, $user->getVar("uname"));
326                    }
327                }
328            }
329            // send private message
330            if ( $this->isPM ) {
331                if ( !$this->sendPM($user->getVar("uid"), $subject, $text) ) {
332                    if ($debug) {
333                        $this->errors[] = sprintf(_MAIL_SENDPMNG, $user->getVar("uname"));
334                    }
335                } else {
336                    if ($debug) {
337                        $this->success[] = sprintf(_MAIL_PMGOOD, $user->getVar("uname"));
338                    }
339                }
340            }
341            flush();
342        }
343        if ( count($this->errors) > 0 ) {
344            return false;
345        }
346        return true;
347    }
348
349    // private
350    function sendPM($uid, $subject, $body)
351    {
352        global $xoopsUser;
353        $pm_handler =& xoops_gethandler('privmessage');
354        $pm =& $pm_handler->create();
355        $pm->setVar("subject", $subject);
356        // RMV-NOTIFY
357        $pm->setVar('from_userid', !empty($this->fromUser) ? $this->fromUser->getVar('uid') : $xoopsUser->getVar('uid'));
358        $pm->setVar("msg_text", $body);
359        $pm->setVar("to_userid", $uid);
360        if (!$pm_handler->insert($pm)) {
361            return false;
362        }
363        return true;
364    }
365
366    /**
367     * Send email
368     *
369     * Uses the new XoopsMultiMailer
370     *
371     * @param   string
372     * @param   string
373     * @param   string
374     * @return  boolean FALSE on error.
375     */
376
377    function sendMail($email, $subject, $body, $headers)
378    {
379        $subject = $this->encodeSubject($subject);
380        $this->encodeBody($body);
381        $this->multimailer->ClearAllRecipients();
382        $this->multimailer->AddAddress($email);
383        $this->multimailer->Subject = $subject;
384        $this->multimailer->Body = $body;
385        $this->multimailer->CharSet = $this->charSet;
386        $this->multimailer->Encoding = $this->encoding;
387        if (!empty($this->fromName)) {
388            $this->multimailer->FromName = $this->encodeFromName($this->fromName);
389        }
390        if (!empty($this->fromEmail)) {
391            $this->multimailer->From = $this->fromEmail;
392        }
393        $this->multimailer->ClearCustomHeaders();
394        foreach ($this->headers as $header) {
395            $this->multimailer->AddCustomHeader($header);
396        }
397        if (!$this->multimailer->Send()) {
398            $this->errors[] = $this->multimailer->ErrorInfo;
399            return FALSE;
400        }
401        return TRUE;
402    }
403
404    // public
405    function getErrors($ashtml = true)
406    {
407        if ( !$ashtml ) {
408            return $this->errors;
409        } else {
410            if ( !empty($this->errors) ) {
411                $ret = "<h4>"._ERRORS."</h4>";
412                foreach ( $this->errors as $error ) {
413                    $ret .= $error."<br />";
414                }
415            } else {
416                $ret = "";
417            }
418            return $ret;
419        }
420    }
421
422    // public
423    function getSuccess($ashtml = true)
424    {
425        if ( !$ashtml ) {
426            return $this->success;
427        } else {
428            $ret = "";
429            if ( !empty($this->success) ) {
430                foreach ( $this->success as $suc ) {
431                    $ret .= $suc."<br />";
432                }
433            }
434            return $ret;
435        }
436    }
437
438    // public
439    function assign($tag, $value=null)
440    {
441        if ( is_array($tag) ) {
442            foreach ( $tag as $k => $v ) {
443                $this->assign($k, $v);
444            }
445        } else {
446            if ( !empty($tag) && isset($value) ) {
447                $tag = strtoupper(trim($tag));
448// RMV-NOTIFY
449// TEMPORARY FIXME: until the X_tags are all in here
450//              if ( substr($tag, 0, 2) != "X_" ) {
451                    $this->assignedTags[$tag] = $value;
452//              }
453            }
454        }
455    }
456
457    // public
458    function addHeaders($value)
459    {
460        if ($this->_checkNoneContorolChar($value)) {
461            $this->headers[] = trim($value).$this->LE;
462        }
463    }
464
465    // public
466    function setToEmails($email)
467    {
468        if ( !is_array($email) ) {
469            if ($this->_checkValidEmail($email)) {
470                array_push($this->toEmails, $email);
471            }
472        } else {
473            foreach ( $email as $e) {
474                $this->setToEmails($e);
475            }
476        }
477    }
478
479    // public
480    function setToUsers(&$user)
481    {
482        if ( !is_array($user) ) {
483            if ( strtolower(get_class($user)) == "xoopsuser" ) {
484                array_push($this->toUsers, $user);
485            }
486        } else {
487            foreach ( $user as $u) {
488                $this->setToUsers($u);
489            }
490        }
491    }
492
493    // public
494    function setToGroups($group)
495    {
496        if ( !is_array($group) ) {
497            if ( strtolower(get_class($group)) == "xoopsgroup" ) {
498                $member_handler =& xoops_gethandler('member');
499                $groups=&$member_handler->getUsersByGroup($group->getVar('groupid'),true);
500                $this->setToUsers($groups, true);
501            }
502        } else {
503            foreach ($group as $g) {
504                $this->setToGroups($g);
505            }
506        }
507    }
508
509    // abstract
510    // to be overidden by lang specific mail class, if needed
511    function encodeFromName($text)
512    {
513        return $text;
514    }
515
516    // abstract
517    // to be overidden by lang specific mail class, if needed
518    function encodeSubject($text)
519    {
520        return $text;
521    }
522
523    // abstract
524    // to be overidden by lang specific mail class, if needed
525    function encodeBody(&$text)
526    {
527
528    }
529   
530    function _checkNoneContorolChar($text) {
531        if (preg_match("/[\\0-\\31]/", $text)) {
532            $this->errors[] = "Invalid Mail Header String."; //ToDo : Use message catalog
533            return false;
534        }
535        return true;
536    }
537    function _checkValidEmail($addr) {
538        if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i",$addr) ) {
539            $this->errors[] = "Invalid Mail Address Format."; //ToDo : Use message catalog;
540            return false;
541        }
542        return true;
543    }
544}
545?>
Note: See TracBrowser for help on using the repository browser.