Added charset selection for email content.
[fa-stable.git] / reporting / includes / class.mail.inc
1 <?php
2 /*
3     Name:           eMail
4     Description:    Simple sending eMail in text and HTML with CC, BCC and attachment
5     Version:        1.0
6     last modified:  2004-05-14
7
8     Autor:          Daniel Käfer
9     Homepage:       http://www.danielkaefer.de
10
11     Leave this header in this file!
12 */
13
14 class email
15 {
16     var $to = array();
17     var $cc = array();
18     var $bcc = array();
19     var $attachment = array();
20     var $boundary = "";
21     var $header = "";
22     var $subject = "";
23     var $body = "";
24         var $charset = 'ISO-8859-1';
25         
26     function email($name, $mail)
27     {
28         $this->boundary = md5(uniqid(time()));
29         $this->header .= "From: $name <$mail>\n";
30     }
31
32     function to($mail)
33     {
34         $this->to[] = $mail;
35     }
36
37     function cc($mail)
38     {
39         $this->cc[] = $mail;
40     }
41
42     function bcc($mail)
43     {
44         $this->bcc[] = $mail;
45     }
46
47     function attachment($file)
48     {
49                 $this->attachment[] = $file;
50     }
51
52     function subject($subject)
53     {
54         $this->subject = $subject;
55     }
56
57     function text($text)
58     {
59             $this->body = "Content-Type: text/plain; charset={$this->charset}\n";
60             $this->body .= "Content-Transfer-Encoding: 8bit\n\n";
61             $this->body .= $text."\n";
62     }
63
64     function html($html)
65     {
66             $this->body = "Content-Type: text/html; charset={$this->charset}\n";
67             $this->body .= "Content-Transfer-Encoding: quoted-printable\n\n";
68             $this->body .= "<html><body>\n".$html."\n</body></html>\n";
69     }
70
71         function mime_type($filename)
72         {
73                 $file = basename($filename, '.zip');
74                 if ($filename == $file . '.zip') return 'application/x-zip-compressed';
75                 $file = basename($filename, '.pdf');
76                 if ($filename == $file . '.pdf') return 'application/pdf';
77                 $file = basename($filename, '.csv');
78                 if ($filename == $file . '.csv') return 'application/vnd.ms-excel';
79                 $file = basename($filename, '.tar');
80                 if ($filename == $file . '.tar') return 'application/x-tar';
81                 $file = basename($filename, '.tar.gz');
82                 if ($filename == $file . '.tar.gz') return 'application/x-tar-gz';
83                 $file = basename($filename, '.tgz');
84                 if ($filename == $file . '.tgz') return 'application/x-tar-gz';
85                 $file = basename($filename, '.gz');
86                 if ($filename == $file . '.gz') return 'application/x-gzip';
87                 return 'application/unknown';
88         }
89
90         function send()
91     {
92         // CC Empfänger hinzufügen
93         $max = count($this->cc);
94         if ($max > 0)
95         {
96             $this->header .= "Cc: ".$this->cc[0];
97             for ($i = 1; $i < $max; $i++)
98             {
99                 $this->header .= ", ".$this->cc[$i];
100             }
101             $this->header .= "\n";
102         }
103         // BCC Empfänger hinzufügen
104         $max = count($this->bcc);
105         if ($max > 0)
106         {
107             $this->header .= "Bcc: ".$this->bcc[0];
108             for ($i = 1; $i < $max; $i++)
109             {
110                 $this->header .= ", ".$this->bcc[$i];
111             }
112             $this->header .= "\n";
113         }
114         $this->header .= "MIME-Version: 1.0\n";
115             $this->header .= "Content-Type: multipart/mixed; boundary=$this->boundary\n\n";
116             $this->header .= "This is a multi-part message in MIME format\n";
117         $this->header .= "--$this->boundary\n";
118         $this->header .= $this->body;
119
120         // Attachment hinzufügen
121         $max = count($this->attachment);
122         if ($max > 0)
123         {
124             for ($i = 0; $i < $max; $i++)
125             {
126                 $file = fread(fopen($this->attachment[$i], "r"), filesize($this->attachment[$i]));
127                 $this->header .= "--".$this->boundary."\n";
128                 $this->header .= "Content-Type: " .$this->mime_type(basename($this->attachment[$i])). "; name=".basename($this->attachment[$i])."\n";
129                 $this->header .= "Content-Transfer-Encoding: base64\n";
130                 $this->header .= "Content-Disposition: attachment; filename=".basename($this->attachment[$i])."\n\n";
131                 $this->header .= chunk_split(base64_encode($file))."\n";
132                 $file = "";
133             }
134         }
135         $this->header .= "--".$this->boundary."--\n\n";
136
137         foreach($this->to as $mail)
138         {
139             $ret = mail($mail, $this->subject, "", $this->header);
140         }
141         return $ret;
142     }
143 }
144 ?>