Changeset 69 for trunk/Packages/Common/Mail.php
- Timestamp:
- Feb 28, 2016, 10:54:30 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/Mail.php
r55 r69 13 13 var $From; 14 14 var $Recipients; 15 var $Bodies; 15 var $Bodies; 16 16 var $Attachments; 17 17 var $AgentIdent; … … 19 19 var $ReplyTo; 20 20 var $Headers; 21 21 22 22 function __construct() 23 23 { 24 $this->Priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)'); 24 $this->Priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)'); 25 25 $this->Boundary = md5(date('r', time())); 26 26 $this->AgentIdent = 'PHP/Mail'; 27 27 $this->Clear(); 28 28 } 29 29 30 30 function Clear() 31 31 { … … 63 63 function AddBody($Content, $MimeType = 'text/plain', $Charset = 'utf-8') 64 64 { 65 $this->Bodies[] = array('Content' => $Content, 'Type' => strtolower($MimeType), 65 $this->Bodies[] = array('Content' => $Content, 'Type' => strtolower($MimeType), 66 66 'Charset' => strtolower($Charset)); 67 67 } … … 75 75 { 76 76 if(!intval($Priority)) return(false); 77 77 78 78 if(!isset($this->priorities[$Priority - 1])) return(false); 79 79 80 $this->xheaders['X-Priority'] = $this->priorities[$Priority - 1]; 80 $this->xheaders['X-Priority'] = $this->priorities[$Priority - 1]; 81 81 return(true); 82 82 } 83 83 84 84 function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT) 85 { 86 $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType, 85 { 86 $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType, 87 87 'Disposition' => $Disposition, 'Type' => 'File'); 88 88 } … … 90 90 function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT) 91 91 { 92 $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType, 92 $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType, 93 93 'Disposition' => $Disposition, 'Type' => 'Data', 'Data' => $Data); 94 94 } … … 96 96 function Send() 97 97 { 98 if(count($this->Bodies) == 0) throw new Exception( 'Mail: Need at least one text body');99 98 if(count($this->Bodies) == 0) throw new Exception(T('Mail message need at least one text body')); 99 100 100 $Body = $this->BuildAttachment($this->BuildBody()); 101 101 102 102 $To = ''; 103 103 $this->Headers['CC'] = ''; 104 104 $this->Headers['BCC'] = ''; 105 foreach($this->Recipients as $Index => $Recipient) 105 foreach($this->Recipients as $Index => $Recipient) 106 106 { 107 107 if($Recipient['Type'] == 'SendCombined') … … 126 126 } 127 127 } 128 if($To == '') throw new Exception( 'Mail: Need at least one recipient address');129 128 if($To == '') throw new Exception(T('Mail message need at least one recipient address')); 129 130 130 $this->Headers['Mime-Version'] = '1.0'; 131 131 132 132 if($this->AgentIdent != '') $this->Headers['X-Mailer'] = $this->AgentIdent; 133 133 if($this->ReplyTo != '') $this->Headers['Reply-To'] = $this->ReplyTo; … … 135 135 136 136 $Headers = ''; 137 foreach($this->Headers as $Header => $Value) 137 foreach($this->Headers as $Header => $Value) 138 138 { 139 139 if(($Header != 'Subject') and ($Value != '')) $Headers .= $Header.': '.$Value."\n"; 140 } 140 } 141 141 142 142 $this->Subject = strtr($this->Subject, "\r\n", ' '); 143 143 144 if($this->Subject == '') throw new Exception( 'Mail: Missing Subject');145 144 if($this->Subject == '') throw new Exception(T('Mail message missing Subject')); 145 146 146 147 147 $res = mail($To, $this->Subject, $Body, $Headers); … … 152 152 { 153 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)) 154 if(ereg("^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$", $Address)) 155 155 return(true); 156 156 else return(false); … … 159 159 function CheckAdresses($Addresses) 160 160 { 161 foreach($Addresses as $Address) 162 { 163 if(!$this->ValidEmail($Address)) 164 throw new Exception('Mail: Invalid address '.$Address);165 } 166 } 167 161 foreach($Addresses as $Address) 162 { 163 if(!$this->ValidEmail($Address)) 164 throw new Exception(sprintf(T('Mail message invalid address %s'), $Address)); 165 } 166 } 167 168 168 private function ContentEncoding($Charset) 169 169 { … … 175 175 { 176 176 if(count($this->Attachments) > 0) 177 { 177 { 178 178 $this->Headers['Content-Type'] = "multipart/mixed;\n boundary=\"PHP-mixed-".$this->Boundary."\""; 179 179 $Result = "This is a multi-part message in MIME format.\n". 180 "--PHP-mixed-".$this->Boundary."\n"; 180 "--PHP-mixed-".$this->Boundary."\n"; 181 181 $Result .= $Body; 182 183 foreach($this->Attachments as $Attachment) 182 183 foreach($this->Attachments as $Attachment) 184 184 { 185 185 $FileName = $Attachment['FileName']; … … 189 189 if($Attachment['Type'] == 'File') 190 190 { 191 if(!file_exists($FileName)) 192 throw new Exception( 'Mail: Attached file '.$FileName.' can\'t be found');191 if(!file_exists($FileName)) 192 throw new Exception(sprintf(T('Mail message attached file %s can\'t be found'), $FileName)); 193 193 $Data = file_get_contents($FileName); 194 } else 194 } else 195 195 if($Attachment['Type'] == 'Data') $Data = $Attachment['Data']; 196 196 else $Data = ''; 197 197 198 198 $Result .= "\n".'--PHP-mixed-'.$this->Boundary."\n". 199 199 "Content-Type: ".$ContentType."; name=\"".$BaseName."\"\n". … … 203 203 } 204 204 } else $Result = $Body; 205 return($Result); 206 } 207 205 return($Result); 206 } 207 208 208 private function BuildBody() 209 209 { 210 210 $Result = ''; 211 if(count($this->Bodies) > 1) 211 if(count($this->Bodies) > 1) 212 212 { 213 213 $this->Headers['Content-Type'] = 'multipart/alternative; boundary="PHP-alt-'.$this->Boundary.'"'; 214 214 $Result .= 'Content-Type: multipart/alternative; boundary="PHP-alt-'.$this->Boundary.'"'. 215 215 "\n\n"; 216 } else 216 } else 217 217 { 218 218 $this->Headers['Content-Type'] = $this->Bodies[0]['Type'].'; charset='.$this->Bodies[0]['Charset']; … … 220 220 } 221 221 222 222 223 223 foreach($this->Bodies as $Body) 224 224 { 225 if(count($this->Bodies) > 1) $Result .= "\n\n".'--PHP-alt-'.$this->Boundary."\n"; 225 if(count($this->Bodies) > 1) $Result .= "\n\n".'--PHP-alt-'.$this->Boundary."\n"; 226 226 $Result .= 'Content-Type: '.$Body['Type'].'; charset='.$Body['Charset']. 227 "\nContent-Transfer-Encoding: ".$this->ContentEncoding($Body['Charset'])."\n\n".$Body['Content']."\n"; 227 "\nContent-Transfer-Encoding: ".$this->ContentEncoding($Body['Charset'])."\n\n".$Body['Content']."\n"; 228 228 } 229 229 return($Result); 230 } 230 } 231 231 } 232 232
Note:
See TracChangeset
for help on using the changeset viewer.