php 8 error. class.mail.inc. line 149. #5 parameter cannot be null.
[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 __construct($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, $filename=null)
69     {
70         if (!isset($filename))
71                 $filename = basename($file);
72         $this->attachment[$filename] = $file;
73     }
74
75     function subject($subject)
76     {
77         $this->subject = $subject;
78     }
79
80     function text($text)
81     {
82         $this->body = "--$this->boundary\n";
83             $this->body .= "Content-Type: text/plain; charset=\"{$this->charset}\"\n";
84             $this->body .= "Content-Transfer-Encoding: 8bit\n\n";
85             $this->body .= $text."\n";
86     }
87
88     function html($html)
89     {
90         $this->body = "--$this->boundary\n";
91             $this->body .= "Content-Type: text/html; charset=\"{$this->charset}\"\n";
92             $this->body .= "Content-Transfer-Encoding: quoted-printable\n\n";
93             $this->body .= "<html><body>\n".$html."\n</body></html>\n";
94     }
95
96         function mime_type($filename)
97         {
98                 $file = basename($filename, '.zip');
99                 if ($filename == $file . '.zip') return 'application/x-zip-compressed';
100                 $file = basename($filename, '.pdf');
101                 if ($filename == $file . '.pdf') return 'application/pdf';
102                 $file = basename($filename, '.xls'); 
103                 if ($filename == $file . '.xls') return 'application/vnd.ms-excel'; 
104                 $file = basename($filename, '.csv');
105                 if ($filename == $file . '.csv') return 'application/vnd.ms-excel';
106                 $file = basename($filename, '.tar');
107                 if ($filename == $file . '.tar') return 'application/x-tar';
108                 $file = basename($filename, '.tar.gz');
109                 if ($filename == $file . '.tar.gz') return 'application/x-tar-gz';
110                 $file = basename($filename, '.tgz');
111                 if ($filename == $file . '.tgz') return 'application/x-tar-gz';
112                 $file = basename($filename, '.gz');
113                 if ($filename == $file . '.gz') return 'application/x-gzip';
114                 $file = basename($filename, '.html');
115                 if ($filename == $file . '.html') return 'text/html';
116                 return 'application/unknown';
117         }
118
119         function send()
120     {
121         // Add CC Recipients 
122                 if (!empty($this->cc)) 
123                         $this->header .= "Cc: " . implode(", ", $this->cc) . "\n" ; 
124
125                 // Add BCC Recipients 
126                 if (!empty($this->bcc)) 
127                         $this->header .= "Bcc: " . implode(", ", $this->bcc) . "\n" ; 
128                 $this->header .= "Content-Type: multipart/mixed;\n boundary=\"$this->boundary\"\n";
129
130         // Add Attachments
131         if (!empty($this->attachment))
132         {
133             foreach ($this->attachment as $filename => $file)
134             {
135                 $file = fread(fopen($file, "r"), filesize($file));
136                                 $this->body .= "--".$this->boundary."\n";
137                                 $this->body .= "Content-Type: " .$this->mime_type($file). "; name=\"".$filename."\"\n";
138                                 $this->body .= "Content-Transfer-Encoding: base64\n";
139                                 $this->body .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
140                                 $this->body .= chunk_split(base64_encode($file),"72","\n");
141                 $file = "";
142             }
143         }
144                 $this->body .= "--".$this->boundary."--\n";
145
146                 $ret = 0;
147         foreach($this->to as $mail)
148         {
149                         if (mail($mail, $this->subject, $this->body, $this->header, $this->add_params))
150                                 $ret++;
151         }
152         return $ret;
153     }
154 }