add bot detection
[php-utility-classes.git] / include / classes / email.php-class
1 <?php
2 class email {
3   // email PHP class
4   // class/object for creating a new mail and send it
5   //
6   // function email()
7   //   CONSTRUCTOR
8   //
9   // var $debug_toSingleAddress
10   //   address to send mail to in debug mode
11   //
12   // var $subject
13   //   the mail's subject line
14   //
15   // var $sender
16   //   the mail's sender (array; fields see recipients)
17   //
18   // var $replyto
19   //   Reply-to address (array; fields see recipients)
20   //
21   // var $recipients
22   //   array of recipients (To: line)
23   //   fields: name - real name
24   //           mail - email address
25   //
26   // var $cc
27   //   array of CC recipients (fields like recipients)
28   //
29   // var $bcc
30   //   array of BCC recipients (fields like recipients)
31   //
32   // var $headers
33   //   array containing all additional headers
34   //   fields: name - headers name
35   //           content - header content
36   //
37   // var $content_type
38   //   the mail's content type (MIME-type) [default: text/plain]
39   //
40   // var $charset
41   //   the mail's charset [default: iso-8859-15]
42   //
43   // var $mailtext
44   //   the main mail body
45   //
46   // var $attachments
47   //   array containing all attachments
48   //   fields: name - attachment name
49   //           content - attachment content
50   //           type - MIME type of that attachment
51   //
52   // function setDebugAddress($debug_email)
53   //   debug mode: send only to this address
54   //
55   // function setSubject($newsubject)
56   //   set subject of mail
57   //
58   // function setSender($email, [$name])
59   //   set sender of mail
60   //
61   // function setReplyTo($email, [$name])
62   //   set reply-to address
63   //
64   // function addRecipient($email, [$name])
65   //   add a recipient to the mail
66   //
67   // function addCC($email, [$name])
68   //   add a CC recipient to the mail
69   //
70   // function addBCC($email, [$name])
71   //   add a BCC recipient to the mail
72   //
73   // function addHeader($hname, [$hcontent])
74   //   add a header to the mail
75   //
76   // function addMailText($textpart)
77   //   add some text to the mail
78   //
79   // function send()
80   //   really send the mail
81   //
82   // function mimeencode($fieldtext)
83   //   helper function:
84   //   encode given field text, ready to be placed into an e-mail MIME header
85
86   var $debug_toSingleAddress = "";
87   var $subject;
88   var $sender = array();
89   var $replyto = array();
90   var $recipients = array();
91   var $cc = array();
92   var $bcc = array();
93   var $headers = array();
94   var $content_type = "text/plain";
95   var $charset = "iso-8859-15";
96   var $mailtext = "";
97   var $attachments = array();
98
99   function email() {
100     // *** constructor ***
101   }
102
103   function setDebugAddress($debug_email) { $this->debug_toSingleAddress = $debug_email; }
104
105   function setSubject($newsubject) { $this->subject = $newsubject; }
106
107   function setSender($email, $name = "") { $this->sender = array("mail" => $email, "name" => $name); }
108
109   function setReplyTo($email, $name = "") { $this->replyto = array("mail" => $email, "name" => $name); }
110
111   function addRecipient($email, $name = "") {
112     $this->recipients[] = array("mail" => $email, "name" => $name);
113   }
114
115   function addCC($email, $name = "") {
116     $this->cc[] = array("mail" => $email, "name" => $name);
117   }
118
119   function addBCC($email, $name = "") {
120     $this->bcc[] = array("mail" => $email, "name" => $name);
121   }
122
123   function addHeader($hname, $hcontent = "") {
124     $this->headers[] = array("name" => $hname, "content" => $hcontent);
125   }
126
127   function addMailText($textpart) { $this->mailtext .= $textpart; }
128
129   function addAttachment($aname, $acontent, $atype = "application/octet-stream") {
130     $this->attachments[] = array("name" => $aname, "content" => $acontent, "type" => $atype);
131   }
132
133   function send() {
134     global $util;
135     $mtxt = "";
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";
140     }
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";
145       }
146       else { $hdrs .= "Reply-to: ".$this->replyto["mail"]."\n"; }
147     }
148     if (count($this->recipients)) {
149       $recpt = "";
150       foreach ($this->recipients as $address) {
151         if (strlen($address["name"])) { $recpt .= $this->mimeencode($address["name"])." <".$address["mail"].">,"; }
152         else { $recpt .= $address["mail"].","; }
153       }
154       $recpt = ereg_replace(",$", "", $recpt);
155     }
156     if (count($this->cc)) {
157       $adrs = "";
158       foreach ($this->cc as $address) {
159         if (strlen($address["name"])) { $adrs .= $this->mimeencode($address["name"])." <".$address["mail"].">,"; }
160         else { $adrs .= $address["mail"].","; }
161       }
162       $adrs = ereg_replace(",$", "", $adrs);
163       $hdrs .= (strlen($this->debug_toSingleAddress)?"X-Real-":"")."Cc: $adrs\n";
164     }
165     if (count($this->bcc)) {
166       $adrs = "";
167       foreach ($this->bcc as $address) {
168         if (strlen($address["name"])) { $adrs .= $this->mimeencode($address["name"])." <".$address["mail"].">,"; }
169         else { $adrs .= $address["mail"].","; }
170       }
171       $adrs = ereg_replace(",$", "", $adrs);
172       $hdrs .= (strlen($this->debug_toSingleAddress)?"X-Real-":"")."Bcc: $adrs\n";
173     }
174     if (count($this->headers)) {
175       foreach ($this->headers as $header) {
176         $hdrs .= $header["name"].": ".$header["content"]."\n";
177       }
178     }
179     if (count($this->attachments)) {
180       // create random boundary, 20 chars, always beginning with KaiRo ;-)
181       $boundary = 'KaiRo';
182       for ($i=1; $i<=15; $i++)  {
183         $r=rand(0,61);
184         if ($r<10) { $boundary .= chr($r+48); }
185         elseif ($r<36) { $boundary .= chr($r+55); }
186         elseif ($r<62) { $boundary .= chr($r+61); }
187       }
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";
196       }
197       else {
198         $mtxt .= "Content-Type: ".$this->content_type."\n";
199       }
200       $mtxt .= "Content-Transfer-Encoding: 8bit\n\n";
201     }
202     else {
203       if (ereg("text/.*", $this->content_type)) {
204         $hdrs .= "Content-Type: ".$this->content_type."; charset=\"".$this->charset."\"\n";
205       }
206       else {
207         $hdrs .= "Content-Type: ".$this->content_type."\n";
208       }
209       $hdrs .= "Content-Transfer-Encoding: 8bit\n";
210     }
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)); ;
219         $mtxt .= "\n";
220       }
221       $mtext .= "--$boundary--\n";
222     }
223
224     if (strlen($this->debug_toSingleAddress)) {
225       $hdrs .= "X-Real-To: $recpt\n";
226       $recpt = $this->debug_toSingleAddress;
227     }
228
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);
234   }
235
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."?=";
242     }
243   return $mText;
244   }
245
246
247 }
248 ?>