1 | <?php
|
---|
2 |
|
---|
3 | define('DISPOSITION_INLINE', 'inline');
|
---|
4 | define('DISPOSITION_ATTACHMENT', 'attachment');
|
---|
5 |
|
---|
6 | define('CHARSET_ASCII', 'us-ascii');
|
---|
7 | define('CHARSET_UTF8', 'utf-8');
|
---|
8 |
|
---|
9 | class 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 AddToCombined($Address)
|
---|
44 | {
|
---|
45 | $this->Recipients[] = array('Address' => $Address, 'Type' => 'SendCombined');
|
---|
46 | }
|
---|
47 |
|
---|
48 | function AddTo($Address, $Name)
|
---|
49 | {
|
---|
50 | $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Send');
|
---|
51 | }
|
---|
52 |
|
---|
53 | function AddCc($Address, $Name)
|
---|
54 | {
|
---|
55 | $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Copy');
|
---|
56 | }
|
---|
57 |
|
---|
58 | function AddBcc($Address, $Name)
|
---|
59 | {
|
---|
60 | $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'HiddenCopy');
|
---|
61 | }
|
---|
62 |
|
---|
63 | function AddBody($Content, $MimeType = 'text/plain', $Charset = 'utf-8')
|
---|
64 | {
|
---|
65 | $this->Bodies[] = array('Content' => $Content, 'Type' => strtolower($MimeType),
|
---|
66 | 'Charset' => strtolower($Charset));
|
---|
67 | }
|
---|
68 |
|
---|
69 | function Organization($org)
|
---|
70 | {
|
---|
71 | if(trim($org != '')) $this->xheaders['Organization'] = $org;
|
---|
72 | }
|
---|
73 |
|
---|
74 | function Priority($Priority)
|
---|
75 | {
|
---|
76 | if(!intval($Priority)) return(false);
|
---|
77 |
|
---|
78 | if(!isset($this->priorities[$Priority - 1])) return(false);
|
---|
79 |
|
---|
80 | $this->xheaders['X-Priority'] = $this->priorities[$Priority - 1];
|
---|
81 | return(true);
|
---|
82 | }
|
---|
83 |
|
---|
84 | function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT)
|
---|
85 | {
|
---|
86 | $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
|
---|
87 | 'Disposition' => $Disposition, 'Type' => 'File');
|
---|
88 | }
|
---|
89 |
|
---|
90 | function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT)
|
---|
91 | {
|
---|
92 | $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
|
---|
93 | 'Disposition' => $Disposition, 'Type' => 'Data', 'Data' => $Data);
|
---|
94 | }
|
---|
95 |
|
---|
96 | function Send()
|
---|
97 | {
|
---|
98 | if(count($this->Bodies) == 0) throw new Exception(T('Mail message need at least one text body'));
|
---|
99 |
|
---|
100 | $Body = $this->BuildAttachment($this->BuildBody());
|
---|
101 |
|
---|
102 | $To = '';
|
---|
103 | $this->Headers['CC'] = '';
|
---|
104 | $this->Headers['BCC'] = '';
|
---|
105 | foreach($this->Recipients as $Index => $Recipient)
|
---|
106 | {
|
---|
107 | if($Recipient['Type'] == 'SendCombined')
|
---|
108 | {
|
---|
109 | if($Index > 0) $To .= ', ';
|
---|
110 | $To .= $Recipient['Address'];
|
---|
111 | } else
|
---|
112 | if($Recipient['Type'] == 'Send')
|
---|
113 | {
|
---|
114 | if($Index > 0) $To .= ', ';
|
---|
115 | $To .= $Recipient['Name'].' <'.$Recipient['Address'].'>';
|
---|
116 | } else
|
---|
117 | if($Recipient['Type'] == 'Copy')
|
---|
118 | {
|
---|
119 | if($Index > 0) $this->Headers['CC'] .= ', ';
|
---|
120 | $this->Headers['CC'] .= $Recipient['Name'].' <'.$To['Address'].'>';
|
---|
121 | } else
|
---|
122 | if($Recipient['Type'] == 'HiddenCopy')
|
---|
123 | {
|
---|
124 | if($Index > 0) $this->Headers['BCC'] .= ', ';
|
---|
125 | $this->Headers['BCC'] .= $Recipient['Name'].' <'.$To['Address'].'>';
|
---|
126 | }
|
---|
127 | }
|
---|
128 | if($To == '') throw new Exception(T('Mail message need at least one recipient address'));
|
---|
129 |
|
---|
130 | $this->Headers['Mime-Version'] = '1.0';
|
---|
131 |
|
---|
132 | if($this->AgentIdent != '') $this->Headers['X-Mailer'] = $this->AgentIdent;
|
---|
133 | if($this->ReplyTo != '') $this->Headers['Reply-To'] = $this->ReplyTo;
|
---|
134 | if($this->From != '') $this->Headers['From'] = $this->From;
|
---|
135 |
|
---|
136 | $Headers = '';
|
---|
137 | foreach($this->Headers as $Header => $Value)
|
---|
138 | {
|
---|
139 | if(($Header != 'Subject') and ($Value != '')) $Headers .= $Header.': '.$Value."\n";
|
---|
140 | }
|
---|
141 |
|
---|
142 | $this->Subject = strtr($this->Subject, "\r\n", ' ');
|
---|
143 |
|
---|
144 | if($this->Subject == '') throw new Exception(T('Mail message missing Subject'));
|
---|
145 |
|
---|
146 |
|
---|
147 | $res = mail($To, $this->Subject, $Body, $Headers);
|
---|
148 | return($res);
|
---|
149 | }
|
---|
150 |
|
---|
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))
|
---|
155 | return(true);
|
---|
156 | else return(false);
|
---|
157 | }
|
---|
158 |
|
---|
159 | function CheckAdresses($Addresses)
|
---|
160 | {
|
---|
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 | private function ContentEncoding($Charset)
|
---|
169 | {
|
---|
170 | if($Charset != CHARSET_ASCII) return('8bit');
|
---|
171 | else return('7bit');
|
---|
172 | }
|
---|
173 |
|
---|
174 | private function BuildAttachment($Body)
|
---|
175 | {
|
---|
176 | if(count($this->Attachments) > 0)
|
---|
177 | {
|
---|
178 | $this->Headers['Content-Type'] = "multipart/mixed;\n boundary=\"PHP-mixed-".$this->Boundary."\"";
|
---|
179 | $Result = "This is a multi-part message in MIME format.\n".
|
---|
180 | "--PHP-mixed-".$this->Boundary."\n";
|
---|
181 | $Result .= $Body;
|
---|
182 |
|
---|
183 | foreach($this->Attachments as $Attachment)
|
---|
184 | {
|
---|
185 | $FileName = $Attachment['FileName'];
|
---|
186 | $BaseName = basename($FileName);
|
---|
187 | $ContentType = $Attachment['FileType'];
|
---|
188 | $Disposition = $Attachment['Disposition'];
|
---|
189 | if($Attachment['Type'] == 'File')
|
---|
190 | {
|
---|
191 | if(!file_exists($FileName))
|
---|
192 | throw new Exception(sprintf(T('Mail message attached file %s can\'t be found'), $FileName));
|
---|
193 | $Data = file_get_contents($FileName);
|
---|
194 | } else
|
---|
195 | if($Attachment['Type'] == 'Data') $Data = $Attachment['Data'];
|
---|
196 | else $Data = '';
|
---|
197 |
|
---|
198 | $Result .= "\n".'--PHP-mixed-'.$this->Boundary."\n".
|
---|
199 | "Content-Type: ".$ContentType."; name=\"".$BaseName."\"\n".
|
---|
200 | "Content-Transfer-Encoding: base64\n".
|
---|
201 | "Content-Disposition: ".$Disposition."\n\n";
|
---|
202 | $Result .= chunk_split(base64_encode($Data))."\r\n";
|
---|
203 | }
|
---|
204 | } else $Result = $Body;
|
---|
205 | return($Result);
|
---|
206 | }
|
---|
207 |
|
---|
208 | private function BuildBody()
|
---|
209 | {
|
---|
210 | $Result = '';
|
---|
211 | if(count($this->Bodies) > 1)
|
---|
212 | {
|
---|
213 | $this->Headers['Content-Type'] = 'multipart/alternative; boundary="PHP-alt-'.$this->Boundary.'"';
|
---|
214 | $Result .= 'Content-Type: multipart/alternative; boundary="PHP-alt-'.$this->Boundary.'"'.
|
---|
215 | "\n\n";
|
---|
216 | } else
|
---|
217 | {
|
---|
218 | $this->Headers['Content-Type'] = $this->Bodies[0]['Type'].'; charset='.$this->Bodies[0]['Charset'];
|
---|
219 | $this->Headers['Content-Transfer-Encoding'] = $this->ContentEncoding($this->Bodies[0]['Charset']);
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | foreach($this->Bodies as $Body)
|
---|
224 | {
|
---|
225 | if(count($this->Bodies) > 1) $Result .= "\n\n".'--PHP-alt-'.$this->Boundary."\n";
|
---|
226 | $Result .= 'Content-Type: '.$Body['Type'].'; charset='.$Body['Charset'].
|
---|
227 | "\nContent-Transfer-Encoding: ".$this->ContentEncoding($Body['Charset'])."\n\n".$Body['Content']."\n";
|
---|
228 | }
|
---|
229 | return($Result);
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | /*
|
---|
234 | // Example
|
---|
235 | $Mail = new Mail();
|
---|
236 | $Mail->AddTo('nobody@nodomain', 'Recipient');
|
---|
237 | $Mail->From = 'No reply <noreply@nodomain>';
|
---|
238 | $Mail->Subject = 'Test message';
|
---|
239 | $Mail->AddBody('This is sample message sent by PHP Mail class');
|
---|
240 | $Mail->AddBody('This is sample <strong>HTML</strong> message sent by <i>PHP Mail class</i>', 'text/html');
|
---|
241 | $Mail->AttachFile('mailtest.php', 'text/plain');
|
---|
242 | $Mail->AttachData('DataFile.txt', 'text/plain', 'Sample text');
|
---|
243 | $Mail->Send();
|
---|
244 | */
|
---|