583a20a489032b0c1342dd4995cbc5d2f0c9462a
[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 $headers = array();
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->headers['From'] = $this->encode($name)." <$mail>";
48
49                 $bcc = get_company_pref('bcc_email');
50                 if ($bcc)
51                         $this->bcc[] = $bcc;
52     }
53
54     function encode($txt)
55     {
56
57         $opts = array('input-charset' => $_SESSION['language']->encoding,
58             'output-charest' => 'utf-8', // utf-8 works always nowadays
59             'line-length'=>  72,
60             'linebreak-chars'=>'\n',
61             'scheme' => 'Q',
62         );
63         return substr(iconv_mime_encode(null, $txt, $opts), 2);
64     }
65
66     //
67     // For backward compatibility in extensions address can be passed
68     // in $name in form: '"full name" <adr@host>'.
69     // Don't use this form unless all the mail addresses are encoded in ASCII
70     //
71     function to($name, $mail='')
72     {
73         $this->to[] = $mail=='' ? $name : ($this->encode($name)." <$mail>");
74     }
75
76     function cc($name, $mail='')
77     {
78         $this->cc[] = $mail=='' ? $name : ($this->encode($name)." <$mail>");
79     }
80
81     function bcc($name='', $mail='')
82     {
83         $this->bcc[] = $mail=='' ? $name : ($this->encode($name)." <$mail>");
84     }
85
86     function attachment($file, $filename=null)
87     {
88         if (!isset($filename))
89                 $filename = $file;
90         $this->attachment[$filename] = $file;
91     }
92
93     function subject($subject)
94     {
95         $this->subject = $this->encode($subject);
96     }
97
98     function text($text)
99     {
100         $this->body = "--$this->boundary\n";
101             $this->body .= "Content-Type: text/plain; charset=\"{$this->charset}\"\n";
102             $this->body .= "Content-Transfer-Encoding: 8bit\n\n";
103             $this->body .= $text."\n";
104     }
105
106     function html($html)
107     {
108         $this->body = "--$this->boundary\n";
109             $this->body .= "Content-Type: text/html; charset=\"{$this->charset}\"\n";
110             $this->body .= "Content-Transfer-Encoding: quoted-printable\n\n";
111             $this->body .= "<html><body>\n".$html."\n</body></html>\n";
112     }
113
114         function mime_type($filename)
115         {
116                 $file = basename($filename, '.zip');
117                 if ($filename == $file . '.zip') return 'application/x-zip-compressed';
118                 $file = basename($filename, '.pdf');
119                 if ($filename == $file . '.pdf') return 'application/pdf';
120                 $file = basename($filename, '.xls'); 
121                 if ($filename == $file . '.xls') return 'application/vnd.ms-excel'; 
122                 $file = basename($filename, '.csv');
123                 if ($filename == $file . '.csv') return 'application/vnd.ms-excel';
124                 $file = basename($filename, '.tar');
125                 if ($filename == $file . '.tar') return 'application/x-tar';
126                 $file = basename($filename, '.tar.gz');
127                 if ($filename == $file . '.tar.gz') return 'application/x-tar-gz';
128                 $file = basename($filename, '.tgz');
129                 if ($filename == $file . '.tgz') return 'application/x-tar-gz';
130                 $file = basename($filename, '.gz');
131                 if ($filename == $file . '.gz') return 'application/x-gzip';
132                 $file = basename($filename, '.html');
133                 if ($filename == $file . '.html') return 'text/html';
134                 return 'application/unknown';
135         }
136
137         function send()
138     {
139         // Add CC Recipients 
140                 if (!empty($this->cc)) 
141                         $this->headers['Cc'] = implode(", ", $this->cc);
142
143                 // Add BCC Recipients 
144                 if (!empty($this->bcc)) 
145                         $this->headers['Bcc'] = implode(", ", $this->bcc);
146                 $this->headers['Content-Type'] = "multipart/mixed;\n boundary=\"$this->boundary\"";
147
148         // Add Attachments
149         if (!empty($this->attachment))
150         {
151             foreach ($this->attachment as $filename => $fname)
152             {
153                 $file = fread(fopen($fname, "r"), filesize($fname));
154                                 $this->body .= "--".$this->boundary."\n";
155                                 $this->body .= "Content-Type: " .$this->mime_type(basename($filename)). "; name=\"".basename($fname)."\"\n";
156                                 $this->body .= "Content-Transfer-Encoding: base64\n";
157                                 $this->body .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
158                                 $this->body .= chunk_split(base64_encode($file),"72","\n");
159             }
160         }
161                 $this->body .= "--".$this->boundary."--\n";
162
163                 $ret = 0;
164         foreach($this->to as $mail)
165         {
166                         if (mail($mail, $this->subject, $this->body, $this->headers, $this->add_params))
167                                 $ret++;
168         }
169         return $ret;
170     }
171 }