2 /* ***** BEGIN LICENSE BLOCK *****
4 * The contents of this file are subject to Austrian copyright reegulations
5 * ("Urheberrecht"); you may not use this file except in compliance with
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.
11 * The Original Code is KaiRo's E-Mail module.
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.
18 * Contributor(s): Robert Kaiser <kairo@kairo.at>
20 * ***** END LICENSE BLOCK ***** */
24 // class/object for creating a new mail and send it
29 // var $debug_toSingleAddress
30 // address to send mail to in debug mode
33 // the mail's subject line
36 // the mail's sender (array; fields see recipients)
39 // Reply-to address (array; fields see recipients)
42 // array of recipients (To: line)
43 // fields: name - real name
44 // mail - email address
47 // array of CC recipients (fields like recipients)
50 // array of BCC recipients (fields like recipients)
53 // array containing all additional headers
54 // fields: name - headers name
55 // content - header content
58 // the mail's content type (MIME-type) [default: text/plain]
61 // the mail's charset [default: iso-8859-15]
67 // array containing all attachments
68 // fields: name - attachment name
69 // content - attachment content
70 // type - MIME type of that attachment
72 // function setDebugAddress($debug_email)
73 // debug mode: send only to this address
75 // function setSubject($newsubject)
76 // set subject of mail
78 // function setSender($email, [$name])
81 // function setReplyTo($email, [$name])
82 // set reply-to address
84 // function addRecipient($email, [$name])
85 // add a recipient to the mail
87 // function addCC($email, [$name])
88 // add a CC recipient to the mail
90 // function addBCC($email, [$name])
91 // add a BCC recipient to the mail
93 // function addHeader($hname, [$hcontent])
94 // add a header to the mail
96 // function addMailText($textpart)
97 // add some text to the mail
99 // function addAttachment($aname, $acontent, [$atype])
100 // add an attachment to the mail, use given file name, content and MIME type (defaults to application/octet-stream)
103 // really send the mail
105 // function mimeencode($fieldtext)
107 // encode given field text, ready to be placed into an e-mail MIME header
109 var $debug_toSingleAddress = "";
111 var $sender = array();
112 var $replyto = array();
113 var $recipients = array();
116 var $headers = array();
117 var $content_type = "text/plain";
118 var $charset = "iso-8859-15";
120 var $attachments = array();
123 // *** constructor ***
126 function setDebugAddress($debug_email) { $this->debug_toSingleAddress = $debug_email; }
128 function setSubject($newsubject) { $this->subject = $newsubject; }
130 function setSender($email, $name = "") { $this->sender = array("mail" => $email, "name" => $name); }
132 function setReplyTo($email, $name = "") { $this->replyto = array("mail" => $email, "name" => $name); }
134 function addRecipient($email, $name = "") {
135 $this->recipients[] = array("mail" => $email, "name" => $name);
138 function addCC($email, $name = "") {
139 $this->cc[] = array("mail" => $email, "name" => $name);
142 function addBCC($email, $name = "") {
143 $this->bcc[] = array("mail" => $email, "name" => $name);
146 function addHeader($hname, $hcontent = "") {
147 $this->headers[] = array("name" => $hname, "content" => $hcontent);
150 function addMailText($textpart) { $this->mailtext .= $textpart; }
152 function addAttachment($aname, $acontent, $atype = "application/octet-stream") {
153 $this->attachments[] = array("name" => $aname, "content" => $acontent, "type" => $atype);
159 $hdrs = "MIME-Version: 1.0\n";
160 $subj = $this->mimeencode($this->subject);
161 if (strlen($this->sender["name"])) {
162 $hdrs .= "From: ".$this->mimeencode($this->sender["name"])." <".$this->sender["mail"].">\n";
164 else { $hdrs .= "From: ".$this->sender["mail"]."\n"; }
165 if (count($this->replyto)) {
166 if (strlen($this->replyto["name"])) {
167 $hdrs .= "Reply-to: ".$this->mimeencode($this->replyto["name"])." <".$this->replyto["mail"].">\n";
169 else { $hdrs .= "Reply-to: ".$this->replyto["mail"]."\n"; }
171 if (count($this->recipients)) {
173 foreach ($this->recipients as $address) {
174 if (strlen($address["name"])) { $recpt .= $this->mimeencode($address["name"])." <".$address["mail"].">,"; }
175 else { $recpt .= $address["mail"].","; }
177 $recpt = ereg_replace(",$", "", $recpt);
179 if (count($this->cc)) {
181 foreach ($this->cc as $address) {
182 if (strlen($address["name"])) { $adrs .= $this->mimeencode($address["name"])." <".$address["mail"].">,"; }
183 else { $adrs .= $address["mail"].","; }
185 $adrs = ereg_replace(",$", "", $adrs);
186 $hdrs .= (strlen($this->debug_toSingleAddress)?"X-Real-":"")."Cc: $adrs\n";
188 if (count($this->bcc)) {
190 foreach ($this->bcc as $address) {
191 if (strlen($address["name"])) { $adrs .= $this->mimeencode($address["name"])." <".$address["mail"].">,"; }
192 else { $adrs .= $address["mail"].","; }
194 $adrs = ereg_replace(",$", "", $adrs);
195 $hdrs .= (strlen($this->debug_toSingleAddress)?"X-Real-":"")."Bcc: $adrs\n";
197 if (count($this->headers)) {
198 foreach ($this->headers as $header) {
199 $hdrs .= $header["name"].": ".$header["content"]."\n";
202 if (count($this->attachments)) {
203 // create random boundary, 20 chars, always beginning with KaiRo ;-)
205 for ($i=1; $i<=15; $i++) {
207 if ($r<10) { $boundary .= chr($r+48); }
208 elseif ($r<36) { $boundary .= chr($r+55); }
209 elseif ($r<62) { $boundary .= chr($r+61); }
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 (ereg("text/.*", $this->content_type)) {
218 $mtxt .= "Content-Type: ".$this->content_type."; charset=\"".$this->charset."\"\n";
221 $mtxt .= "Content-Type: ".$this->content_type."\n";
223 $mtxt .= "Content-Transfer-Encoding: 8bit\n\n";
226 if (ereg("text/.*", $this->content_type)) {
227 $hdrs .= "Content-Type: ".$this->content_type."; charset=\"".$this->charset."\"\n";
230 $hdrs .= "Content-Type: ".$this->content_type."\n";
232 $hdrs .= "Content-Transfer-Encoding: 8bit\n";
234 $mtxt .= stripslashes($this->mailtext);
235 if (count($this->attachments)) {
236 foreach ($this->attachments as $attach) {
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)); ;
244 $mtext .= "--$boundary--\n";
247 if (strlen($this->debug_toSingleAddress)) {
248 $hdrs .= "X-Real-To: $recpt\n";
249 $recpt = $this->debug_toSingleAddress;
252 //print("Subject: ".$util->htmlify($subj)."<br>\n");
253 //print("To: ".$util->htmlify($recpt)."<br>\n");
254 //print(nl2br($util->htmlify($hdrs)));
255 //print(nl2br($util->htmlify($mtxt)));
256 mail($recpt, $subj, $mtxt, $hdrs);
259 function mimeencode($fieldtext) {
260 $mText = imap_8bit($fieldtext);
261 if ($mText != $fieldtext) {
262 $trans = array("_" => "=5F", " " => "_", "?" => "=3F");
263 $mText = strtr($mText, $trans);
264 $mText = "=?ISO-8859-15?Q?".$mText."?=";