add new insertA/updateA interfaces from com project, add escaping. I just hope that...
[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.
15 * Portions created by the Initial Developer are Copyright (C) 2003-2004
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 //
26 // function email()
27 // CONSTRUCTOR
28 //
29 // var $debug_toSingleAddress
30 // address to send mail to in debug mode
31 //
32 // var $subject
33 // the mail's subject line
34 //
35 // var $sender
36 // the mail's sender (array; fields see recipients)
37 //
38 // var $replyto
39 // Reply-to address (array; fields see recipients)
40 //
41 // var $recipients
42 // array of recipients (To: line)
43 // fields: name - real name
44 // mail - email address
45 //
46 // var $cc
47 // array of CC recipients (fields like recipients)
48 //
49 // var $bcc
50 // array of BCC recipients (fields like recipients)
51 //
52 // var $headers
53 // array containing all additional headers
54 // fields: name - headers name
55 // content - header content
56 //
57 // var $content_type
58 // the mail's content type (MIME-type) [default: text/plain]
59 //
60 // var $charset
61 // the mail's charset [default: iso-8859-15]
62 //
63 // var $mailtext
64 // the main mail body
65 //
66 // var $attachments
67 // array containing all attachments
68 // fields: name - attachment name
69 // content - attachment content
70 // type - MIME type of that attachment
71 //
72 // function setDebugAddress($debug_email)
73 // debug mode: send only to this address
74 //
75 // function setSubject($newsubject)
76 // set subject of mail
77 //
78 // function setSender($email, [$name])
79 // set sender of mail
80 //
81 // function setReplyTo($email, [$name])
82 // set reply-to address
83 //
84 // function addRecipient($email, [$name])
85 // add a recipient to the mail
86 //
87 // function addCC($email, [$name])
88 // add a CC recipient to the mail
89 //
90 // function addBCC($email, [$name])
91 // add a BCC recipient to the mail
92 //
93 // function addHeader($hname, [$hcontent])
94 // add a header to the mail
95 //
96 // function addMailText($textpart)
97 // add some text to the mail
98 //
99 // function send()
100 // really send the mail
101 //
102 // function mimeencode($fieldtext)
103 // helper function:
104 // encode given field text, ready to be placed into an e-mail MIME header
105
106 var $debug_toSingleAddress = "";
107 var $subject;
108 var $sender = array();
109 var $replyto = array();
110 var $recipients = array();
111 var $cc = array();
112 var $bcc = array();
113 var $headers = array();
114 var $content_type = "text/plain";
115 var $charset = "iso-8859-15";
116 var $mailtext = "";
117 var $attachments = array();
118
119 function email() {
120 // *** constructor ***
121 }
122
123 function setDebugAddress($debug_email) { $this->debug_toSingleAddress = $debug_email; }
124
125 function setSubject($newsubject) { $this->subject = $newsubject; }
126
127 function setSender($email, $name = "") { $this->sender = array("mail" => $email, "name" => $name); }
128
129 function setReplyTo($email, $name = "") { $this->replyto = array("mail" => $email, "name" => $name); }
130
131 function addRecipient($email, $name = "") {
132 $this->recipients[] = array("mail" => $email, "name" => $name);
133 }
134
135 function addCC($email, $name = "") {
136 $this->cc[] = array("mail" => $email, "name" => $name);
137 }
138
139 function addBCC($email, $name = "") {
140 $this->bcc[] = array("mail" => $email, "name" => $name);
141 }
142
143 function addHeader($hname, $hcontent = "") {
144 $this->headers[] = array("name" => $hname, "content" => $hcontent);
145 }
146
147 function addMailText($textpart) { $this->mailtext .= $textpart; }
148
149 function addAttachment($aname, $acontent, $atype = "application/octet-stream") {
150 $this->attachments[] = array("name" => $aname, "content" => $acontent, "type" => $atype);
151 }
152
153 function send() {
154 global $util;
155 $mtxt = "";
156 $hdrs = "MIME-Version: 1.0\n";
157 $subj = $this->mimeencode($this->subject);
158 if (strlen($this->sender["name"])) {
159 $hdrs .= "From: ".$this->mimeencode($this->sender["name"])." <".$this->sender["mail"].">\n";
160 }
161 else { $hdrs .= "From: ".$this->sender["mail"]."\n"; }
162 if (count($this->replyto)) {
163 if (strlen($this->replyto["name"])) {
164 $hdrs .= "Reply-to: ".$this->mimeencode($this->replyto["name"])." <".$this->replyto["mail"].">\n";
165 }
166 else { $hdrs .= "Reply-to: ".$this->replyto["mail"]."\n"; }
167 }
168 if (count($this->recipients)) {
169 $recpt = "";
170 foreach ($this->recipients as $address) {
171 if (strlen($address["name"])) { $recpt .= $this->mimeencode($address["name"])." <".$address["mail"].">,"; }
172 else { $recpt .= $address["mail"].","; }
173 }
174 $recpt = ereg_replace(",$", "", $recpt);
175 }
176 if (count($this->cc)) {
177 $adrs = "";
178 foreach ($this->cc as $address) {
179 if (strlen($address["name"])) { $adrs .= $this->mimeencode($address["name"])." <".$address["mail"].">,"; }
180 else { $adrs .= $address["mail"].","; }
181 }
182 $adrs = ereg_replace(",$", "", $adrs);
183 $hdrs .= (strlen($this->debug_toSingleAddress)?"X-Real-":"")."Cc: $adrs\n";
184 }
185 if (count($this->bcc)) {
186 $adrs = "";
187 foreach ($this->bcc as $address) {
188 if (strlen($address["name"])) { $adrs .= $this->mimeencode($address["name"])." <".$address["mail"].">,"; }
189 else { $adrs .= $address["mail"].","; }
190 }
191 $adrs = ereg_replace(",$", "", $adrs);
192 $hdrs .= (strlen($this->debug_toSingleAddress)?"X-Real-":"")."Bcc: $adrs\n";
193 }
194 if (count($this->headers)) {
195 foreach ($this->headers as $header) {
196 $hdrs .= $header["name"].": ".$header["content"]."\n";
197 }
198 }
199 if (count($this->attachments)) {
269bd147 200 // create random boundary, 20 chars, always beginning with KaiRo ;-)
201 $boundary = 'KaiRo';
202 for ($i=1; $i<=15; $i++) {
203 $r=rand(0,61);
204 if ($r<10) { $boundary .= chr($r+48); }
205 elseif ($r<36) { $boundary .= chr($r+55); }
206 elseif ($r<62) { $boundary .= chr($r+61); }
207 }
0c81b5b3 208 $hdrs .= "Content-Type: multipart/mixed; boundary=\"$boundary\";\n";
209 $hdrs .= "Content-Transfer-Encoding: 7bit\n";
210 $mtxt .= "This part of the E-mail should never be seen. If\n";
211 $mtxt .= "you are reading this, consider upgrading your e-mail\n";
212 $mtxt .= "client to a MIME-compatible client.\n";
269bd147 213 $mtxt .= "\n--$boundary\n";
0c81b5b3 214 if (ereg("text/.*", $this->content_type)) {
215 $mtxt .= "Content-Type: ".$this->content_type."; charset=\"".$this->charset."\"\n";
216 }
217 else {
218 $mtxt .= "Content-Type: ".$this->content_type."\n";
219 }
220 $mtxt .= "Content-Transfer-Encoding: 8bit\n\n";
221 }
222 else {
223 if (ereg("text/.*", $this->content_type)) {
224 $hdrs .= "Content-Type: ".$this->content_type."; charset=\"".$this->charset."\"\n";
225 }
226 else {
227 $hdrs .= "Content-Type: ".$this->content_type."\n";
228 }
229 $hdrs .= "Content-Transfer-Encoding: 8bit\n";
230 }
231 $mtxt .= stripslashes($this->mailtext);
232 if (count($this->attachments)) {
233 foreach ($this->attachments as $attach) {
269bd147 234 $mtxt .= "\n--$boundary\n";
0c81b5b3 235 $mtxt .= "Content-Type: ".$attach["type"]."; name=\"".$attach["name"]."\";\n";
236 $mtxt .= "Content-Transfer-Encoding: base64\n";
237 $mtxt .= "Content-Disposition: attachment\n\n";
238 $mtxt .= rtrim(chunk_split(base64_encode($attach["content"]), 76)); ;
239 $mtxt .= "\n";
240 }
241 $mtext .= "--$boundary--\n";
242 }
243
244 if (strlen($this->debug_toSingleAddress)) {
245 $hdrs .= "X-Real-To: $recpt\n";
246 $recpt = $this->debug_toSingleAddress;
247 }
248
249 //print("Subject: ".$util->htmlify($subj)."<br>\n");
250 //print("To: ".$util->htmlify($recpt)."<br>\n");
251 //print(nl2br($util->htmlify($hdrs)));
252 //print(nl2br($util->htmlify($mtxt)));
253 mail($recpt, $subj, $mtxt, $hdrs);
254 }
255
256 function mimeencode($fieldtext) {
257 $mText = imap_8bit($fieldtext);
258 if ($mText != $fieldtext) {
259 $trans = array("_" => "=5F", " " => "_", "?" => "=3F");
260 $mText = strtr($mText, $trans);
261 $mText = "=?ISO-8859-15?Q?".$mText."?=";
262 }
263 return $mText;
264 }
265
266
267}
268?>