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