4f35aa2054ce4d299892ac61aa22693f7933dd1e
[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     if (function_exists('imap_8bit')) {
319       $mText = imap_8bit($fieldtext);
320     }
321     else {
322       $mText = quoted_printable_encode($fieldtext);
323     }
324     $is_qpformat = ($mText != $fieldtext);
325     if ($stringescape && preg_match('/[^\w !#$%&\'*+\/=?^`{|}~-]/', $mText)) {
326       // if needed, make this a quoted-string instead of an atom (to speak in RFC2822 language)
327       $mText = '"'.strtr($mText, array('"' => '\"', '\\' => '\\\\')).'"';
328     }
329     if ($is_qpformat) {
330       $mText = strtr($mText, array('_' => '=5F', ' ' => '_', '?' => '=3F'));
331       $mText = '=?'.strtoupper($this->charset).'?Q?'.$mText.'?=';
332     }
333   return $mText;
334   }
335
336   private function quoted_printable_encode($sText, $bEmulate_imap_8bit=true) {
337     /* by ...deed.ztinmehc-ut.zrh@umuumu@hrz.tu-chemnitz.deed...
338        from https://secure.php.net/manual/en/function.imap-8bit.php#61216
339
340         I use the following function instead of imap_8bit
341         when using PHP without the IMAP module,
342         which is based on code found in
343         http://www.php.net/quoted_printable_decode,
344         and giving (supposedly) exactly the same results as imap_8bit,
345         (tested on thousands of random strings containing lots
346         of spaces, tabs, crlf, lfcr, lf, cr and so on,
347         no counterexample found SO FAR:)
348
349         AND you can force a trailing space to be encoded,
350         as opposed to what imap_8bit does,
351         which I consider is a violation of RFC2045,
352         (see http://bugs.php.net/bug.php?id=35290)
353         by commenting that one central line.
354     */
355     // split text into lines
356     $aLines=explode(chr(13).chr(10),$sText);
357
358     for ($i=0;$i<count($aLines);$i++) {
359       $sLine =& $aLines[$i];
360       if (strlen($sLine)===0) continue; // do nothing, if empty
361
362       $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
363
364       // imap_8bit encodes x09 everywhere, not only at lineends,
365       // for EBCDIC safeness encode !"#$@[\]^`{|}~,
366       // for complete safeness encode every character :)
367       if ($bEmulate_imap_8bit)
368         $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e';
369
370       $sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;';
371       $sLine = preg_replace( $sRegExp, $sReplmt, $sLine );
372
373       // encode x09,x20 at lineends
374       {
375         $iLength = strlen($sLine);
376         $iLastChar = ord($sLine{$iLength-1});
377
378         //              !!!!!!!!
379         // imap_8_bit does not encode x20 at the very end of a text,
380         // here is, where I don't agree with imap_8_bit,
381         // please correct me, if I'm wrong,
382         // or comment next line for RFC2045 conformance, if you like
383         if (!($bEmulate_imap_8bit && ($i==count($aLines)-1)))
384
385         if (($iLastChar==0x09)||($iLastChar==0x20)) {
386           $sLine{$iLength-1}='=';
387           $sLine .= ($iLastChar==0x09)?'09':'20';
388         }
389       }    // imap_8bit encodes x20 before chr(13), too
390       // although IMHO not requested by RFC2045, why not do it safer :)
391       // and why not encode any x20 around chr(10) or chr(13)
392       if ($bEmulate_imap_8bit) {
393         $sLine=str_replace(' =0D','=20=0D',$sLine);
394         //$sLine=str_replace(' =0A','=20=0A',$sLine);
395         //$sLine=str_replace('=0D ','=0D=20',$sLine);
396         //$sLine=str_replace('=0A ','=0A=20',$sLine);
397       }
398
399       // finally split into softlines no longer than 76 chars,
400       // for even more safeness one could encode x09,x20
401       // at the very first character of the line
402       // and after soft linebreaks, as well,
403       // but this wouldn't be caught by such an easy RegExp
404       preg_match_all( '/.{1,73}([^=]{0,2})?/', $sLine, $aMatch );
405       $sLine = implode( '=' . chr(13).chr(10), $aMatch[0] ); // add soft crlf's
406     }
407
408     // join lines into text
409     return implode(chr(13).chr(10),$aLines);
410   }
411 }
412 ?>