Improved email sending of documents. With help of Tom Moulton
[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
25     function email($name, $mail)
26     {
27         $this->boundary = md5(uniqid(time()));
28         $this->header .= "From: $name <$mail>\n";
29     }
30
31     function to($mail)
32     {
33         $this->to[] = $mail;
34     }
35
36     function cc($mail)
37     {
38         $this->cc[] = $mail;
39     }
40
41     function bcc($mail)
42     {
43         $this->bcc[] = $mail;
44     }
45
46     function attachment($file)
47     {
48                 $this->attachment[] = $file;
49     }
50
51     function subject($subject)
52     {
53         $this->subject = $subject;
54     }
55
56     function text($text)
57     {
58             $this->body = "Content-Type: text/plain; charset=ISO-8859-1\n";
59             $this->body .= "Content-Transfer-Encoding: 8bit\n\n";
60             $this->body .= $text."\n";
61     }
62
63     function html($html)
64     {
65             $this->body = "Content-Type: text/html; charset=ISO-8859-1\n";
66             $this->body .= "Content-Transfer-Encoding: quoted-printable\n\n";
67             $this->body .= "<html><body>\n".$html."\n</body></html>\n";
68     }
69
70         function mime_type($filename)
71         {
72                 $file = basename($filename, '.zip');
73                 if ($filename == $file . '.zip') return 'application/x-zip-compressed';
74                 $file = basename($filename, '.pdf');
75                 if ($filename == $file . '.pdf') return 'application/pdf';
76                 $file = basename($filename, '.csv');
77                 if ($filename == $file . '.csv') return 'application/vnd.ms-excel';
78                 $file = basename($filename, '.tar');
79                 if ($filename == $file . '.tar') return 'application/x-tar';
80                 $file = basename($filename, '.tar.gz');
81                 if ($filename == $file . '.tar.gz') return 'application/x-tar-gz';
82                 $file = basename($filename, '.tgz');
83                 if ($filename == $file . '.tgz') return 'application/x-tar-gz';
84                 $file = basename($filename, '.gz');
85                 if ($filename == $file . '.gz') return 'application/x-gzip';
86                 return 'application/unknown';
87         }
88
89         function send()
90     {
91         // CC Empfänger hinzufügen
92         $max = count($this->cc);
93         if ($max > 0)
94         {
95             $this->header .= "Cc: ".$this->cc[0];
96             for ($i = 1; $i < $max; $i++)
97             {
98                 $this->header .= ", ".$this->cc[$i];
99             }
100             $this->header .= "\n";
101         }
102         // BCC Empfänger hinzufügen
103         $max = count($this->bcc);
104         if ($max > 0)
105         {
106             $this->header .= "Bcc: ".$this->bcc[0];
107             for ($i = 1; $i < $max; $i++)
108             {
109                 $this->header .= ", ".$this->bcc[$i];
110             }
111             $this->header .= "\n";
112         }
113         $this->header .= "MIME-Version: 1.0\n";
114             $this->header .= "Content-Type: multipart/mixed; boundary=$this->boundary\n\n";
115             $this->header .= "This is a multi-part message in MIME format\n";
116         $this->header .= "--$this->boundary\n";
117         $this->header .= $this->body;
118
119         // Attachment hinzufügen
120         $max = count($this->attachment);
121         if ($max > 0)
122         {
123             for ($i = 0; $i < $max; $i++)
124             {
125                 $file = fread(fopen($this->attachment[$i], "r"), filesize($this->attachment[$i]));
126                 $this->header .= "--".$this->boundary."\n";
127                 $this->header .= "Content-Type: " .$this->mime_type(basename($this->attachment[$i])). "; name=".basename($this->attachment[$i])."\n";
128                 $this->header .= "Content-Transfer-Encoding: base64\n";
129                 $this->header .= "Content-Disposition: attachment; filename=".basename($this->attachment[$i])."\n\n";
130                 $this->header .= chunk_split(base64_encode($file))."\n";
131                 $file = "";
132             }
133         }
134         $this->header .= "--".$this->boundary."--\n\n";
135
136         foreach($this->to as $mail)
137         {
138             $ret = mail($mail, $this->subject, "", $this->header);
139         }
140         return $ret;
141     }
142 }
143 ?>