0c485fe3dcbf2b9a81fab4fa6ca51bbbee1bf4a7
[php-utility-classes.git] / include / classes / email.php-class
1 <?php
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-2006
16  * the Initial Developer. All Rights Reserved.
17  *
18  * Contributor(s): Robert Kaiser <kairo@kairo.at>
19  *
20  * ***** END LICENSE BLOCK ***** */
21
22 class email {
23   // email PHP class
24   // class/object for creating a new mail and send it
25   //
26   // function __construct()
27   //   CONSTRUCTOR
28   //
29   // private $debug_toSingleAddress
30   //   address to send mail to in debug mode
31   //
32   // private $subject
33   //   the mail's subject line
34   //
35   // private $sender
36   //   the mail's sender (array; fields see recipients)
37   //
38   // private $replyto
39   //   Reply-to address (array; fields see recipients)
40   //
41   // private $recipients
42   //   array of recipients (To: line)
43   //   fields: name - real name
44   //           mail - email address
45   //
46   // private $cc
47   //   array of CC recipients (fields like recipients)
48   //
49   // private $bcc
50   //   array of BCC recipients (fields like recipients)
51   //
52   // private $headers
53   //   array containing all additional headers
54   //   fields: name - headers name
55   //           content - header content
56   //
57   // private $content_type
58   //   the mail's content type (MIME-type) [default: text/plain]
59   //
60   // private $charset
61   //   the mail's charset [default: iso-8859-15]
62   //
63   // private $mailtext
64   //   the main mail body
65   //
66   // private $attachments
67   //   array containing all attachments
68   //   fields: name - attachment name
69   //           content - attachment content
70   //           type - MIME type of that attachment
71   //
72   // public function setDebugAddress($debug_email)
73   //   debug mode: send only to this address
74   //
75   // public function setSubject($newsubject)
76   //   set subject of mail
77   //
78   // public function setSender($email, [$name])
79   //   set sender of mail
80   //
81   // public function setReplyTo($email, [$name])
82   //   set reply-to address
83   //
84   // public function addRecipient($email, [$name])
85   //   add a recipient to the mail
86   //
87   // public function addCC($email, [$name])
88   //   add a CC recipient to the mail
89   //
90   // public function addBCC($email, [$name])
91   //   add a BCC recipient to the mail
92   //
93   // public function addHeader($hname, [$hcontent])
94   //   add a header to the mail
95   //
96   // public function addHeaderAddress($hname, $email, [$name])
97   //   add an address header to the mail, possibly with both name and mail parts
98   //
99   // public function addMailText($textpart)
100   //   add some text to the mail
101   //
102   // public function addAttachment($aname, $acontent, [$atype])
103   //   add an attachment to the mail, use given file name, content and MIME type (defaults to application/octet-stream)
104   //
105   // public function getAddresses([$addrtype])
106   //   returns an array of all addresses this mail gets sent to
107   //     fields: email, name, addrtype
108   //       addrtype is one of to/cc/bcc
109   //       the $addrtype parameter is a comma-separated list of such types, default: all of them
110   //
111   // public function send()
112   //   really send the mail
113   //
114   // private function mimeencode($fieldtext, [$stringescape])
115   //   helper function:
116   //     encode given field text, ready to be placed into an e-mail MIME header
117   //     if the boolean $stringescape is true, make sure this is sent as a single word in RFC2822 context (e.g. for names)
118   //     see http://www.ietf.org/rfc/rfc2822.txt for the RFC in question
119
120   private $debug_toSingleAddress = '';
121   private $subject;
122   private $sender = array();
123   private $replyto = array();
124   private $recipients = array();
125   private $cc = array();
126   private $bcc = array();
127   private $headers = array();
128   private $content_type = 'text/plain';
129   private $charset = 'iso-8859-15';
130   private $mailtext = '';
131   private $attachments = array();
132
133   function __construct() {
134     // *** constructor ***
135   }
136
137   public function setDebugAddress($debug_email) { $this->debug_toSingleAddress = $debug_email; }
138
139   public function setSubject($newsubject) { $this->subject = $newsubject; }
140
141   public function setSender($email, $name = '') { $this->sender = array('mail' => $email, 'name' => $name); }
142
143   public function setReplyTo($email, $name = '') { $this->replyto = array('mail' => $email, 'name' => $name); }
144
145   public function addRecipient($email, $name = '') {
146     $this->recipients[] = array('mail' => $email, 'name' => $name);
147   }
148
149   public function addCC($email, $name = '') {
150     $this->cc[] = array('mail' => $email, 'name' => $name);
151   }
152
153   public function addBCC($email, $name = '') {
154     $this->bcc[] = array('mail' => $email, 'name' => $name);
155   }
156
157   public function addHeader($hname, $hcontent = '') {
158     $this->headers[] = array('name' => $hname, 'content' => $hcontent);
159   }
160
161   public function addHeaderAddress($hname, $email, $name = '') {
162     if (strlen($name)) { $hcontent = $this->mimeencode($name, true).' <'.$email.'>'; }
163     else { $hcontent = $email; }
164     $this->headers[] = array('name' => $hname, 'content' => $hcontent);
165   }
166
167   public function addMailText($textpart) { $this->mailtext .= $textpart; }
168
169   public function addAttachment($aname, $acontent, $atype = 'application/octet-stream') {
170     $this->attachments[] = array('name' => $aname, 'content' => $acontent, 'type' => $atype);
171   }
172
173   public function getAddresses($addrtype = null) {
174     // returns all addresses this mail gets sent to
175     $addrtype = explode(',', strtolower($addrtype));
176     if (!is_array($addrtype)) { $addrtype = array('to','cc','bcc'); }
177     $mailaddresses = array();
178
179     if (in_array($addrtype, 'to')) {
180       foreach ($this->recipients as $address) {
181         if (strlen(@$address['mail'])) {
182           $mailaddresses[] = array('mail'=>$address['mail'],
183                                    'name'=>strlen($address['name'])?$address['name']:'',
184                                    'addrtype'=>'to');
185         }
186       }
187     }
188     if (in_array($addrtype, 'cc')) {
189       foreach ($this->cc as $address) {
190         if (strlen(@$address['mail'])) {
191           $mailaddresses[] = array('mail'=>$address['mail'],
192                                    'name'=>strlen($address['name'])?$address['name']:'',
193                                    'addrtype'=>'cc');
194         }
195       }
196     }
197     if (in_array($addrtype, 'bcc')) {
198       foreach ($this->bcc as $address) {
199         if (strlen(@$address['mail'])) {
200           $mailaddresses[] = array('mail'=>$address['mail'],
201                                    'name'=>strlen($address['name'])?$address['name']:'',
202                                    'addrtype'=>'bcc');
203         }
204       }
205     }
206   }
207
208   public function send() {
209     global $util;
210     $mtxt = '';
211     $hdrs = 'MIME-Version: 1.0'."\n";
212     $subj = $this->mimeencode($this->subject);
213     if (strlen($this->sender['name'])) {
214       $hdrs .= 'From: '.$this->mimeencode($this->sender['name'], true).' <'.$this->sender['mail'].'>'."\n";
215     }
216     else { $hdrs .= 'From: '.$this->sender['mail']."\n"; }
217     if (count($this->replyto)) {
218       if (strlen($this->replyto['name'])) {
219         $hdrs .= 'Reply-to: '.$this->mimeencode($this->replyto['name'], true).' <'.$this->replyto['mail'].'>'."\n";
220       }
221       else { $hdrs .= 'Reply-to: '.$this->replyto['mail']."\n"; }
222     }
223     if (count($this->recipients)) {
224       $recpt = '';
225       foreach ($this->recipients as $address) {
226         if (strlen(@$address['mail'])) {
227           if (strlen($address['name'])) { $recpt .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
228           else { $recpt .= $address['mail'].','; }
229         }
230       }
231       $recpt = preg_replace('/,$/', '', $recpt);
232     }
233     if (!strlen($recpt)) {
234       return null;
235     }
236     if (count($this->cc)) {
237       $adrs = '';
238       foreach ($this->cc as $address) {
239         if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
240         else { $adrs .= $address['mail'].','; }
241       }
242       $adrs = preg_replace('/,$/', '', $adrs);
243       $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Cc: '.$adrs."\n";
244     }
245     if (count($this->bcc)) {
246       $adrs = '';
247       foreach ($this->bcc as $address) {
248         if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
249         else { $adrs .= $address['mail'].','; }
250       }
251       $adrs = preg_replace('/,$/', '', $adrs);
252       $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Bcc: '.$adrs."\n";
253     }
254     if (count($this->headers)) {
255       foreach ($this->headers as $header) {
256         $hdrs .= $header['name'].': '.$header['content']."\n";
257       }
258     }
259     if (count($this->attachments)) {
260       // create random boundary, 20 chars, always beginning with KaiRo ;-)
261       $boundary = 'KaiRo';
262       for ($i = 1; $i <= 15; $i++)      {
263         $r = rand(0, 61);
264         if ($r < 10) { $boundary .= chr($r + 48); }
265         elseif ($r < 36) { $boundary .= chr($r + 55); }
266         elseif ($r < 62) { $boundary .= chr($r + 61); }
267       }
268       $hdrs .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'";'."\n";
269       $hdrs .= 'Content-Transfer-Encoding: 7bit'."\n";
270       $mtxt .= 'This part of the E-mail should never be seen. If'."\n";
271       $mtxt .= 'you are reading this, consider upgrading your e-mail'."\n";
272       $mtxt .= 'client to a MIME-compatible client.'."\n";
273       $mtxt .= "\n".'--'.$boundary."\n";
274       if (preg_match('|^text/|', $this->content_type)) {
275         $mtxt .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n";
276       }
277       else {
278         $mtxt .= 'Content-Type: '.$this->content_type."\n";
279       }
280       $mtxt .= 'Content-Transfer-Encoding: 8bit'."\n\n";
281     }
282     else {
283       if (preg_match('|^text/|', $this->content_type)) {
284         $hdrs .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n";
285       }
286       else {
287         $hdrs .= 'Content-Type: '.$this->content_type."\n";
288       }
289       $hdrs .= 'Content-Transfer-Encoding: 8bit'."\n";
290     }
291     $mtxt .= stripslashes($this->mailtext);
292     if (count($this->attachments)) {
293       foreach ($this->attachments as $attach) {
294         $mtxt .= "\n".'--'.$boundary."\n";
295         $mtxt .= 'Content-Type: '.$attach['type'].'; name="'.$attach['name'].'";'."\n";
296         if (preg_match('/^(text|message)\//', $attach['type'])) {
297           $mtxt .= 'Content-Transfer-Encoding: 8bit'."\n";
298           $mtxt .= 'Content-Disposition: attachment'."\n\n";
299           $mtxt .= $attach['content'];
300           $mtxt .= "\n";
301         }
302         else {
303           $mtxt .= 'Content-Transfer-Encoding: base64'."\n";
304           $mtxt .= 'Content-Disposition: attachment'."\n\n";
305           $mtxt .= rtrim(chunk_split(base64_encode($attach['content']), 76)); ;
306           $mtxt .= "\n";
307         }
308       }
309       $mtxt .= '--'.$boundary.'--'."\n";
310     }
311
312     if (strlen($this->debug_toSingleAddress)) {
313       $hdrs .= 'X-Real-To: '.$recpt."\n";
314       $recpt = $this->debug_toSingleAddress;
315     }
316
317     //print('Subject: '.$util->htmlify($subj).'<br>'."\n");
318     //print('To: '.$util->htmlify($recpt).'<br>'."\n");
319     //print(nl2br($util->htmlify($hdrs)));
320     //print(nl2br($util->htmlify($mtxt)));
321     return mail($recpt, $subj, $mtxt, $hdrs);
322   }
323
324   private function mimeencode($fieldtext, $stringescape = false) {
325     $mText = imap_8bit($fieldtext);
326     $is_qpformat = ($mText != $fieldtext);
327     if ($stringescape && preg_match('/[^\w !#$%&\'*+\/=?^`{|}~-]/', $mText)) {
328       // if needed, make this a quoted-string instead of an atom (to speak in RFC2822 language)
329       $mText = '"'.strtr($mText, array('"' => '\"', '\\' => '\\\\')).'"';
330     }
331     if ($is_qpformat) {
332       $mText = strtr($mText, array('_' => '=5F', ' ' => '_', '?' => '=3F'));
333       $mText = '=?'.strtoupper($this->charset).'?Q?'.$mText.'?=';
334     }
335   return $mText;
336   }
337
338
339 }
340 ?>