Ignore:
Timestamp:
Dec 27, 2022, 7:50:23 PM (2 years ago)
Author:
chronos
Message:
  • Modified: Updated Common package to latest version.
  • Modified: Fixes related to PHP 8.x.
File:
1 edited

Legend:

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

    r880 r888  
    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;
     22  public bool $TestMode;
    2123
    2224  function __construct()
     
    2527    $this->Boundary = md5(date('r', time()));
    2628    $this->AgentIdent = 'PHP/Mail';
     29    $this->TestMode = false;
    2730    $this->Clear();
    2831  }
    2932
    30   function Clear()
     33  function Clear(): void
    3134  {
    3235    $this->Bodies = array();
     
    4144  }
    4245
    43   function AddToCombined($Address)
     46  function AddToCombined(string $Address): void
    4447  {
    4548    $this->Recipients[] = array('Address' => $Address, 'Type' => 'SendCombined');
    4649  }
    4750
    48   function AddTo($Address, $Name)
     51  function AddTo(string $Address, string $Name): void
    4952  {
    5053    $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Send');
    5154  }
    5255
    53   function AddCc($Address, $Name)
     56  function AddCc(string $Address, string $Name): void
    5457  {
    5558    $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Copy');
    5659  }
    5760
    58   function AddBcc($Address, $Name)
     61  function AddBcc(string $Address, string $Name): void
    5962  {
    6063    $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'HiddenCopy');
    6164  }
    6265
    63   function AddBody($Content, $MimeType = 'text/plain', $Charset = 'utf-8')
     66  function AddBody(string $Content, string $MimeType = 'text/plain', string $Charset = 'utf-8'): void
    6467  {
    6568    $this->Bodies[] = array('Content' => $Content, 'Type' => strtolower($MimeType),
     
    6770  }
    6871
    69   function Organization($org)
     72  function Organization(string $org): void
    7073  {
    7174    if (trim($org != '')) $this->xheaders['Organization'] = $org;
    7275  }
    7376
    74   function Priority($Priority)
     77  function Priority(int $Priority): bool
    7578  {
    7679    if (!intval($Priority)) return false;
    7780
    78     if (!isset($this->priorities[$Priority - 1])) return false;
    79 
    80     $this->xheaders['X-Priority'] = $this->priorities[$Priority - 1];
     81    if (!isset($this->Priorities[$Priority - 1])) return false;
     82
     83    $this->xheaders['X-Priority'] = $this->Priorities[$Priority - 1];
    8184    return true;
    8285  }
    8386
    84   function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT)
     87  function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT): void
    8588  {
    8689    $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
     
    8891  }
    8992
    90   function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT)
     93  function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT): void
    9194  {
    9295    $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
     
    9497  }
    9598
    96   function Send()
     99  function Send(): bool
    97100  {
    98101    if (count($this->Bodies) == 0) throw new Exception(T('Mail message need at least one text body'));
     
    132135    if ($this->AgentIdent != '') $this->Headers['X-Mailer'] = $this->AgentIdent;
    133136    if ($this->ReplyTo != '') $this->Headers['Reply-To'] = $this->ReplyTo;
    134     if ($this->From != '') $this->Headers['From'] = $this->From;
     137    if ($this->From != '')
     138    {
     139      $IndexStart = strpos($this->From, '<');
     140      if ($IndexStart !== false)
     141      {
     142        $this->Headers['From'] = '=?utf-8?Q?'.quoted_printable_encode(trim(substr($this->From, 0, $IndexStart))).'?= '.substr($this->From, $IndexStart);
     143      } else
     144      {
     145        $this->Headers['From'] = $this->From;
     146      }
     147    }
    135148
    136149    $Headers = '';
     
    144157    if ($this->Subject == '') throw new Exception(T('Mail message missing Subject'));
    145158
    146 
    147     $res = mail($To, $this->Subject, $Body, $Headers);
     159    if ($this->TestMode)
     160    {
     161      echo('to: '.$To.', subject: '.$this->Subject.', body: '.$Body.', headers: '.$Headers);
     162      $res = true;
     163    } else
     164    {
     165      $res = mail($To, $this->Subject, $Body, $Headers);
     166    }
    148167    return $res;
    149168  }
    150169
    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))
     170  function ValidEmail(string $Address): bool
     171  {
     172    if (preg_match(".*<(.+)>", $Address, $regs)) $Address = $regs[1];
     173    if (preg_match("^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$", $Address))
    155174      return true;
    156175      else return false;
    157176  }
    158177
    159   function CheckAdresses($Addresses)
     178  function CheckAdresses(array $Addresses): void
    160179  {
    161180    foreach ($Addresses as $Address)
    162181    {
    163182      if (!$this->ValidEmail($Address))
    164   throw new Exception(sprintf(T('Mail message invalid address %s'), $Address));
    165     }
    166   }
    167 
    168   private function ContentEncoding($Charset)
     183      {
     184        throw new Exception(sprintf(T('Mail message invalid address %s'), $Address));
     185      }
     186    }
     187  }
     188
     189  private function ContentEncoding($Charset): string
    169190  {
    170191    if ($Charset != CHARSET_ASCII) return '8bit';
     
    172193  }
    173194
    174   private function BuildAttachment($Body)
     195  private function BuildAttachment($Body): string
    175196  {
    176197    if (count($this->Attachments) > 0)
     
    206227  }
    207228
    208   private function BuildBody()
     229  private function BuildBody(): string
    209230  {
    210231    $Result = '';
     
    219240      $this->Headers['Content-Transfer-Encoding'] = $this->ContentEncoding($this->Bodies[0]['Charset']);
    220241    }
    221 
    222242
    223243    foreach ($this->Bodies as $Body)
Note: See TracChangeset for help on using the changeset viewer.