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