home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / class / xoopsmailer.php < prev    next >
Encoding:
PHP Script  |  2007-09-09  |  13.7 KB  |  521 lines

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