Merged changes from master branch up to current state.
[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         var $add_params;
43         
44     function email($name, $mail)
45     {
46         $this->boundary = md5(uniqid(time()));
47                 $this->header = "From: $name <$mail>\n";
48                 $bcc = get_company_pref('bcc_email');
49                 if ($bcc)
50                         $this->bcc[] = $bcc;
51     }
52
53     function to($mail)
54     {
55         $this->to[] = $mail;
56     }
57
58     function cc($mail)
59     {
60         $this->cc[] = $mail;
61     }
62
63     function bcc($mail)
64     {
65         $this->bcc[] = $mail;
66     }
67
68     function attachment($file)
69     {
70                 $this->attachment[] = $file;
71     }
72
73     function subject($subject)
74     {
75         $this->subject = $subject;
76     }
77
78     function text($text)
79     {
80         $this->body = "--$this->boundary\n";
81             $this->body .= "Content-Type: text/plain; charset=\"{$this->charset}\"\n";
82             $this->body .= "Content-Transfer-Encoding: 8bit\n\n";
83             $this->body .= $text."\n";
84     }
85
86     function html($html)
87     {
88         $this->body = "--$this->boundary\n";
89             $this->body .= "Content-Type: text/html; charset=\"{$this->charset}\"\n";
90             $this->body .= "Content-Transfer-Encoding: quoted-printable\n\n";
91             $this->body .= "<html><body>\n".$html."\n</body></html>\n";
92     }
93
94         function mime_type($filename)
95         {
96                 $file = basename($filename, '.zip');
97                 if ($filename == $file . '.zip') return 'application/x-zip-compressed';
98                 $file = basename($filename, '.pdf');
99                 if ($filename == $file . '.pdf') return 'application/pdf';
100                 $file = basename($filename, '.xls'); 
101                 if ($filename == $file . '.xls') return 'application/vnd.ms-excel'; 
102                 $file = basename($filename, '.csv');
103                 if ($filename == $file . '.csv') return 'application/vnd.ms-excel';
104                 $file = basename($filename, '.tar');
105                 if ($filename == $file . '.tar') return 'application/x-tar';
106                 $file = basename($filename, '.tar.gz');
107                 if ($filename == $file . '.tar.gz') return 'application/x-tar-gz';
108                 $file = basename($filename, '.tgz');
109                 if ($filename == $file . '.tgz') return 'application/x-tar-gz';
110                 $file = basename($filename, '.gz');
111                 if ($filename == $file . '.gz') return 'application/x-gzip';
112                 $file = basename($filename, '.html');
113                 if ($filename == $file . '.html') return 'text/html';
114                 return 'application/unknown';
115         }
116
117         function send()
118     {
119         // Add CC Recipients 
120                 if (!empty($this->cc)) 
121                         $this->header .= "Cc: " . implode(", ", $this->cc) . "\n" ; 
122
123                 // Add BCC Recipients 
124                 if (!empty($this->bcc)) 
125                         $this->header .= "Bcc: " . implode(", ", $this->bcc) . "\n" ; 
126                 $this->header .= "Content-Type: multipart/mixed;\n boundary=\"$this->boundary\"\n";
127
128         // Add Attachments
129         $max = count($this->attachment);
130         if ($max > 0)
131         {
132             for ($i = 0; $i < $max; $i++)
133             {
134                 $file = fread(fopen($this->attachment[$i], "r"), filesize($this->attachment[$i]));
135                                 $this->body .= "--".$this->boundary."\n";
136                                 $this->body .= "Content-Type: " .$this->mime_type(basename($this->attachment[$i])). "; name=\"".basename($this->attachment[$i])."\"\n";
137                                 $this->body .= "Content-Transfer-Encoding: base64\n";
138                                 $this->body .= "Content-Disposition: attachment; filename=\"".basename($this->attachment[$i])."\"\n\n";
139                                 $this->body .= chunk_split(base64_encode($file),"72","\n");
140                 $file = "";
141             }
142         }
143                 $this->body .= "--".$this->boundary."--\n";
144
145                 $ret = 0;
146         foreach($this->to as $mail)
147         {
148                         if (mail($mail, $this->subject, $this->body, $this->header, $this->add_params))
149                                 $ret++;
150         }
151         return $ret;
152     }
153 }