Ignore:
Timestamp:
Jun 1, 2023, 12:18:18 AM (18 months ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
  • Modified: Form types made as separate FormManager package.
  • Fixed: PHP 8.1 support.
File:
1 edited

Legend:

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

    r7 r8  
    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;
     23  public string $SenderAddress;
     24  public string $SenderName;
    2125
    2226  function __construct()
     
    2529    $this->Boundary = md5(date('r', time()));
    2630    $this->AgentIdent = 'PHP/Mail';
     31    $this->TestMode = false;
    2732    $this->Clear();
    2833  }
    2934
    30   function Clear()
     35  function Clear(): void
    3136  {
    3237    $this->Bodies = array();
     
    4146  }
    4247
    43   function AddToCombined($Address)
     48  function AddToCombined(string $Address): void
    4449  {
    4550    $this->Recipients[] = array('Address' => $Address, 'Type' => 'SendCombined');
    4651  }
    4752
    48   function AddTo($Address, $Name)
     53  function AddTo(string $Address, string $Name): void
    4954  {
    5055    $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Send');
    5156  }
    5257
    53   function AddCc($Address, $Name)
     58  function AddCc(string $Address, string $Name): void
    5459  {
    5560    $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Copy');
    5661  }
    5762
    58   function AddBcc($Address, $Name)
     63  function AddBcc(string $Address, string $Name): void
    5964  {
    6065    $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'HiddenCopy');
    6166  }
    6267
    63   function AddBody($Content, $MimeType = 'text/plain', $Charset = 'utf-8')
     68  function AddBody(string $Content, string $MimeType = 'text/plain', string $Charset = 'utf-8'): void
    6469  {
    6570    $this->Bodies[] = array('Content' => $Content, 'Type' => strtolower($MimeType),
     
    6772  }
    6873
    69   function Organization($org)
    70   {
    71     if (trim($org != '')) $this->xheaders['Organization'] = $org;
    72   }
    73 
    74   function Priority($Priority)
     74  function Organization(string $org): void
     75  {
     76    if (trim($org != '')) $this->Headers['Organization'] = $org;
     77  }
     78
     79  function Priority(int $Priority): bool
    7580  {
    7681    if (!intval($Priority)) return false;
    7782
    78     if (!isset($this->priorities[$Priority - 1])) return false;
    79 
    80     $this->xheaders['X-Priority'] = $this->priorities[$Priority - 1];
     83    if (!isset($this->Priorities[$Priority - 1])) return false;
     84
     85    $this->Headers['X-Priority'] = $this->Priorities[$Priority - 1];
    8186    return true;
    8287  }
    8388
    84   function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT)
     89  function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT): void
    8590  {
    8691    $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
     
    8893  }
    8994
    90   function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT)
     95  function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT): void
    9196  {
    9297    $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
     
    9499  }
    95100
    96   function Send()
     101  function Send(): bool
    97102  {
    98103    if (count($this->Bodies) == 0) throw new Exception(T('Mail message need at least one text body'));
     
    132137    if ($this->AgentIdent != '') $this->Headers['X-Mailer'] = $this->AgentIdent;
    133138    if ($this->ReplyTo != '') $this->Headers['Reply-To'] = $this->ReplyTo;
    134     if ($this->From != '') $this->Headers['From'] = $this->From;
     139    if ($this->From != '')
     140    {
     141      $IndexStart = strpos($this->From, '<');
     142      if ($IndexStart !== false)
     143      {
     144        $this->Headers['From'] = '=?utf-8?Q?'.quoted_printable_encode(trim(substr($this->From, 0, $IndexStart))).'?= '.substr($this->From, $IndexStart);
     145      } else
     146      {
     147        $this->Headers['From'] = $this->From;
     148      }
     149    }
    135150
    136151    $Headers = '';
     
    144159    if ($this->Subject == '') throw new Exception(T('Mail message missing Subject'));
    145160
    146 
    147     $res = mail($To, $this->Subject, $Body, $Headers);
     161    if ($this->TestMode)
     162    {
     163      echo('to: '.$To.', subject: '.$this->Subject.', body: '.$Body.', headers: '.$Headers);
     164      $res = true;
     165    } else
     166    {
     167      $res = mail($To, $this->Subject, $Body, $Headers);
     168    }
    148169    return $res;
    149170  }
    150171
    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))
     172  function ValidEmail(string $Address): bool
     173  {
     174    if (preg_match(".*<(.+)>", $Address, $regs)) $Address = $regs[1];
     175    if (preg_match("^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$", $Address))
    155176      return true;
    156177      else return false;
    157178  }
    158179
    159   function CheckAdresses($Addresses)
     180  function CheckAdresses(array $Addresses): void
    160181  {
    161182    foreach ($Addresses as $Address)
    162183    {
    163184      if (!$this->ValidEmail($Address))
    164   throw new Exception(sprintf(T('Mail message invalid address %s'), $Address));
    165     }
    166   }
    167 
    168   private function ContentEncoding($Charset)
     185      {
     186        throw new Exception(sprintf(T('Mail message invalid address %s'), $Address));
     187      }
     188    }
     189  }
     190
     191  private function ContentEncoding($Charset): string
    169192  {
    170193    if ($Charset != CHARSET_ASCII) return '8bit';
     
    172195  }
    173196
    174   private function BuildAttachment($Body)
     197  private function BuildAttachment($Body): string
    175198  {
    176199    if (count($this->Attachments) > 0)
     
    206229  }
    207230
    208   private function BuildBody()
     231  private function BuildBody(): string
    209232  {
    210233    $Result = '';
     
    219242      $this->Headers['Content-Transfer-Encoding'] = $this->ContentEncoding($this->Bodies[0]['Charset']);
    220243    }
    221 
    222244
    223245    foreach ($this->Bodies as $Body)
Note: See TracChangeset for help on using the changeset viewer.