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

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