Changeset 8 for trunk/Packages/Common/Mail.php
- Timestamp:
- Jun 1, 2023, 12:18:18 AM (19 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/Mail.php
r7 r8 9 9 class Mail 10 10 { 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; 21 25 22 26 function __construct() … … 25 29 $this->Boundary = md5(date('r', time())); 26 30 $this->AgentIdent = 'PHP/Mail'; 31 $this->TestMode = false; 27 32 $this->Clear(); 28 33 } 29 34 30 function Clear() 35 function Clear(): void 31 36 { 32 37 $this->Bodies = array(); … … 41 46 } 42 47 43 function AddToCombined( $Address)48 function AddToCombined(string $Address): void 44 49 { 45 50 $this->Recipients[] = array('Address' => $Address, 'Type' => 'SendCombined'); 46 51 } 47 52 48 function AddTo( $Address, $Name)53 function AddTo(string $Address, string $Name): void 49 54 { 50 55 $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Send'); 51 56 } 52 57 53 function AddCc( $Address, $Name)58 function AddCc(string $Address, string $Name): void 54 59 { 55 60 $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Copy'); 56 61 } 57 62 58 function AddBcc( $Address, $Name)63 function AddBcc(string $Address, string $Name): void 59 64 { 60 65 $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'HiddenCopy'); 61 66 } 62 67 63 function AddBody( $Content, $MimeType = 'text/plain', $Charset = 'utf-8')68 function AddBody(string $Content, string $MimeType = 'text/plain', string $Charset = 'utf-8'): void 64 69 { 65 70 $this->Bodies[] = array('Content' => $Content, 'Type' => strtolower($MimeType), … … 67 72 } 68 73 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 75 80 { 76 81 if (!intval($Priority)) return false; 77 82 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]; 81 86 return true; 82 87 } 83 88 84 function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT) 89 function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT): void 85 90 { 86 91 $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType, … … 88 93 } 89 94 90 function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT) 95 function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT): void 91 96 { 92 97 $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType, … … 94 99 } 95 100 96 function Send() 101 function Send(): bool 97 102 { 98 103 if (count($this->Bodies) == 0) throw new Exception(T('Mail message need at least one text body')); … … 132 137 if ($this->AgentIdent != '') $this->Headers['X-Mailer'] = $this->AgentIdent; 133 138 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 } 135 150 136 151 $Headers = ''; … … 144 159 if ($this->Subject == '') throw new Exception(T('Mail message missing Subject')); 145 160 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 } 148 169 return $res; 149 170 } 150 171 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)) 155 176 return true; 156 177 else return false; 157 178 } 158 179 159 function CheckAdresses( $Addresses)180 function CheckAdresses(array $Addresses): void 160 181 { 161 182 foreach ($Addresses as $Address) 162 183 { 163 184 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 169 192 { 170 193 if ($Charset != CHARSET_ASCII) return '8bit'; … … 172 195 } 173 196 174 private function BuildAttachment($Body) 197 private function BuildAttachment($Body): string 175 198 { 176 199 if (count($this->Attachments) > 0) … … 206 229 } 207 230 208 private function BuildBody() 231 private function BuildBody(): string 209 232 { 210 233 $Result = ''; … … 219 242 $this->Headers['Content-Transfer-Encoding'] = $this->ContentEncoding($this->Bodies[0]['Charset']); 220 243 } 221 222 244 223 245 foreach ($this->Bodies as $Body)
Note:
See TracChangeset
for help on using the changeset viewer.