PHP5ize several modules
[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 //
3077a4f6 96 // public function addMailText($textpart)
0c81b5b3 97 // add some text to the mail
98 //
3077a4f6 99 // public function addAttachment($aname, $acontent, [$atype])
d3f5b37c 100 // add an attachment to the mail, use given file name, content and MIME type (defaults to application/octet-stream)
101 //
3077a4f6 102 // public function send()
0c81b5b3 103 // really send the mail
104 //
3077a4f6 105 // private function mimeencode($fieldtext)
0c81b5b3 106 // helper function:
107 // encode given field text, ready to be placed into an e-mail MIME header
108
3077a4f6 109 private $debug_toSingleAddress = '';
110 private $subject;
111 private $sender = array();
112 private $replyto = array();
113 private $recipients = array();
114 private $cc = array();
115 private $bcc = array();
116 private $headers = array();
117 private $content_type = 'text/plain';
118 private $charset = 'iso-8859-15';
119 private $mailtext = '';
120 private $attachments = array();
0c81b5b3 121
3077a4f6 122 function __construct() {
0c81b5b3 123 // *** constructor ***
124 }
125
3077a4f6 126 public function setDebugAddress($debug_email) { $this->debug_toSingleAddress = $debug_email; }
0c81b5b3 127
3077a4f6 128 public function setSubject($newsubject) { $this->subject = $newsubject; }
0c81b5b3 129
3077a4f6 130 public function setSender($email, $name = '') { $this->sender = array('mail' => $email, 'name' => $name); }
0c81b5b3 131
3077a4f6 132 public function setReplyTo($email, $name = '') { $this->replyto = array('mail' => $email, 'name' => $name); }
0c81b5b3 133
3077a4f6 134 public function addRecipient($email, $name = '') {
1d38ad3e 135 $this->recipients[] = array('mail' => $email, 'name' => $name);
0c81b5b3 136 }
137
3077a4f6 138 public function addCC($email, $name = '') {
1d38ad3e 139 $this->cc[] = array('mail' => $email, 'name' => $name);
0c81b5b3 140 }
141
3077a4f6 142 public function addBCC($email, $name = '') {
1d38ad3e 143 $this->bcc[] = array('mail' => $email, 'name' => $name);
0c81b5b3 144 }
145
3077a4f6 146 public function addHeader($hname, $hcontent = '') {
1d38ad3e 147 $this->headers[] = array('name' => $hname, 'content' => $hcontent);
0c81b5b3 148 }
149
3077a4f6 150 public function addMailText($textpart) { $this->mailtext .= $textpart; }
0c81b5b3 151
3077a4f6 152 public function addAttachment($aname, $acontent, $atype = 'application/octet-stream') {
1d38ad3e 153 $this->attachments[] = array('name' => $aname, 'content' => $acontent, 'type' => $atype);
0c81b5b3 154 }
155
3077a4f6 156 public function send() {
0c81b5b3 157 global $util;
1d38ad3e 158 $mtxt = '';
159 $hdrs = 'MIME-Version: 1.0'."\n";
0c81b5b3 160 $subj = $this->mimeencode($this->subject);
1d38ad3e 161 if (strlen($this->sender['name'])) {
162 $hdrs .= 'From: '.$this->mimeencode($this->sender['name']).' <'.$this->sender['mail'].'>'."\n";
0c81b5b3 163 }
1d38ad3e 164 else { $hdrs .= 'From: '.$this->sender['mail']."\n"; }
0c81b5b3 165 if (count($this->replyto)) {
1d38ad3e 166 if (strlen($this->replyto['name'])) {
167 $hdrs .= 'Reply-to: '.$this->mimeencode($this->replyto['name']).' <'.$this->replyto['mail'].'>'."\n";
0c81b5b3 168 }
1d38ad3e 169 else { $hdrs .= 'Reply-to: '.$this->replyto['mail']."\n"; }
0c81b5b3 170 }
171 if (count($this->recipients)) {
1d38ad3e 172 $recpt = '';
0c81b5b3 173 foreach ($this->recipients as $address) {
1d38ad3e 174 if (strlen($address['name'])) { $recpt .= $this->mimeencode($address['name']).' <'.$address['mail'].'>,'; }
175 else { $recpt .= $address['mail'].','; }
0c81b5b3 176 }
1d38ad3e 177 $recpt = preg_replace('/,$/', '', $recpt);
0c81b5b3 178 }
179 if (count($this->cc)) {
1d38ad3e 180 $adrs = '';
0c81b5b3 181 foreach ($this->cc as $address) {
1d38ad3e 182 if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name']).' <'.$address['mail'].'>,'; }
183 else { $adrs .= $address['mail'].','; }
0c81b5b3 184 }
1d38ad3e 185 $adrs = preg_replace('/,$/', '', $adrs);
186 $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Cc: '.$adrs."\n";
0c81b5b3 187 }
188 if (count($this->bcc)) {
1d38ad3e 189 $adrs = '';
0c81b5b3 190 foreach ($this->bcc as $address) {
1d38ad3e 191 if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name']).' <'.$address['mail'].'>,'; }
192 else { $adrs .= $address['mail'].','; }
0c81b5b3 193 }
1d38ad3e 194 $adrs = preg_replace('/,$/', '', $adrs);
195 $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Bcc: '.$adrs."\n";
0c81b5b3 196 }
197 if (count($this->headers)) {
198 foreach ($this->headers as $header) {
1d38ad3e 199 $hdrs .= $header['name'].': '.$header['content']."\n";
0c81b5b3 200 }
201 }
202 if (count($this->attachments)) {
269bd147 203 // create random boundary, 20 chars, always beginning with KaiRo ;-)
204 $boundary = 'KaiRo';
1d38ad3e 205 for ($i = 1; $i <= 15; $i++) {
206 $r = rand(0, 61);
207 if ($r < 10) { $boundary .= chr($r + 48); }
208 elseif ($r < 36) { $boundary .= chr($r + 55); }
209 elseif ($r < 62) { $boundary .= chr($r + 61); }
269bd147 210 }
1d38ad3e 211 $hdrs .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'";'."\n";
212 $hdrs .= 'Content-Transfer-Encoding: 7bit'."\n";
213 $mtxt .= 'This part of the E-mail should never be seen. If'."\n";
214 $mtxt .= 'you are reading this, consider upgrading your e-mail'."\n";
215 $mtxt .= 'client to a MIME-compatible client.'."\n";
216 $mtxt .= "\n".'--'.$boundary."\n";
217 if (preg_match('|^text/|', $this->content_type)) {
218 $mtxt .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n";
0c81b5b3 219 }
220 else {
1d38ad3e 221 $mtxt .= 'Content-Type: '.$this->content_type."\n";
0c81b5b3 222 }
1d38ad3e 223 $mtxt .= 'Content-Transfer-Encoding: 8bit'."\n\n";
0c81b5b3 224 }
225 else {
1d38ad3e 226 if (preg_match('|^text/|', $this->content_type)) {
227 $hdrs .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n";
0c81b5b3 228 }
229 else {
1d38ad3e 230 $hdrs .= 'Content-Type: '.$this->content_type."\n";
0c81b5b3 231 }
1d38ad3e 232 $hdrs .= 'Content-Transfer-Encoding: 8bit'."\n";
0c81b5b3 233 }
234 $mtxt .= stripslashes($this->mailtext);
235 if (count($this->attachments)) {
236 foreach ($this->attachments as $attach) {
1d38ad3e 237 $mtxt .= "\n".'--'.$boundary."\n";
238 $mtxt .= 'Content-Type: '.$attach['type'].'; name="'.$attach['name'].'";'."\n";
239 $mtxt .= 'Content-Transfer-Encoding: base64'."\n";
240 $mtxt .= 'Content-Disposition: attachment'."\n\n";
241 $mtxt .= rtrim(chunk_split(base64_encode($attach['content']), 76)); ;
0c81b5b3 242 $mtxt .= "\n";
243 }
1d38ad3e 244 $mtext .= '--'.$boundary.'--'."\n";
0c81b5b3 245 }
246
247 if (strlen($this->debug_toSingleAddress)) {
1d38ad3e 248 $hdrs .= 'X-Real-To: '.$recpt."\n";
0c81b5b3 249 $recpt = $this->debug_toSingleAddress;
250 }
251
1d38ad3e 252 //print('Subject: '.$util->htmlify($subj).'<br>'."\n");
253 //print('To: '.$util->htmlify($recpt).'<br>'."\n");
0c81b5b3 254 //print(nl2br($util->htmlify($hdrs)));
255 //print(nl2br($util->htmlify($mtxt)));
61432682 256 return mail($recpt, $subj, $mtxt, $hdrs);
0c81b5b3 257 }
258
3077a4f6 259 private function mimeencode($fieldtext) {
0c81b5b3 260 $mText = imap_8bit($fieldtext);
261 if ($mText != $fieldtext) {
1d38ad3e 262 $trans = array('_' => '=5F', ' ' => '_', '?' => '=3F');
0c81b5b3 263 $mText = strtr($mText, $trans);
1d38ad3e 264 $mText = '=?'.strtoupper($this->charset).'?Q?'.$mText.'?=';
0c81b5b3 265 }
266 return $mText;
267 }
268
269
270}
271?>