Ignore:
Timestamp:
Dec 6, 2021, 11:33:48 AM (3 years ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
  • Added: Explicit types for better type checking.
  • Fixed: Support for php 8.0.
File:
1 edited

Legend:

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

    r92 r95  
    99class Mail
    1010{
    11   var $Priorities;
    12   var $Subject;
    13   var $From;
    14   var $Recipients;
    15   var $Bodies;
    16   var $Attachments;
    17   var $AgentIdent;
    18   var $Organization;
    19   var $ReplyTo;
    20   var $Headers;
     11  public string $Subject;
     12  public string $From;
     13  public array $Recipients;
     14  public array $Bodies;
     15  public array $Attachments;
     16  public string $AgentIdent;
     17  public string $Organization;
     18  public string $ReplyTo;
     19  public array $Headers;
     20  private array $Priorities;
     21  private string $Boundary;
    2122
    2223  function __construct()
     
    2829  }
    2930
    30   function Clear()
     31  function Clear(): void
    3132  {
    3233    $this->Bodies = array();
     
    4142  }
    4243
    43   function AddToCombined($Address)
     44  function AddToCombined(string $Address): void
    4445  {
    4546    $this->Recipients[] = array('Address' => $Address, 'Type' => 'SendCombined');
    4647  }
    4748
    48   function AddTo($Address, $Name)
     49  function AddTo(string $Address, string $Name): void
    4950  {
    5051    $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Send');
    5152  }
    5253
    53   function AddCc($Address, $Name)
     54  function AddCc(string $Address, string $Name): void
    5455  {
    5556    $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Copy');
    5657  }
    5758
    58   function AddBcc($Address, $Name)
     59  function AddBcc(string $Address, string $Name): void
    5960  {
    6061    $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'HiddenCopy');
    6162  }
    6263
    63   function AddBody($Content, $MimeType = 'text/plain', $Charset = 'utf-8')
     64  function AddBody(string $Content, string $MimeType = 'text/plain', string $Charset = 'utf-8'): void
    6465  {
    6566    $this->Bodies[] = array('Content' => $Content, 'Type' => strtolower($MimeType),
     
    6768  }
    6869
    69   function Organization($org)
     70  function Organization(string $org): void
    7071  {
    7172    if (trim($org != '')) $this->xheaders['Organization'] = $org;
    7273  }
    7374
    74   function Priority($Priority)
     75  function Priority(int $Priority): bool
    7576  {
    7677    if (!intval($Priority)) return false;
    7778
    78     if (!isset($this->priorities[$Priority - 1])) return false;
    79 
    80     $this->xheaders['X-Priority'] = $this->priorities[$Priority - 1];
     79    if (!isset($this->Priorities[$Priority - 1])) return false;
     80
     81    $this->xheaders['X-Priority'] = $this->Priorities[$Priority - 1];
    8182    return true;
    8283  }
    8384
    84   function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT)
     85  function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT): void
    8586  {
    8687    $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
     
    8889  }
    8990
    90   function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT)
     91  function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT): void
    9192  {
    9293    $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
     
    9495  }
    9596
    96   function Send()
     97  function Send(): bool
    9798  {
    9899    if (count($this->Bodies) == 0) throw new Exception(T('Mail message need at least one text body'));
     
    144145    if ($this->Subject == '') throw new Exception(T('Mail message missing Subject'));
    145146
    146 
    147147    $res = mail($To, $this->Subject, $Body, $Headers);
    148148    return $res;
    149149  }
    150150
    151   function ValidEmail($Address)
    152   {
    153     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))
     151  function ValidEmail(string $Address): bool
     152  {
     153    if (preg_match(".*<(.+)>", $Address, $regs)) $Address = $regs[1];
     154    if (preg_match("^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$", $Address))
    155155      return true;
    156156      else return false;
    157157  }
    158158
    159   function CheckAdresses($Addresses)
     159  function CheckAdresses(array $Addresses): void
    160160  {
    161161    foreach ($Addresses as $Address)
    162162    {
    163163      if (!$this->ValidEmail($Address))
    164   throw new Exception(sprintf(T('Mail message invalid address %s'), $Address));
    165     }
    166   }
    167 
    168   private function ContentEncoding($Charset)
     164      {
     165        throw new Exception(sprintf(T('Mail message invalid address %s'), $Address));
     166      }
     167    }
     168  }
     169
     170  private function ContentEncoding($Charset): string
    169171  {
    170172    if ($Charset != CHARSET_ASCII) return '8bit';
     
    172174  }
    173175
    174   private function BuildAttachment($Body)
     176  private function BuildAttachment($Body): string
    175177  {
    176178    if (count($this->Attachments) > 0)
     
    206208  }
    207209
    208   private function BuildBody()
     210  private function BuildBody(): string
    209211  {
    210212    $Result = '';
     
    219221      $this->Headers['Content-Transfer-Encoding'] = $this->ContentEncoding($this->Bodies[0]['Charset']);
    220222    }
    221 
    222223
    223224    foreach ($this->Bodies as $Body)
Note: See TracChangeset for help on using the changeset viewer.