source: trunk/Mail.php@ 428

Last change on this file since 428 was 396, checked in by chronos, 13 years ago
  • Upraveno: Přepracována třída Mail pro lepší zpracování více těl zpráv a více příloh.
  • Přidáno: Částečná podpora pro zasílání faktur jako přílohy v poště při vyúčtování.
File size: 7.0 KB
Line 
1<?php
2
3define('DISPOSITION_INLINE', 'inline');
4define('DISPOSITION_ATTACHMENT', 'attachment');
5
6define('CHARSET_ASCII', 'us-ascii');
7define('CHARSET_UTF8', 'utf-8');
8
9class Mail
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;
21
22 function __construct()
23 {
24 $this->Priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
25 $this->Boundary = md5(date('r', time()));
26 $this->AgentIdent = 'PHP/Mail';
27 $this->Clear();
28 }
29
30 function Clear()
31 {
32 $this->Bodies = array();
33 $this->Attachments = array();
34 $this->Recipients = array();
35 $this->SenderAddress = '';
36 $this->SenderName = '';
37 $this->Subject = '';
38 $this->Organization = '';
39 $this->ReplyTo = '';
40 $this->Headers = array();
41 }
42
43 function AddTo($Address, $Name)
44 {
45 $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Send');
46 }
47
48 function AddCc($Address, $Name)
49 {
50 $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Copy');
51 }
52
53 function AddBcc($bcc)
54 {
55 $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'HiddenCopy');
56 }
57
58 function AddBody($Content, $MimeType = 'text/plain', $Charset = 'utf-8')
59 {
60 $this->Bodies[] = array('Content' => $Content, 'Type' => strtolower($MimeType),
61 'Charset' => strtolower($Charset));
62 }
63
64 function Organization($org)
65 {
66 if(trim($org != '')) $this->xheaders['Organization'] = $org;
67 }
68
69 function Priority($Priority)
70 {
71 if(!intval($Priority)) return(false);
72
73 if(!isset($this->priorities[$Priority - 1])) return(false);
74
75 $this->xheaders['X-Priority'] = $this->priorities[$Priority - 1];
76 return(true);
77 }
78
79 function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT)
80 {
81 $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
82 'Disposition' => $Disposition, 'Type' => 'File');
83 }
84
85 function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT)
86 {
87 $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
88 'Disposition' => $Disposition, 'Type' => 'Data', 'Data' => $Data);
89 }
90
91 function Send()
92 {
93 if(count($this->Bodies) == 0) throw new Exception('Mail: Need at least one text body');
94
95 $Body = $this->BuildAttachment($this->BuildBody());
96
97 $To = '';
98 $this->Headers['CC'] = '';
99 $this->Headers['BCC'] = '';
100 foreach($this->Recipients as $Index => $Recipient)
101 {
102 if($Recipient['Type'] == 'Send')
103 {
104 if($Index > 0) $To .= ', ';
105 $To .= $Recipient['Name'].' <'.$Recipient['Address'].'>';
106 } else
107 if($Recipient['Type'] == 'Copy')
108 {
109 if($Index > 0) $this->Headers['CC'] .= ', ';
110 $this->Headers['CC'] .= $Recipient['Name'].' <'.$To['Address'].'>';
111 } else
112 if($Recipient['Type'] == 'HiddenCopy')
113 {
114 if($Index > 0) $this->Headers['BCC'] .= ', ';
115 $this->Headers['BCC'] .= $Recipient['Name'].' <'.$To['Address'].'>';
116 }
117 }
118 if($To == '') throw new Exception('Mail: Need at least one recipient address');
119
120 $this->Headers['Mime-Version'] = '1.0';
121 if(count($this->Attachments) == 0)
122 {
123 $this->Headers['Content-Type'] = $this->Bodies[0]['Type'].'; charset='.$this->Bodies[0]['Charset'];
124 $this->Headers['Content-Transfer-Encoding'] = $this->ContentEncoding($this->Bodies[0]['Charset']);
125 }
126
127 if($this->AgentIdent != '') $this->Headers['X-Mailer'] = $this->AgentIdent;
128 if($this->ReplyTo != '') $this->Headers['Reply-To'] = $this->ReplyTo;
129 if($this->From != '') $this->Headers['From'] = $this->From;
130
131 $Headers = '';
132 foreach($this->Headers as $Header => $Value)
133 {
134 if(($Header != 'Subject') and ($Value != '')) $Headers .= $Header.': '.$Value."\n";
135 }
136
137 $this->Subject = strtr($this->Subject, "\r\n", ' ');
138
139 if($this->Subject == '') throw new Exception('Mail: Missing Subject');
140
141
142 $res = mail($To, $this->Subject, $Body, $Headers);
143 return($res);
144 }
145
146 function ValidEmail($Address)
147 {
148 if(ereg(".*<(.+)>", $Address, $regs)) $Address = $regs[1];
149 if(ereg("^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$", $Address))
150 return(true);
151 else return(false);
152 }
153
154 function CheckAdresses($Addresses)
155 {
156 foreach($Addresses as $Address)
157 {
158 if(!$this->ValidEmail($Address))
159 throw new Exception('Mail: Invalid address '.$Address);
160 }
161 }
162
163 private function ContentEncoding($Charset)
164 {
165 if($Charset != CHARSET_ASCII) return('8bit');
166 else return('7bit');
167 }
168
169 private function BuildAttachment($Body)
170 {
171 if(count($this->Attachments > 0))
172 {
173 $this->Headers['Content-Type'] = "multipart/mixed;\n boundary=\"PHP-mixed-".$this->Boundary."\"";
174
175 $Result = "This is a multi-part message in MIME format.\n".
176 "--PHP-mixed-".$this->Boundary."\n";
177 $Result .= $Body;
178
179 foreach($this->Attachments as $Attachment)
180 {
181 $FileName = $Attachment['FileName'];
182 $BaseName = basename($FileName);
183 $ContentType = $Attachment['FileType'];
184 $Disposition = $Attachment['Disposition'];
185 if($Attachment['Type'] == 'File')
186 {
187 if(!file_exists($FileName))
188 throw new Exception('Mail: Attached file '.$FileName.' can\'t be found');
189 $Data = file_get_contents($FileName);
190 } else
191 if($Attachment['Type'] == 'Data') $Data = $Attachment['Data'];
192 else $Data = '';
193
194 $Result .= "\n".'--PHP-mixed-'.$this->Boundary."\n".
195 "Content-Type: ".$ContentType."; name=\"".$BaseName."\"\n".
196 "Content-Transfer-Encoding: base64\n".
197 "Content-Disposition: ".$Disposition."\n\n";
198 $Result .= chunk_split(base64_encode($Data))."\r\n";
199 }
200 } else $Result = $Body;
201 return($Result);
202 }
203
204 private function BuildBody()
205 {
206 $Result = '';
207 if(count($this->Bodies) > 1)
208 $Result .= 'Content-Type: multipart/alternative; boundary="PHP-alt-'.$this->Boundary.'"'.
209 "\n\n";
210
211 foreach($this->Bodies as $Body)
212 {
213 if(count($this->Bodies) > 1) $Result .= "\n\n".'--PHP-alt-'.$this->Boundary."\n";
214 $Result .= 'Content-Type: '.$Body['Type'].'; charset='.$Body['Charset'].
215 "\nContent-Transfer-Encoding: ".$this->ContentEncoding($Body['Charset'])."\n\n".$Body['Content']."\n";
216 }
217 return($Result);
218 }
219}
220
221/*
222// Example
223$Mail = new Mail();
224$Mail->AddTo('nobody@nodomain', 'Recipient');
225$Mail->From = 'No reply <noreply@nodomain>';
226$Mail->Subject = 'Test message';
227$Mail->AddBody('This is sample message sent by PHP Mail class');
228$Mail->AddBody('This is sample <strong>HTML</strong> message sent by <i>PHP Mail class</i>', 'text/html');
229$Mail->AttachFile('mailtest.php', 'text/plain');
230$Mail->AttachData('DataFile.txt', 'text/plain', 'Sample text');
231$Mail->Send();
232*/
233
234?>
Note: See TracBrowser for help on using the repository browser.