4 Description: Simple sending eMail in text and HTML with CC, BCC and attachment
6 last modified: 2004-05-14
9 Homepage: http://www.danielkaefer.de
11 Leave this header in this file!
12 ------------------------------------------------------------------
14 Updated by: Michael Hahn (MPH)
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.
28 * Lots of fixes for FA
36 var $attachment = array();
38 var $headers = array();
41 var $charset = 'ISO-8859-1';
44 function __construct($name, $mail)
46 $this->boundary = md5(uniqid(time()));
47 $this->headers['From'] = $this->encode($name)." <$mail>";
49 $bcc = get_company_pref('bcc_email');
57 $opts = array('input-charset' => $_SESSION['language']->encoding,
58 'output-charest' => 'utf-8', // utf-8 works always nowadays
60 'linebreak-chars'=>'\n',
63 return substr(iconv_mime_encode('', $txt, $opts), 2);
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
71 function to($name, $mail='')
73 $this->to[] = $mail=='' ? $name : ($this->encode($name)." <$mail>");
76 function cc($name, $mail='')
78 $this->cc[] = $mail=='' ? $name : ($this->encode($name)." <$mail>");
81 function bcc($name='', $mail='')
83 $this->bcc[] = $mail=='' ? $name : ($this->encode($name)." <$mail>");
86 function attachment($file, $filename=null)
88 if (!isset($filename))
90 $this->attachment[$filename] = $file;
93 function subject($subject)
95 $this->subject = $this->encode($subject);
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";
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";
114 function mime_type($filename)
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';
140 if (!empty($this->cc))
141 $this->headers['Cc'] = implode(", ", $this->cc);
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\"";
149 if (!empty($this->attachment))
151 foreach ($this->attachment as $filename => $fname)
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");
161 $this->body .= "--".$this->boundary."--\n";
164 foreach($this->to as $mail)
166 if (mail($mail, $this->subject, $this->body, $this->headers, $this->add_params))