65066ca75fa8eb75ca4cbad1d3f6eaacc145f68e
[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  Updated: 2010-10-25
14  Updated by: Michael Hahn (MPH)
15  
16  Problem: The Suhosin patch for PHP was blocking the email before it ever reached
17  the email server due to double line feeds (\n) in the header of the email.
18  Also, the body of the message was included in the header. This would also
19  trip the security measure everytime it spotted a double line feed.
20  Fix: Remove any double line feed from the header info and seperate the body of 
21  the message from the header.
22  Other updates: I'm not sure about RFC compliance but, every other email I've look at had
23  certain information included in double quotes. More than likely to avoid
24  erroneous file naming. I tried to emulate this mindset.
25  Added line length and EOL char for file chunking. For some reason without
26  it there were extra line feeds in the chunked file.
27
28  * Lots of fixes for FA
29 */
30
31 class email
32 {
33     var $to = array();
34     var $cc = array();
35     var $bcc = array();
36     var $attachment = array();
37     var $boundary = "";
38     var $header = "";
39     var $subject = "";
40     var $body = "";
41         var $charset = 'ISO-8859-1';
42         
43     function email($name, $mail)
44     {
45         $this->boundary = md5(uniqid(time()));
46                 $this->header = "From: $name <$mail>\n";
47     }
48
49     function to($mail)
50     {
51         $this->to[] = $mail;
52     }
53
54     function cc($mail)
55     {
56         $this->cc[] = $mail;
57     }
58
59     function bcc($mail)
60     {
61         $this->bcc[] = $mail;
62     }
63
64     function attachment($file)
65     {
66                 $this->attachment[] = $file;
67     }
68
69     function subject($subject)
70     {
71         $this->subject = $subject;
72     }
73
74     function text($text)
75     {
76         $this->body = "--$this->boundary\n";
77             $this->body .= "Content-Type: text/plain; charset=\"{$this->charset}\"\n";
78             $this->body .= "Content-Transfer-Encoding: 8bit\n\n";
79             $this->body .= $text."\n";
80     }
81
82     function html($html)
83     {
84         $this->body = "--$this->boundary\n";
85             $this->body .= "Content-Type: text/html; charset=\"{$this->charset}\"\n";
86             $this->body .= "Content-Transfer-Encoding: quoted-printable\n\n";
87             $this->body .= "<html><body>\n".$html."\n</body></html>\n";
88     }
89
90         function mime_type($filename)
91         {
92                 $file = basename($filename, '.zip');
93                 if ($filename == $file . '.zip') return 'application/x-zip-compressed';
94                 $file = basename($filename, '.pdf');
95                 if ($filename == $file . '.pdf') return 'application/pdf';
96                 $file = basename($filename, '.csv');
97                 if ($filename == $file . '.csv') return 'application/vnd.ms-excel';
98                 $file = basename($filename, '.tar');
99                 if ($filename == $file . '.tar') return 'application/x-tar';
100                 $file = basename($filename, '.tar.gz');
101                 if ($filename == $file . '.tar.gz') return 'application/x-tar-gz';
102                 $file = basename($filename, '.tgz');
103                 if ($filename == $file . '.tgz') return 'application/x-tar-gz';
104                 $file = basename($filename, '.gz');
105                 if ($filename == $file . '.gz') return 'application/x-gzip';
106                 $file = basename($filename, '.html');
107                 if ($filename == $file . '.html') return 'text/html';
108                 return 'application/unknown';
109         }
110
111         function send()
112     {
113         // CC Empfänger hinzufügen
114         $max = count($this->cc);
115         if ($max > 0)
116         {
117             $this->header .= "Cc: ".$this->cc[0];
118             for ($i = 1; $i < $max; $i++)
119             {
120                 $this->header .= ", ".$this->cc[$i];
121             }
122             $this->header .= "\n";
123         }
124         // BCC Empfänger hinzufügen
125         $max = count($this->bcc);
126         if ($max > 0)
127         {
128             $this->header .= "Bcc: ".$this->bcc[0];
129             for ($i = 1; $i < $max; $i++)
130             {
131                 $this->header .= ", ".$this->bcc[$i];
132             }
133             $this->header .= "\n";
134         }
135                 $this->header .= "MIME-Version: 1.0\n";
136                 $this->header .= "Content-Type: multipart/mixed;\n boundary=\"$this->boundary\"\n";
137                 $this->header .= "This is a multi-part message in MIME format.\n";
138
139         // Attachment hinzufügen
140         $max = count($this->attachment);
141         if ($max > 0)
142         {
143             for ($i = 0; $i < $max; $i++)
144             {
145                 $file = fread(fopen($this->attachment[$i], "r"), filesize($this->attachment[$i]));
146                                 $this->body .= "--".$this->boundary."\n";
147                                 $this->body .= "Content-Type: " .$this->mime_type(basename($this->attachment[$i])). "; name=\"".basename($this->attachment[$i])."\"\n";
148                                 $this->body .= "Content-Transfer-Encoding: base64\n";
149                                 $this->body .= "Content-Disposition: attachment; filename=\"".basename($this->attachment[$i])."\"\n\n";
150                                 $this->body .= chunk_split(base64_encode($file),"72","\n");
151                 $file = "";
152             }
153         }
154                         $this->body .= "--".$this->boundary."--\n";
155
156                 $ret = 0;
157         foreach($this->to as $mail)
158         {
159                         if (mail($mail, $this->subject, $this->body, $this->header))
160                                 $ret++;
161         }
162         return $ret;
163     }
164 }
165 ?>