4 // class/object for creating a new mail and send it
9 // var $debug_toSingleAddress
10 // address to send mail to in debug mode
13 // the mail's subject line
16 // the mail's sender (array; fields see recipients)
19 // Reply-to address (array; fields see recipients)
22 // array of recipients (To: line)
23 // fields: name - real name
24 // mail - email address
27 // array of CC recipients (fields like recipients)
30 // array of BCC recipients (fields like recipients)
33 // array containing all additional headers
34 // fields: name - headers name
35 // content - header content
38 // the mail's content type (MIME-type) [default: text/plain]
41 // the mail's charset [default: iso-8859-15]
47 // array containing all attachments
48 // fields: name - attachment name
49 // content - attachment content
50 // type - MIME type of that attachment
52 // function setDebugAddress($debug_email)
53 // debug mode: send only to this address
55 // function setSubject($newsubject)
56 // set subject of mail
58 // function setSender($email, [$name])
61 // function setReplyTo($email, [$name])
62 // set reply-to address
64 // function addRecipient($email, [$name])
65 // add a recipient to the mail
67 // function addCC($email, [$name])
68 // add a CC recipient to the mail
70 // function addBCC($email, [$name])
71 // add a BCC recipient to the mail
73 // function addHeader($hname, [$hcontent])
74 // add a header to the mail
76 // function addMailText($textpart)
77 // add some text to the mail
80 // really send the mail
82 // function mimeencode($fieldtext)
84 // encode given field text, ready to be placed into an e-mail MIME header
86 var $debug_toSingleAddress = "";
88 var $sender = array();
89 var $replyto = array();
90 var $recipients = array();
93 var $headers = array();
94 var $content_type = "text/plain";
95 var $charset = "iso-8859-15";
97 var $attachments = array();
100 // *** constructor ***
103 function setDebugAddress($debug_email) { $this->debug_toSingleAddress = $debug_email; }
105 function setSubject($newsubject) { $this->subject = $newsubject; }
107 function setSender($email, $name = "") { $this->sender = array("mail" => $email, "name" => $name); }
109 function setReplyTo($email, $name = "") { $this->replyto = array("mail" => $email, "name" => $name); }
111 function addRecipient($email, $name = "") {
112 $this->recipients[] = array("mail" => $email, "name" => $name);
115 function addCC($email, $name = "") {
116 $this->cc[] = array("mail" => $email, "name" => $name);
119 function addBCC($email, $name = "") {
120 $this->bcc[] = array("mail" => $email, "name" => $name);
123 function addHeader($hname, $hcontent = "") {
124 $this->headers[] = array("name" => $hname, "content" => $hcontent);
127 function addMailText($textpart) { $this->mailtext .= $textpart; }
129 function addAttachment($aname, $acontent, $atype = "application/octet-stream") {
130 $this->attachments[] = array("name" => $aname, "content" => $acontent, "type" => $atype);
136 $hdrs = "MIME-Version: 1.0\n";
137 $subj = $this->mimeencode($this->subject);
138 if (strlen($this->sender["name"])) {
139 $hdrs .= "From: ".$this->mimeencode($this->sender["name"])." <".$this->sender["mail"].">\n";
141 else { $hdrs .= "From: ".$this->sender["mail"]."\n"; }
142 if (count($this->replyto)) {
143 if (strlen($this->replyto["name"])) {
144 $hdrs .= "Reply-to: ".$this->mimeencode($this->replyto["name"])." <".$this->replyto["mail"].">\n";
146 else { $hdrs .= "Reply-to: ".$this->replyto["mail"]."\n"; }
148 if (count($this->recipients)) {
150 foreach ($this->recipients as $address) {
151 if (strlen($address["name"])) { $recpt .= $this->mimeencode($address["name"])." <".$address["mail"].">,"; }
152 else { $recpt .= $address["mail"].","; }
154 $recpt = ereg_replace(",$", "", $recpt);
156 if (count($this->cc)) {
158 foreach ($this->cc as $address) {
159 if (strlen($address["name"])) { $adrs .= $this->mimeencode($address["name"])." <".$address["mail"].">,"; }
160 else { $adrs .= $address["mail"].","; }
162 $adrs = ereg_replace(",$", "", $adrs);
163 $hdrs .= (strlen($this->debug_toSingleAddress)?"X-Real-":"")."Cc: $adrs\n";
165 if (count($this->bcc)) {
167 foreach ($this->bcc as $address) {
168 if (strlen($address["name"])) { $adrs .= $this->mimeencode($address["name"])." <".$address["mail"].">,"; }
169 else { $adrs .= $address["mail"].","; }
171 $adrs = ereg_replace(",$", "", $adrs);
172 $hdrs .= (strlen($this->debug_toSingleAddress)?"X-Real-":"")."Bcc: $adrs\n";
174 if (count($this->headers)) {
175 foreach ($this->headers as $header) {
176 $hdrs .= $header["name"].": ".$header["content"]."\n";
179 if (count($this->attachments)) {
180 // create random boundary, 20 chars, always beginning with KaiRo ;-)
182 for ($i=1; $i<=15; $i++) {
184 if ($r<10) { $boundary .= chr($r+48); }
185 elseif ($r<36) { $boundary .= chr($r+55); }
186 elseif ($r<62) { $boundary .= chr($r+61); }
188 $hdrs .= "Content-Type: multipart/mixed; boundary=\"$boundary\";\n";
189 $hdrs .= "Content-Transfer-Encoding: 7bit\n";
190 $mtxt .= "This part of the E-mail should never be seen. If\n";
191 $mtxt .= "you are reading this, consider upgrading your e-mail\n";
192 $mtxt .= "client to a MIME-compatible client.\n";
193 $mtxt .= "\n--$boundary\n";
194 if (ereg("text/.*", $this->content_type)) {
195 $mtxt .= "Content-Type: ".$this->content_type."; charset=\"".$this->charset."\"\n";
198 $mtxt .= "Content-Type: ".$this->content_type."\n";
200 $mtxt .= "Content-Transfer-Encoding: 8bit\n\n";
203 if (ereg("text/.*", $this->content_type)) {
204 $hdrs .= "Content-Type: ".$this->content_type."; charset=\"".$this->charset."\"\n";
207 $hdrs .= "Content-Type: ".$this->content_type."\n";
209 $hdrs .= "Content-Transfer-Encoding: 8bit\n";
211 $mtxt .= stripslashes($this->mailtext);
212 if (count($this->attachments)) {
213 foreach ($this->attachments as $attach) {
214 $mtxt .= "\n--$boundary\n";
215 $mtxt .= "Content-Type: ".$attach["type"]."; name=\"".$attach["name"]."\";\n";
216 $mtxt .= "Content-Transfer-Encoding: base64\n";
217 $mtxt .= "Content-Disposition: attachment\n\n";
218 $mtxt .= rtrim(chunk_split(base64_encode($attach["content"]), 76)); ;
221 $mtext .= "--$boundary--\n";
224 if (strlen($this->debug_toSingleAddress)) {
225 $hdrs .= "X-Real-To: $recpt\n";
226 $recpt = $this->debug_toSingleAddress;
229 //print("Subject: ".$util->htmlify($subj)."<br>\n");
230 //print("To: ".$util->htmlify($recpt)."<br>\n");
231 //print(nl2br($util->htmlify($hdrs)));
232 //print(nl2br($util->htmlify($mtxt)));
233 mail($recpt, $subj, $mtxt, $hdrs);
236 function mimeencode($fieldtext) {
237 $mText = imap_8bit($fieldtext);
238 if ($mText != $fieldtext) {
239 $trans = array("_" => "=5F", " " => "_", "?" => "=3F");
240 $mText = strtr($mText, $trans);
241 $mText = "=?ISO-8859-15?Q?".$mText."?=";