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();
41 var $charset = 'ISO-8859-1';
43 function email($name, $mail)
45 $this->boundary = md5(uniqid(time()));
46 $this->header = "From: $name <$mail>\n";
64 function attachment($file)
66 $this->attachment[] = $file;
69 function subject($subject)
71 $this->subject = $subject;
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\n";
79 $this->body .= $text."\n";
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\n";
87 $this->body .= "<html><body>\n".$html."\n</body></html>\n";
90 function mime_type($filename)
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 $file = basename($filename, '.html');
107 if ($filename == $file . '.html') return 'text/html';
108 return 'application/unknown';
113 // CC Empfänger hinzufügen
114 $max = count($this->cc);
117 $this->header .= "Cc: ".$this->cc[0];
118 for ($i = 1; $i < $max; $i++)
120 $this->header .= ", ".$this->cc[$i];
122 $this->header .= "\n";
124 // BCC Empfänger hinzufügen
125 $max = count($this->bcc);
128 $this->header .= "Bcc: ".$this->bcc[0];
129 for ($i = 1; $i < $max; $i++)
131 $this->header .= ", ".$this->bcc[$i];
133 $this->header .= "\n";
135 $this->header .= "MIME-Version: 1.0\n";
136 $this->header .= "Content-Type: multipart/mixed;\n boundary=\"$this->boundary\"\n";
137 $this->header .= "This is a multi-part message in MIME format.\n";
139 // Attachment hinzufügen
140 $max = count($this->attachment);
143 for ($i = 0; $i < $max; $i++)
145 $file = fread(fopen($this->attachment[$i], "r"), filesize($this->attachment[$i]));
146 $this->body .= "--".$this->boundary."\n";
147 $this->body .= "Content-Type: " .$this->mime_type(basename($this->attachment[$i])). "; name=\"".basename($this->attachment[$i])."\"\n";
148 $this->body .= "Content-Transfer-Encoding: base64\n";
149 $this->body .= "Content-Disposition: attachment; filename=\"".basename($this->attachment[$i])."\"\n\n";
150 $this->body .= chunk_split(base64_encode($file),"72","\n");
154 $this->body .= "--".$this->boundary."--\n";
157 foreach($this->to as $mail)
159 if (mail($mail, $this->subject, $this->body, $this->header))