Ignore:
Timestamp:
Feb 28, 2016, 10:54:30 AM (9 years ago)
Author:
chronos
Message:
  • Modified: Use object oriented approach for page drawing using Application class.
  • Added: SQL updated will be automatic using UpdateTrace.php file.
  • Added: Use generic setup page at URL /setup for SQL structure update.
  • Modified: Update Common package to newer version.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/Mail.php

    r55 r69  
    1313  var $From;
    1414  var $Recipients;
    15   var $Bodies; 
     15  var $Bodies;
    1616  var $Attachments;
    1717  var $AgentIdent;
     
    1919  var $ReplyTo;
    2020  var $Headers;
    21  
     21
    2222  function __construct()
    2323  {
    24     $this->Priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');           
     24    $this->Priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
    2525    $this->Boundary = md5(date('r', time()));
    2626    $this->AgentIdent = 'PHP/Mail';
    2727    $this->Clear();
    2828  }
    29  
     29
    3030  function Clear()
    3131  {
     
    6363  function AddBody($Content, $MimeType = 'text/plain', $Charset = 'utf-8')
    6464  {
    65     $this->Bodies[] = array('Content' => $Content, 'Type' => strtolower($MimeType), 
     65    $this->Bodies[] = array('Content' => $Content, 'Type' => strtolower($MimeType),
    6666      'Charset' => strtolower($Charset));
    6767  }
     
    7575  {
    7676    if(!intval($Priority)) return(false);
    77                
     77
    7878    if(!isset($this->priorities[$Priority - 1])) return(false);
    7979
    80     $this->xheaders['X-Priority'] = $this->priorities[$Priority - 1];   
     80    $this->xheaders['X-Priority'] = $this->priorities[$Priority - 1];
    8181    return(true);
    8282  }
    8383
    8484  function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT)
    85   {     
    86     $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType, 
     85  {
     86    $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
    8787      'Disposition' => $Disposition, 'Type' => 'File');
    8888  }
     
    9090  function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT)
    9191  {
    92     $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType, 
     92    $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
    9393      'Disposition' => $Disposition, 'Type' => 'Data', 'Data' => $Data);
    9494  }
     
    9696  function Send()
    9797  {
    98     if(count($this->Bodies) == 0) throw new Exception('Mail: Need at least one text body');
    99    
     98    if(count($this->Bodies) == 0) throw new Exception(T('Mail message need at least one text body'));
     99
    100100    $Body = $this->BuildAttachment($this->BuildBody());
    101    
     101
    102102    $To = '';
    103103    $this->Headers['CC'] = '';
    104104    $this->Headers['BCC'] = '';
    105     foreach($this->Recipients as $Index => $Recipient)     
     105    foreach($this->Recipients as $Index => $Recipient)
    106106    {
    107107      if($Recipient['Type'] == 'SendCombined')
     
    126126      }
    127127    }
    128     if($To == '') throw new Exception('Mail: Need at least one recipient address');
    129    
     128    if($To == '') throw new Exception(T('Mail message need at least one recipient address'));
     129
    130130    $this->Headers['Mime-Version'] = '1.0';
    131    
     131
    132132    if($this->AgentIdent != '') $this->Headers['X-Mailer'] = $this->AgentIdent;
    133133    if($this->ReplyTo != '') $this->Headers['Reply-To'] = $this->ReplyTo;
     
    135135
    136136    $Headers = '';
    137     foreach($this->Headers as $Header => $Value) 
     137    foreach($this->Headers as $Header => $Value)
    138138    {
    139139      if(($Header != 'Subject') and ($Value != '')) $Headers .= $Header.': '.$Value."\n";
    140     }   
     140    }
    141141
    142142    $this->Subject = strtr($this->Subject, "\r\n", '  ');
    143143
    144     if($this->Subject == '') throw new Exception('Mail: Missing Subject');
    145    
     144    if($this->Subject == '') throw new Exception(T('Mail message missing Subject'));
     145
    146146
    147147    $res = mail($To, $this->Subject, $Body, $Headers);
     
    152152  {
    153153    if(ereg(".*<(.+)>", $Address, $regs)) $Address = $regs[1];
    154     if(ereg("^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$", $Address)) 
     154    if(ereg("^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$", $Address))
    155155      return(true);
    156156      else return(false);
     
    159159  function CheckAdresses($Addresses)
    160160  {
    161     foreach($Addresses as $Address) 
    162     {
    163       if(!$this->ValidEmail($Address)) 
    164         throw new Exception('Mail: Invalid address '.$Address);
    165     }
    166   } 
    167  
     161    foreach($Addresses as $Address)
     162    {
     163      if(!$this->ValidEmail($Address))
     164  throw new Exception(sprintf(T('Mail message invalid address %s'), $Address));
     165    }
     166  }
     167
    168168  private function ContentEncoding($Charset)
    169169  {
     
    175175  {
    176176    if(count($this->Attachments) > 0)
    177     {       
     177    {
    178178      $this->Headers['Content-Type'] = "multipart/mixed;\n boundary=\"PHP-mixed-".$this->Boundary."\"";
    179179      $Result = "This is a multi-part message in MIME format.\n".
    180         "--PHP-mixed-".$this->Boundary."\n";   
     180        "--PHP-mixed-".$this->Boundary."\n";
    181181      $Result .= $Body;
    182    
    183       foreach($this->Attachments as $Attachment) 
     182
     183      foreach($this->Attachments as $Attachment)
    184184      {
    185185        $FileName = $Attachment['FileName'];
     
    189189        if($Attachment['Type'] == 'File')
    190190        {
    191           if(!file_exists($FileName)) 
    192             throw new Exception('Mail: Attached file '.$FileName.' can\'t be found');
     191          if(!file_exists($FileName))
     192            throw new Exception(sprintf(T('Mail message attached file %s can\'t be found'), $FileName));
    193193          $Data = file_get_contents($FileName);
    194         } else 
     194        } else
    195195        if($Attachment['Type'] == 'Data') $Data = $Attachment['Data'];
    196196          else $Data = '';
    197                
     197
    198198        $Result .= "\n".'--PHP-mixed-'.$this->Boundary."\n".
    199199          "Content-Type: ".$ContentType."; name=\"".$BaseName."\"\n".
     
    203203      }
    204204    } else $Result = $Body;
    205     return($Result);   
    206   }
    207  
     205    return($Result);
     206  }
     207
    208208  private function BuildBody()
    209209  {
    210210    $Result = '';
    211     if(count($this->Bodies) > 1) 
     211    if(count($this->Bodies) > 1)
    212212    {
    213213      $this->Headers['Content-Type'] = 'multipart/alternative; boundary="PHP-alt-'.$this->Boundary.'"';
    214214      $Result .= 'Content-Type: multipart/alternative; boundary="PHP-alt-'.$this->Boundary.'"'.
    215215        "\n\n";
    216     } else 
     216    } else
    217217    {
    218218      $this->Headers['Content-Type'] = $this->Bodies[0]['Type'].'; charset='.$this->Bodies[0]['Charset'];
     
    220220    }
    221221
    222        
     222
    223223    foreach($this->Bodies as $Body)
    224224    {
    225       if(count($this->Bodies) > 1) $Result .= "\n\n".'--PHP-alt-'.$this->Boundary."\n";           
     225      if(count($this->Bodies) > 1) $Result .= "\n\n".'--PHP-alt-'.$this->Boundary."\n";
    226226      $Result .= 'Content-Type: '.$Body['Type'].'; charset='.$Body['Charset'].
    227         "\nContent-Transfer-Encoding: ".$this->ContentEncoding($Body['Charset'])."\n\n".$Body['Content']."\n";         
     227        "\nContent-Transfer-Encoding: ".$this->ContentEncoding($Body['Charset'])."\n\n".$Body['Content']."\n";
    228228    }
    229229    return($Result);
    230   } 
     230  }
    231231}
    232232
Note: See TracChangeset for help on using the changeset viewer.