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";
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";
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 return 'application/unknown';
111 // CC Empfänger hinzufügen
112 $max = count($this->cc);
115 $this->header .= "Cc: ".$this->cc[0];
116 for ($i = 1; $i < $max; $i++)
118 $this->header .= ", ".$this->cc[$i];
120 $this->header .= "\n";
122 // BCC Empfänger hinzufügen
123 $max = count($this->bcc);
126 $this->header .= "Bcc: ".$this->bcc[0];
127 for ($i = 1; $i < $max; $i++)
129 $this->header .= ", ".$this->bcc[$i];
131 $this->header .= "\n";
133 $this->header .= "MIME-Version: 1.0\n";
134 $this->header .= "Content-Type: multipart/mixed;\n boundary=\"$this->boundary\"\n";
135 $this->header .= "This is a multi-part message in MIME format.\n";
137 // Attachment hinzufügen
138 $max = count($this->attachment);
141 for ($i = 0; $i < $max; $i++)
143 $file = fread(fopen($this->attachment[$i], "r"), filesize($this->attachment[$i]));
144 $this->body .= "--".$this->boundary."\n";
145 $this->body .= "Content-Type: " .$this->mime_type(basename($this->attachment[$i])). "; name=\"".basename($this->attachment[$i])."\"\n";
146 $this->body .= "Content-Transfer-Encoding: base64\n";
147 $this->body .= "Content-Disposition: attachment; filename=\"".basename($this->attachment[$i])."\"\n\n";
148 $this->body .= chunk_split(base64_encode($file),"72","\n");
152 $this->body .= "--".$this->boundary."--\n";
154 foreach($this->to as $mail)
156 $ret = mail($mail, $this->subject, $this->body, $this->header);