Fixed error reporting in mail send method
[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";
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";
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                 return 'application/unknown';
107         }
108
109         function send()
110     {
111         // CC Empfänger hinzufügen
112         $max = count($this->cc);
113         if ($max > 0)
114         {
115             $this->header .= "Cc: ".$this->cc[0];
116             for ($i = 1; $i < $max; $i++)
117             {
118                 $this->header .= ", ".$this->cc[$i];
119             }
120             $this->header .= "\n";
121         }
122         // BCC Empfänger hinzufügen
123         $max = count($this->bcc);
124         if ($max > 0)
125         {
126             $this->header .= "Bcc: ".$this->bcc[0];
127             for ($i = 1; $i < $max; $i++)
128             {
129                 $this->header .= ", ".$this->bcc[$i];
130             }
131             $this->header .= "\n";
132         }
133                 $this->header .= "MIME-Version: 1.0\n";
134                 $this->header .= "Content-Type: multipart/mixed;\n boundary=\"$this->boundary\"\n";
135                 $this->header .= "This is a multi-part message in MIME format.\n";
136
137         // Attachment hinzufügen
138         $max = count($this->attachment);
139         if ($max > 0)
140         {
141             for ($i = 0; $i < $max; $i++)
142             {
143                 $file = fread(fopen($this->attachment[$i], "r"), filesize($this->attachment[$i]));
144                                 $this->body .= "--".$this->boundary."\n";
145                                 $this->body .= "Content-Type: " .$this->mime_type(basename($this->attachment[$i])). "; name=\"".basename($this->attachment[$i])."\"\n";
146                                 $this->body .= "Content-Transfer-Encoding: base64\n";
147                                 $this->body .= "Content-Disposition: attachment; filename=\"".basename($this->attachment[$i])."\"\n\n";
148                                 $this->body .= chunk_split(base64_encode($file),"72","\n");
149                 $file = "";
150             }
151         }
152                         $this->body .= "--".$this->boundary."--\n";
153
154                 $ret = 0;
155         foreach($this->to as $mail)
156         {
157                         if (mail($mail, $this->subject, $this->body, $this->header))
158                                 $ret++;
159         }
160         return $ret;
161     }
162 }
163 ?>