Commit | Line | Data |
---|---|---|
0c81b5b3 | 1 | <?php |
4f96c398 | 2 | /* ***** BEGIN LICENSE BLOCK ***** |
3 | * | |
4 | * The contents of this file are subject to Austrian copyright reegulations | |
5 | * ("Urheberrecht"); you may not use this file except in compliance with | |
6 | * those laws. | |
7 | * This contents and any derived work, if it gets distributed in any way, | |
8 | * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, | |
9 | * either express or implied. | |
10 | * | |
11 | * The Original Code is KaiRo's E-Mail module. | |
12 | * | |
13 | * The Initial Developer of the Original Code is | |
14 | * KaiRo - Robert Kaiser. | |
1d38ad3e | 15 | * Portions created by the Initial Developer are Copyright (C) 2003-2006 |
4f96c398 | 16 | * the Initial Developer. All Rights Reserved. |
17 | * | |
18 | * Contributor(s): Robert Kaiser <kairo@kairo.at> | |
19 | * | |
20 | * ***** END LICENSE BLOCK ***** */ | |
21 | ||
0c81b5b3 | 22 | class email { |
23 | // email PHP class | |
24 | // class/object for creating a new mail and send it | |
25 | // | |
3077a4f6 | 26 | // function __construct() |
0c81b5b3 | 27 | // CONSTRUCTOR |
28 | // | |
3077a4f6 | 29 | // private $debug_toSingleAddress |
0c81b5b3 | 30 | // address to send mail to in debug mode |
31 | // | |
3077a4f6 | 32 | // private $subject |
0c81b5b3 | 33 | // the mail's subject line |
34 | // | |
3077a4f6 | 35 | // private $sender |
0c81b5b3 | 36 | // the mail's sender (array; fields see recipients) |
37 | // | |
3077a4f6 | 38 | // private $replyto |
0c81b5b3 | 39 | // Reply-to address (array; fields see recipients) |
40 | // | |
3077a4f6 | 41 | // private $recipients |
0c81b5b3 | 42 | // array of recipients (To: line) |
43 | // fields: name - real name | |
44 | // mail - email address | |
45 | // | |
3077a4f6 | 46 | // private $cc |
0c81b5b3 | 47 | // array of CC recipients (fields like recipients) |
48 | // | |
3077a4f6 | 49 | // private $bcc |
0c81b5b3 | 50 | // array of BCC recipients (fields like recipients) |
51 | // | |
3077a4f6 | 52 | // private $headers |
0c81b5b3 | 53 | // array containing all additional headers |
54 | // fields: name - headers name | |
55 | // content - header content | |
56 | // | |
3077a4f6 | 57 | // private $content_type |
0c81b5b3 | 58 | // the mail's content type (MIME-type) [default: text/plain] |
59 | // | |
3077a4f6 | 60 | // private $charset |
0c81b5b3 | 61 | // the mail's charset [default: iso-8859-15] |
62 | // | |
3077a4f6 | 63 | // private $mailtext |
0c81b5b3 | 64 | // the main mail body |
65 | // | |
3077a4f6 | 66 | // private $attachments |
0c81b5b3 | 67 | // array containing all attachments |
68 | // fields: name - attachment name | |
69 | // content - attachment content | |
70 | // type - MIME type of that attachment | |
71 | // | |
3077a4f6 | 72 | // public function setDebugAddress($debug_email) |
0c81b5b3 | 73 | // debug mode: send only to this address |
74 | // | |
3077a4f6 | 75 | // public function setSubject($newsubject) |
0c81b5b3 | 76 | // set subject of mail |
77 | // | |
3077a4f6 | 78 | // public function setSender($email, [$name]) |
0c81b5b3 | 79 | // set sender of mail |
80 | // | |
3077a4f6 | 81 | // public function setReplyTo($email, [$name]) |
0c81b5b3 | 82 | // set reply-to address |
83 | // | |
3077a4f6 | 84 | // public function addRecipient($email, [$name]) |
0c81b5b3 | 85 | // add a recipient to the mail |
86 | // | |
3077a4f6 | 87 | // public function addCC($email, [$name]) |
0c81b5b3 | 88 | // add a CC recipient to the mail |
89 | // | |
3077a4f6 | 90 | // public function addBCC($email, [$name]) |
0c81b5b3 | 91 | // add a BCC recipient to the mail |
92 | // | |
3077a4f6 | 93 | // public function addHeader($hname, [$hcontent]) |
0c81b5b3 | 94 | // add a header to the mail |
95 | // | |
025016ca | 96 | // public function addHeaderAddress($hname, $email, [$name]) |
97 | // add an address header to the mail, possibly with both name and mail parts | |
98 | // | |
3aa9a02f RK |
99 | // public function setCharset($newcharset) |
100 | // set charset for this mail | |
101 | // | |
3077a4f6 | 102 | // public function addMailText($textpart) |
0c81b5b3 | 103 | // add some text to the mail |
104 | // | |
3077a4f6 | 105 | // public function addAttachment($aname, $acontent, [$atype]) |
d3f5b37c | 106 | // add an attachment to the mail, use given file name, content and MIME type (defaults to application/octet-stream) |
107 | // | |
a04c09ff RK |
108 | // public function getAddresses([$addrtype]) |
109 | // returns an array of all addresses this mail gets sent to | |
110 | // fields: email, name, addrtype | |
111 | // addrtype is one of to/cc/bcc | |
112 | // the $addrtype parameter is a comma-separated list of such types, default: all of them | |
113 | // | |
3077a4f6 | 114 | // public function send() |
0c81b5b3 | 115 | // really send the mail |
116 | // | |
4edf094e | 117 | // private function mimeencode($fieldtext, [$stringescape]) |
0c81b5b3 | 118 | // helper function: |
4edf094e | 119 | // encode given field text, ready to be placed into an e-mail MIME header |
120 | // if the boolean $stringescape is true, make sure this is sent as a single word in RFC2822 context (e.g. for names) | |
121 | // see http://www.ietf.org/rfc/rfc2822.txt for the RFC in question | |
0c81b5b3 | 122 | |
3077a4f6 | 123 | private $debug_toSingleAddress = ''; |
124 | private $subject; | |
125 | private $sender = array(); | |
126 | private $replyto = array(); | |
127 | private $recipients = array(); | |
128 | private $cc = array(); | |
129 | private $bcc = array(); | |
130 | private $headers = array(); | |
131 | private $content_type = 'text/plain'; | |
132 | private $charset = 'iso-8859-15'; | |
133 | private $mailtext = ''; | |
134 | private $attachments = array(); | |
0c81b5b3 | 135 | |
3077a4f6 | 136 | function __construct() { |
0c81b5b3 | 137 | // *** constructor *** |
138 | } | |
139 | ||
3077a4f6 | 140 | public function setDebugAddress($debug_email) { $this->debug_toSingleAddress = $debug_email; } |
0c81b5b3 | 141 | |
3077a4f6 | 142 | public function setSubject($newsubject) { $this->subject = $newsubject; } |
0c81b5b3 | 143 | |
3077a4f6 | 144 | public function setSender($email, $name = '') { $this->sender = array('mail' => $email, 'name' => $name); } |
0c81b5b3 | 145 | |
3077a4f6 | 146 | public function setReplyTo($email, $name = '') { $this->replyto = array('mail' => $email, 'name' => $name); } |
0c81b5b3 | 147 | |
3077a4f6 | 148 | public function addRecipient($email, $name = '') { |
1d38ad3e | 149 | $this->recipients[] = array('mail' => $email, 'name' => $name); |
0c81b5b3 | 150 | } |
151 | ||
3077a4f6 | 152 | public function addCC($email, $name = '') { |
1d38ad3e | 153 | $this->cc[] = array('mail' => $email, 'name' => $name); |
0c81b5b3 | 154 | } |
155 | ||
3077a4f6 | 156 | public function addBCC($email, $name = '') { |
1d38ad3e | 157 | $this->bcc[] = array('mail' => $email, 'name' => $name); |
0c81b5b3 | 158 | } |
159 | ||
3077a4f6 | 160 | public function addHeader($hname, $hcontent = '') { |
1d38ad3e | 161 | $this->headers[] = array('name' => $hname, 'content' => $hcontent); |
025016ca | 162 | } |
163 | ||
164 | public function addHeaderAddress($hname, $email, $name = '') { | |
165 | if (strlen($name)) { $hcontent = $this->mimeencode($name, true).' <'.$email.'>'; } | |
166 | else { $hcontent = $email; } | |
167 | $this->headers[] = array('name' => $hname, 'content' => $hcontent); | |
0c81b5b3 | 168 | } |
169 | ||
3aa9a02f RK |
170 | public function setCharset($newcharset) { $this->charset = $newcharset; } |
171 | ||
3077a4f6 | 172 | public function addMailText($textpart) { $this->mailtext .= $textpart; } |
0c81b5b3 | 173 | |
3077a4f6 | 174 | public function addAttachment($aname, $acontent, $atype = 'application/octet-stream') { |
1d38ad3e | 175 | $this->attachments[] = array('name' => $aname, 'content' => $acontent, 'type' => $atype); |
0c81b5b3 | 176 | } |
177 | ||
a04c09ff RK |
178 | public function getAddresses($addrtype = null) { |
179 | // returns all addresses this mail gets sent to | |
43bc07b3 RK |
180 | if (!is_array($addrtype)) { |
181 | if (strlen($addrtype)) { $addrtype = explode(',', strtolower($addrtype)); } | |
182 | else { $addrtype = array('to','cc','bcc'); } | |
183 | } | |
a04c09ff RK |
184 | $mailaddresses = array(); |
185 | ||
43bc07b3 | 186 | if (in_array('to', $addrtype)) { |
a04c09ff RK |
187 | foreach ($this->recipients as $address) { |
188 | if (strlen(@$address['mail'])) { | |
189 | $mailaddresses[] = array('mail'=>$address['mail'], | |
190 | 'name'=>strlen($address['name'])?$address['name']:'', | |
191 | 'addrtype'=>'to'); | |
192 | } | |
193 | } | |
194 | } | |
43bc07b3 | 195 | if (in_array('cc', $addrtype)) { |
a04c09ff RK |
196 | foreach ($this->cc as $address) { |
197 | if (strlen(@$address['mail'])) { | |
198 | $mailaddresses[] = array('mail'=>$address['mail'], | |
199 | 'name'=>strlen($address['name'])?$address['name']:'', | |
200 | 'addrtype'=>'cc'); | |
201 | } | |
202 | } | |
203 | } | |
43bc07b3 | 204 | if (in_array('bcc', $addrtype)) { |
a04c09ff RK |
205 | foreach ($this->bcc as $address) { |
206 | if (strlen(@$address['mail'])) { | |
207 | $mailaddresses[] = array('mail'=>$address['mail'], | |
208 | 'name'=>strlen($address['name'])?$address['name']:'', | |
209 | 'addrtype'=>'bcc'); | |
210 | } | |
211 | } | |
212 | } | |
43bc07b3 RK |
213 | |
214 | return $mailaddresses; | |
a04c09ff RK |
215 | } |
216 | ||
3077a4f6 | 217 | public function send() { |
0c81b5b3 | 218 | global $util; |
1d38ad3e | 219 | $mtxt = ''; |
220 | $hdrs = 'MIME-Version: 1.0'."\n"; | |
0c81b5b3 | 221 | $subj = $this->mimeencode($this->subject); |
1d38ad3e | 222 | if (strlen($this->sender['name'])) { |
4edf094e | 223 | $hdrs .= 'From: '.$this->mimeencode($this->sender['name'], true).' <'.$this->sender['mail'].'>'."\n"; |
0c81b5b3 | 224 | } |
1d38ad3e | 225 | else { $hdrs .= 'From: '.$this->sender['mail']."\n"; } |
0c81b5b3 | 226 | if (count($this->replyto)) { |
1d38ad3e | 227 | if (strlen($this->replyto['name'])) { |
4edf094e | 228 | $hdrs .= 'Reply-to: '.$this->mimeencode($this->replyto['name'], true).' <'.$this->replyto['mail'].'>'."\n"; |
0c81b5b3 | 229 | } |
1d38ad3e | 230 | else { $hdrs .= 'Reply-to: '.$this->replyto['mail']."\n"; } |
0c81b5b3 | 231 | } |
232 | if (count($this->recipients)) { | |
1d38ad3e | 233 | $recpt = ''; |
0c81b5b3 | 234 | foreach ($this->recipients as $address) { |
ba79b48c RK |
235 | if (strlen(@$address['mail'])) { |
236 | if (strlen($address['name'])) { $recpt .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; } | |
237 | else { $recpt .= $address['mail'].','; } | |
238 | } | |
0c81b5b3 | 239 | } |
1d38ad3e | 240 | $recpt = preg_replace('/,$/', '', $recpt); |
0c81b5b3 | 241 | } |
ba79b48c RK |
242 | if (!strlen($recpt)) { |
243 | return null; | |
244 | } | |
0c81b5b3 | 245 | if (count($this->cc)) { |
1d38ad3e | 246 | $adrs = ''; |
0c81b5b3 | 247 | foreach ($this->cc as $address) { |
4edf094e | 248 | if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; } |
1d38ad3e | 249 | else { $adrs .= $address['mail'].','; } |
0c81b5b3 | 250 | } |
1d38ad3e | 251 | $adrs = preg_replace('/,$/', '', $adrs); |
252 | $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Cc: '.$adrs."\n"; | |
0c81b5b3 | 253 | } |
254 | if (count($this->bcc)) { | |
1d38ad3e | 255 | $adrs = ''; |
0c81b5b3 | 256 | foreach ($this->bcc as $address) { |
4edf094e | 257 | if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; } |
1d38ad3e | 258 | else { $adrs .= $address['mail'].','; } |
0c81b5b3 | 259 | } |
1d38ad3e | 260 | $adrs = preg_replace('/,$/', '', $adrs); |
261 | $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Bcc: '.$adrs."\n"; | |
0c81b5b3 | 262 | } |
263 | if (count($this->headers)) { | |
264 | foreach ($this->headers as $header) { | |
1d38ad3e | 265 | $hdrs .= $header['name'].': '.$header['content']."\n"; |
0c81b5b3 | 266 | } |
267 | } | |
268 | if (count($this->attachments)) { | |
269bd147 | 269 | // create random boundary, 20 chars, always beginning with KaiRo ;-) |
270 | $boundary = 'KaiRo'; | |
1d38ad3e | 271 | for ($i = 1; $i <= 15; $i++) { |
272 | $r = rand(0, 61); | |
273 | if ($r < 10) { $boundary .= chr($r + 48); } | |
274 | elseif ($r < 36) { $boundary .= chr($r + 55); } | |
275 | elseif ($r < 62) { $boundary .= chr($r + 61); } | |
269bd147 | 276 | } |
1d38ad3e | 277 | $hdrs .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'";'."\n"; |
278 | $hdrs .= 'Content-Transfer-Encoding: 7bit'."\n"; | |
279 | $mtxt .= 'This part of the E-mail should never be seen. If'."\n"; | |
280 | $mtxt .= 'you are reading this, consider upgrading your e-mail'."\n"; | |
281 | $mtxt .= 'client to a MIME-compatible client.'."\n"; | |
282 | $mtxt .= "\n".'--'.$boundary."\n"; | |
283 | if (preg_match('|^text/|', $this->content_type)) { | |
284 | $mtxt .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n"; | |
0c81b5b3 | 285 | } |
286 | else { | |
1d38ad3e | 287 | $mtxt .= 'Content-Type: '.$this->content_type."\n"; |
0c81b5b3 | 288 | } |
1d38ad3e | 289 | $mtxt .= 'Content-Transfer-Encoding: 8bit'."\n\n"; |
0c81b5b3 | 290 | } |
291 | else { | |
1d38ad3e | 292 | if (preg_match('|^text/|', $this->content_type)) { |
293 | $hdrs .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n"; | |
0c81b5b3 | 294 | } |
295 | else { | |
1d38ad3e | 296 | $hdrs .= 'Content-Type: '.$this->content_type."\n"; |
0c81b5b3 | 297 | } |
1d38ad3e | 298 | $hdrs .= 'Content-Transfer-Encoding: 8bit'."\n"; |
0c81b5b3 | 299 | } |
300 | $mtxt .= stripslashes($this->mailtext); | |
301 | if (count($this->attachments)) { | |
302 | foreach ($this->attachments as $attach) { | |
1d38ad3e | 303 | $mtxt .= "\n".'--'.$boundary."\n"; |
304 | $mtxt .= 'Content-Type: '.$attach['type'].'; name="'.$attach['name'].'";'."\n"; | |
1b1735d1 | 305 | if (preg_match('/^(text|message)\//', $attach['type'])) { |
306 | $mtxt .= 'Content-Transfer-Encoding: 8bit'."\n"; | |
307 | $mtxt .= 'Content-Disposition: attachment'."\n\n"; | |
308 | $mtxt .= $attach['content']; | |
309 | $mtxt .= "\n"; | |
310 | } | |
311 | else { | |
312 | $mtxt .= 'Content-Transfer-Encoding: base64'."\n"; | |
313 | $mtxt .= 'Content-Disposition: attachment'."\n\n"; | |
314 | $mtxt .= rtrim(chunk_split(base64_encode($attach['content']), 76)); ; | |
315 | $mtxt .= "\n"; | |
316 | } | |
0c81b5b3 | 317 | } |
1b1735d1 | 318 | $mtxt .= '--'.$boundary.'--'."\n"; |
0c81b5b3 | 319 | } |
320 | ||
321 | if (strlen($this->debug_toSingleAddress)) { | |
1d38ad3e | 322 | $hdrs .= 'X-Real-To: '.$recpt."\n"; |
0c81b5b3 | 323 | $recpt = $this->debug_toSingleAddress; |
324 | } | |
325 | ||
1d38ad3e | 326 | //print('Subject: '.$util->htmlify($subj).'<br>'."\n"); |
327 | //print('To: '.$util->htmlify($recpt).'<br>'."\n"); | |
0c81b5b3 | 328 | //print(nl2br($util->htmlify($hdrs))); |
329 | //print(nl2br($util->htmlify($mtxt))); | |
61432682 | 330 | return mail($recpt, $subj, $mtxt, $hdrs); |
0c81b5b3 | 331 | } |
332 | ||
4edf094e | 333 | private function mimeencode($fieldtext, $stringescape = false) { |
0c81b5b3 | 334 | $mText = imap_8bit($fieldtext); |
4edf094e | 335 | $is_qpformat = ($mText != $fieldtext); |
336 | if ($stringescape && preg_match('/[^\w !#$%&\'*+\/=?^`{|}~-]/', $mText)) { | |
337 | // if needed, make this a quoted-string instead of an atom (to speak in RFC2822 language) | |
338 | $mText = '"'.strtr($mText, array('"' => '\"', '\\' => '\\\\')).'"'; | |
339 | } | |
340 | if ($is_qpformat) { | |
341 | $mText = strtr($mText, array('_' => '=5F', ' ' => '_', '?' => '=3F')); | |
1d38ad3e | 342 | $mText = '=?'.strtoupper($this->charset).'?Q?'.$mText.'?='; |
0c81b5b3 | 343 | } |
344 | return $mText; | |
345 | } | |
0c81b5b3 | 346 | } |
347 | ?> |