emailclass.php
<BR><? <BR>class CMailFile { <br><br>var $subject; <BR>var $addr_to; <BR>var $text_body; <BR>var $text_encoded; <BR>var $mime_headers; <BR>var $mime_boundary = "--==================_846811060==_"; <BR>var $smtp_headers; <br><br>function CMailFile($subject,$to,$from,$msg,$filename,$downfilename,$mimetype = "application/octet-stream",$mime_filename = false) { <BR>$this->subject = $subject; <BR>$this->addr_to = $to; <BR>$this->smtp_headers = $this->write_smtpheaders($from); <BR>$this->text_body = $this->write_body($msg); <BR>$this->text_encoded = $this->attach_file($filename,$downfilename,$mimetype,$mime_filename); <BR>$this->mime_headers = $this->write_mimeheaders($filename, $mime_filename); <BR>} <br><br>function attach_file($filename,$downfilename,$mimetype,$mime_filename) { <BR>$encoded = $this->encode_file($filename); <BR>if ($mime_filename) $filename = $mime_filename; <BR>$out = "--" . $this->mime_boundary . "\n"; <BR>$out = $out . "Content-type: " . $mimetype . "; name=\"$filename\";\n"; <BR>$out = $out . "Content-Transfer-Encoding: base64\n"; <BR>$out = $out . "Content-disposition: attachment; filename=\"$downfilename\"\n\n"; <BR>$out = $out . $encoded . "\n"; <BR>$out = $out . "--" . $this->mime_boundary . "--" . "\n"; <BR>return $out; <BR>} <br><br>function encode_file($sourcefile) { <BR>if (is_readable($sourcefile)) { <BR>$fd = fopen($sourcefile, "r"); <BR>$contents = fread($fd, filesize($sourcefile)); <BR>$encoded = chunk_split(base64_encode($contents)); <BR>fclose($fd); <BR>} <BR>return $encoded; <BR>} <br><br>function sendfile() { <BR>$headers = $this->smtp_headers . $this->mime_headers; <BR>$message = $this->text_body . $this->text_encoded; <BR><div>)本文来源gaodai.ma#com搞#代!码网_</div><strong>搞代gaodaima码</strong>mail($this->addr_to,$this->subject,$message,$headers); <BR>} <br><br>function write_body($msgtext) { <BR>$out = "--" . $this->mime_boundary . "\n"; <BR>$out = $out . "Content-Type: text/plain; charset=\"us-ascii\"\n\n"; <BR>$out = $out . $msgtext . "\n"; <BR>return $out; <BR>} <br><br>function write_mimeheaders($filename, $mime_filename) { <BR>if ($mime_filename) $filename = $mime_filename; <BR>$out = "MIME-version: 1.0\n"; <BR>$out = $out . "Content-type: multipart/mixed; "; <BR>$out = $out . "boundary=\"$this->mime_boundary\"\n"; <BR>$out = $out . "Content-transfer-encoding: 7BIT\n"; <BR>$out = $out . "X-attachments: $filename;\n\n"; <BR>return $out; <BR>} <br><br>function write_smtpheaders($addr_from) { <BR>$out = "From: $addr_from\n"; <BR>$out = $out . "Reply-To: $addr_from\n"; <BR>$out = $out . "X-Mailer: PHP3\n"; <BR>$out = $out . "X-Sender: $addr_from\n"; <BR>return $out; <BR>} <BR>} <br><br>/*用法 - 例如:mimetype 为 "image/gif" <BR>$mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$mimetype); <BR>$mailfile->sendfile(); <br><br>$subject -- 主题 <BR>$sendto -- 收信人地址 <BR>$replyto -- 回复地址 <BR>$message -- 信件内容 <BR>$filename -- 附件文件名 <BR>$downfilename -- 下載的文件名 <BR>$mimetype -- mime类型 <BR>*/ <BR>?> <BR>
Demo
<BR><?php <BR>require_once('emailclass.php'); <br><br>//发送邮件 <br><br>//主題 <BR>$subject = "test send email"; <br><br>//收件人 <BR>$sendto = '[email protected]'; <br><br>//發件人 <BR>$replyto = '[email protected]'; <br><br>//內容 <BR>$message = "test send email content"; <br><br>//附件 <BR>$filename = 'test.jpg'; <br><br>//附件類別 <BR>$mimetype = "image/jpeg"; <br><br>$mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$excelname,$mimetype); <BR>$mailfile->sendfile(); <BR>?> <BR>