add email class for sending mail easily
[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       $boundary = "KaiRoJVPSteyr1030713";
181       $hdrs .= "Content-Type: multipart/mixed; boundary=\"$boundary\";\n";
182       $hdrs .= "Content-Transfer-Encoding: 7bit\n";
183       $mtxt .= "This part of the E-mail should never be seen. If\n";
184       $mtxt .= "you are reading this, consider upgrading your e-mail\n";
185       $mtxt .= "client to a MIME-compatible client.\n";
186       $mtxt .= "--$boundary\n";
187       if (ereg("text/.*", $this->content_type)) {
188         $mtxt .= "Content-Type: ".$this->content_type."; charset=\"".$this->charset."\"\n";
189       }
190       else {
191         $mtxt .= "Content-Type: ".$this->content_type."\n";
192       }
193       $mtxt .= "Content-Transfer-Encoding: 8bit\n\n";
194     }
195     else {
196       if (ereg("text/.*", $this->content_type)) {
197         $hdrs .= "Content-Type: ".$this->content_type."; charset=\"".$this->charset."\"\n";
198       }
199       else {
200         $hdrs .= "Content-Type: ".$this->content_type."\n";
201       }
202       $hdrs .= "Content-Transfer-Encoding: 8bit\n";
203     }
204     $mtxt .= stripslashes($this->mailtext);
205     if (count($this->attachments)) {
206       foreach ($this->attachments as $attach) {
207         $mtxt .= "--$boundary\n";
208         $mtxt .= "Content-Type: ".$attach["type"]."; name=\"".$attach["name"]."\";\n";
209         $mtxt .= "Content-Transfer-Encoding: base64\n";
210         $mtxt .= "Content-Disposition: attachment\n\n";
211         $mtxt .= rtrim(chunk_split(base64_encode($attach["content"]), 76)); ;
212         $mtxt .= "\n";
213       }
214       $mtext .= "--$boundary--\n";
215     }
216
217     if (strlen($this->debug_toSingleAddress)) {
218       $hdrs .= "X-Real-To: $recpt\n";
219       $recpt = $this->debug_toSingleAddress;
220     }
221
222     //print("Subject: ".$util->htmlify($subj)."<br>\n");
223     //print("To: ".$util->htmlify($recpt)."<br>\n");
224     //print(nl2br($util->htmlify($hdrs)));
225     //print(nl2br($util->htmlify($mtxt)));
226     mail($recpt, $subj, $mtxt, $hdrs);
227   }
228
229   function mimeencode($fieldtext) {
230     $mText = imap_8bit($fieldtext);
231     if ($mText != $fieldtext) {
232       $trans = array("_" => "=5F", " " => "_", "?" => "=3F");
233       $mText = strtr($mText, $trans);
234       $mText = "=?ISO-8859-15?Q?".$mText."?=";
235     }
236   return $mText;
237   }
238
239
240 }
241 ?>