XOOPS Docs - class
[ class tree: class ] [ index: class ] [ all elements ]

Source for file xoopsmailer.php

Documentation is available at xoopsmailer.php

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

XOOPS Docs generated by phpDocumentor