point to actual location of rrdtool
[php-utility-classes.git] / include / classes / email.php-class
CommitLineData
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 22class 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 //
3077a4f6 99 // public function addMailText($textpart)
0c81b5b3 100 // add some text to the mail
101 //
3077a4f6 102 // public function addAttachment($aname, $acontent, [$atype])
d3f5b37c 103 // add an attachment to the mail, use given file name, content and MIME type (defaults to application/octet-stream)
104 //
a04c09ff
RK
105 // public function getAddresses([$addrtype])
106 // returns an array of all addresses this mail gets sent to
107 // fields: email, name, addrtype
108 // addrtype is one of to/cc/bcc
109 // the $addrtype parameter is a comma-separated list of such types, default: all of them
110 //
3077a4f6 111 // public function send()
0c81b5b3 112 // really send the mail
113 //
4edf094e 114 // private function mimeencode($fieldtext, [$stringescape])
0c81b5b3 115 // helper function:
4edf094e 116 // encode given field text, ready to be placed into an e-mail MIME header
117 // if the boolean $stringescape is true, make sure this is sent as a single word in RFC2822 context (e.g. for names)
118 // see http://www.ietf.org/rfc/rfc2822.txt for the RFC in question
0c81b5b3 119
3077a4f6 120 private $debug_toSingleAddress = '';
121 private $subject;
122 private $sender = array();
123 private $replyto = array();
124 private $recipients = array();
125 private $cc = array();
126 private $bcc = array();
127 private $headers = array();
128 private $content_type = 'text/plain';
129 private $charset = 'iso-8859-15';
130 private $mailtext = '';
131 private $attachments = array();
0c81b5b3 132
3077a4f6 133 function __construct() {
0c81b5b3 134 // *** constructor ***
135 }
136
3077a4f6 137 public function setDebugAddress($debug_email) { $this->debug_toSingleAddress = $debug_email; }
0c81b5b3 138
3077a4f6 139 public function setSubject($newsubject) { $this->subject = $newsubject; }
0c81b5b3 140
3077a4f6 141 public function setSender($email, $name = '') { $this->sender = array('mail' => $email, 'name' => $name); }
0c81b5b3 142
3077a4f6 143 public function setReplyTo($email, $name = '') { $this->replyto = array('mail' => $email, 'name' => $name); }
0c81b5b3 144
3077a4f6 145 public function addRecipient($email, $name = '') {
1d38ad3e 146 $this->recipients[] = array('mail' => $email, 'name' => $name);
0c81b5b3 147 }
148
3077a4f6 149 public function addCC($email, $name = '') {
1d38ad3e 150 $this->cc[] = array('mail' => $email, 'name' => $name);
0c81b5b3 151 }
152
3077a4f6 153 public function addBCC($email, $name = '') {
1d38ad3e 154 $this->bcc[] = array('mail' => $email, 'name' => $name);
0c81b5b3 155 }
156
3077a4f6 157 public function addHeader($hname, $hcontent = '') {
1d38ad3e 158 $this->headers[] = array('name' => $hname, 'content' => $hcontent);
025016ca 159 }
160
161 public function addHeaderAddress($hname, $email, $name = '') {
162 if (strlen($name)) { $hcontent = $this->mimeencode($name, true).' <'.$email.'>'; }
163 else { $hcontent = $email; }
164 $this->headers[] = array('name' => $hname, 'content' => $hcontent);
0c81b5b3 165 }
166
3077a4f6 167 public function addMailText($textpart) { $this->mailtext .= $textpart; }
0c81b5b3 168
3077a4f6 169 public function addAttachment($aname, $acontent, $atype = 'application/octet-stream') {
1d38ad3e 170 $this->attachments[] = array('name' => $aname, 'content' => $acontent, 'type' => $atype);
0c81b5b3 171 }
172
a04c09ff
RK
173 public function getAddresses($addrtype = null) {
174 // returns all addresses this mail gets sent to
43bc07b3
RK
175 if (!is_array($addrtype)) {
176 if (strlen($addrtype)) { $addrtype = explode(',', strtolower($addrtype)); }
177 else { $addrtype = array('to','cc','bcc'); }
178 }
a04c09ff
RK
179 $mailaddresses = array();
180
43bc07b3 181 if (in_array('to', $addrtype)) {
a04c09ff
RK
182 foreach ($this->recipients as $address) {
183 if (strlen(@$address['mail'])) {
184 $mailaddresses[] = array('mail'=>$address['mail'],
185 'name'=>strlen($address['name'])?$address['name']:'',
186 'addrtype'=>'to');
187 }
188 }
189 }
43bc07b3 190 if (in_array('cc', $addrtype)) {
a04c09ff
RK
191 foreach ($this->cc as $address) {
192 if (strlen(@$address['mail'])) {
193 $mailaddresses[] = array('mail'=>$address['mail'],
194 'name'=>strlen($address['name'])?$address['name']:'',
195 'addrtype'=>'cc');
196 }
197 }
198 }
43bc07b3 199 if (in_array('bcc', $addrtype)) {
a04c09ff
RK
200 foreach ($this->bcc as $address) {
201 if (strlen(@$address['mail'])) {
202 $mailaddresses[] = array('mail'=>$address['mail'],
203 'name'=>strlen($address['name'])?$address['name']:'',
204 'addrtype'=>'bcc');
205 }
206 }
207 }
43bc07b3
RK
208
209 return $mailaddresses;
a04c09ff
RK
210 }
211
3077a4f6 212 public function send() {
0c81b5b3 213 global $util;
1d38ad3e 214 $mtxt = '';
215 $hdrs = 'MIME-Version: 1.0'."\n";
0c81b5b3 216 $subj = $this->mimeencode($this->subject);
1d38ad3e 217 if (strlen($this->sender['name'])) {
4edf094e 218 $hdrs .= 'From: '.$this->mimeencode($this->sender['name'], true).' <'.$this->sender['mail'].'>'."\n";
0c81b5b3 219 }
1d38ad3e 220 else { $hdrs .= 'From: '.$this->sender['mail']."\n"; }
0c81b5b3 221 if (count($this->replyto)) {
1d38ad3e 222 if (strlen($this->replyto['name'])) {
4edf094e 223 $hdrs .= 'Reply-to: '.$this->mimeencode($this->replyto['name'], true).' <'.$this->replyto['mail'].'>'."\n";
0c81b5b3 224 }
1d38ad3e 225 else { $hdrs .= 'Reply-to: '.$this->replyto['mail']."\n"; }
0c81b5b3 226 }
227 if (count($this->recipients)) {
1d38ad3e 228 $recpt = '';
0c81b5b3 229 foreach ($this->recipients as $address) {
ba79b48c
RK
230 if (strlen(@$address['mail'])) {
231 if (strlen($address['name'])) { $recpt .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
232 else { $recpt .= $address['mail'].','; }
233 }
0c81b5b3 234 }
1d38ad3e 235 $recpt = preg_replace('/,$/', '', $recpt);
0c81b5b3 236 }
ba79b48c
RK
237 if (!strlen($recpt)) {
238 return null;
239 }
0c81b5b3 240 if (count($this->cc)) {
1d38ad3e 241 $adrs = '';
0c81b5b3 242 foreach ($this->cc as $address) {
4edf094e 243 if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
1d38ad3e 244 else { $adrs .= $address['mail'].','; }
0c81b5b3 245 }
1d38ad3e 246 $adrs = preg_replace('/,$/', '', $adrs);
247 $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Cc: '.$adrs."\n";
0c81b5b3 248 }
249 if (count($this->bcc)) {
1d38ad3e 250 $adrs = '';
0c81b5b3 251 foreach ($this->bcc as $address) {
4edf094e 252 if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
1d38ad3e 253 else { $adrs .= $address['mail'].','; }
0c81b5b3 254 }
1d38ad3e 255 $adrs = preg_replace('/,$/', '', $adrs);
256 $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Bcc: '.$adrs."\n";
0c81b5b3 257 }
258 if (count($this->headers)) {
259 foreach ($this->headers as $header) {
1d38ad3e 260 $hdrs .= $header['name'].': '.$header['content']."\n";
0c81b5b3 261 }
262 }
263 if (count($this->attachments)) {
269bd147 264 // create random boundary, 20 chars, always beginning with KaiRo ;-)
265 $boundary = 'KaiRo';
1d38ad3e 266 for ($i = 1; $i <= 15; $i++) {
267 $r = rand(0, 61);
268 if ($r < 10) { $boundary .= chr($r + 48); }
269 elseif ($r < 36) { $boundary .= chr($r + 55); }
270 elseif ($r < 62) { $boundary .= chr($r + 61); }
269bd147 271 }
1d38ad3e 272 $hdrs .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'";'."\n";
273 $hdrs .= 'Content-Transfer-Encoding: 7bit'."\n";
274 $mtxt .= 'This part of the E-mail should never be seen. If'."\n";
275 $mtxt .= 'you are reading this, consider upgrading your e-mail'."\n";
276 $mtxt .= 'client to a MIME-compatible client.'."\n";
277 $mtxt .= "\n".'--'.$boundary."\n";
278 if (preg_match('|^text/|', $this->content_type)) {
279 $mtxt .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n";
0c81b5b3 280 }
281 else {
1d38ad3e 282 $mtxt .= 'Content-Type: '.$this->content_type."\n";
0c81b5b3 283 }
1d38ad3e 284 $mtxt .= 'Content-Transfer-Encoding: 8bit'."\n\n";
0c81b5b3 285 }
286 else {
1d38ad3e 287 if (preg_match('|^text/|', $this->content_type)) {
288 $hdrs .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n";
0c81b5b3 289 }
290 else {
1d38ad3e 291 $hdrs .= 'Content-Type: '.$this->content_type."\n";
0c81b5b3 292 }
1d38ad3e 293 $hdrs .= 'Content-Transfer-Encoding: 8bit'."\n";
0c81b5b3 294 }
295 $mtxt .= stripslashes($this->mailtext);
296 if (count($this->attachments)) {
297 foreach ($this->attachments as $attach) {
1d38ad3e 298 $mtxt .= "\n".'--'.$boundary."\n";
299 $mtxt .= 'Content-Type: '.$attach['type'].'; name="'.$attach['name'].'";'."\n";
1b1735d1 300 if (preg_match('/^(text|message)\//', $attach['type'])) {
301 $mtxt .= 'Content-Transfer-Encoding: 8bit'."\n";
302 $mtxt .= 'Content-Disposition: attachment'."\n\n";
303 $mtxt .= $attach['content'];
304 $mtxt .= "\n";
305 }
306 else {
307 $mtxt .= 'Content-Transfer-Encoding: base64'."\n";
308 $mtxt .= 'Content-Disposition: attachment'."\n\n";
309 $mtxt .= rtrim(chunk_split(base64_encode($attach['content']), 76)); ;
310 $mtxt .= "\n";
311 }
0c81b5b3 312 }
1b1735d1 313 $mtxt .= '--'.$boundary.'--'."\n";
0c81b5b3 314 }
315
316 if (strlen($this->debug_toSingleAddress)) {
1d38ad3e 317 $hdrs .= 'X-Real-To: '.$recpt."\n";
0c81b5b3 318 $recpt = $this->debug_toSingleAddress;
319 }
320
1d38ad3e 321 //print('Subject: '.$util->htmlify($subj).'<br>'."\n");
322 //print('To: '.$util->htmlify($recpt).'<br>'."\n");
0c81b5b3 323 //print(nl2br($util->htmlify($hdrs)));
324 //print(nl2br($util->htmlify($mtxt)));
61432682 325 return mail($recpt, $subj, $mtxt, $hdrs);
0c81b5b3 326 }
327
4edf094e 328 private function mimeencode($fieldtext, $stringescape = false) {
0c81b5b3 329 $mText = imap_8bit($fieldtext);
4edf094e 330 $is_qpformat = ($mText != $fieldtext);
331 if ($stringescape && preg_match('/[^\w !#$%&\'*+\/=?^`{|}~-]/', $mText)) {
332 // if needed, make this a quoted-string instead of an atom (to speak in RFC2822 language)
333 $mText = '"'.strtr($mText, array('"' => '\"', '\\' => '\\\\')).'"';
334 }
335 if ($is_qpformat) {
336 $mText = strtr($mText, array('_' => '=5F', ' ' => '_', '?' => '=3F'));
1d38ad3e 337 $mText = '=?'.strtoupper($this->charset).'?Q?'.$mText.'?=';
0c81b5b3 338 }
339 return $mText;
340 }
0c81b5b3 341}
342?>