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