mime mail class (Sascha & Tobias) - msWORD doc attachment not working

From: Date: Mon, 19 Mar 2001 16:56:17 +0000
Subject: mime mail class (Sascha & Tobias) - msWORD doc attachment not working
Groups: php.general 
Request: Send a blank email to php-general+get-44588@lists.php.net to get a copy of this message
Hi there I am trying to get the scriplet code from http://www.phpwizard.net/resources/phpMisc/scripts/pretty/mail.php3 to work for a website I'm building. I am trying to allow a user to select a file from their hard drive, and then have that file sent as an email attachment. Most of the time that file will be a Microsoft Word document. I have the code for the file selection all done, and I integrated the codelet (THANK YOU) into my code, and when I attach a .jpg file, it works fine, but if I choose a word doc, it attaches what it thinks is an image. I changed the line $mail->add_attachment("$attachment", $copy_filename, "application/msword"); to reflect the mime type for application/msword, but it still doesn't work. What suggestions might you have? url to see it in action http://www.emergejobs.com/jen/jen_file_upload.phtml the submit script that does the file upload then the email send is below. thank you SO MUCH -jen . ----- jen_file_upload_submit.phtml: <html><head><title>Jen's file upload module</title></head> <BODY bgcolor="#666699" link="#FF9933" text="#FFFFFF" vlink="#FF8040"> <font face="arial, helvetica, sans-serif"> <P><h1>file upload</h1><P> <?php $directory_to_copy_to = "/home/web/e/emerge/virtual_html/jen/"; function is_uploaded_file($filename) { if (!$tmp_file = get_cfg_var('upload_tmp_dir')) { $tmp_file = dirname(tempnam('', '')); } $tmp_file .= '/' . basename($filename); /* User might have trailing slash in php.ini... */ return (ereg_replace('/+', '/', $tmp_file) == $filename); } if (is_uploaded_file($userfile)) { $clean_filename = ereg_replace(" ","_",$userfile_name); $clean_filename = strtoupper($clean_filename); $copy_filename = $directory_to_copy_to . $clean_filename; copy($userfile, $copy_filename); unlink($userfile); echo "<P>you uploaded $userfile, <P>this script just renamed it (and made it all caps) to $clean_filename <P>and copied it to $copy_filename"; } else { echo "ERROR there was an error uploading the file: '$userfile' ($userfile_name)."; } /* * Class mime_mail * Original implementation by Sascha Schumann <sascha@schumann.cx> * Modified by Tobias Ratschiller <tobias@dnet.it>: * - General code clean-up * - separate body- and from-property * - killed some mostly un-necessary stuff */ class mime_mail { var $parts; var $to; var $from; var $headers; var $subject; var $body; /* * void mime_mail() * class constructor */ function mime_mail() { $this->parts = array(); $this->to = ""; $this->from = ""; $this->subject = ""; $this->body = ""; $this->headers = ""; } /* * void add_attachment(string message, [string name], [string ctype]) * Add an attachment to the mail object */ function add_attachment($message, $name = "", $ctype = "application/octet-stream") { $this->parts[] = array ( "ctype" => $ctype, "message" => $message, "encode" => $encode, "name" => $name ); } /* * void build_message(array part= * Build message parts of an multipart mail */ function build_message($part) { $message = $part[ "message"]; $message = chunk_split(base64_encode($message)); $encoding = "base64"; return "Content-Type: ".$part[ "ctype"]. ($part[ "name"]? "; name = \"".$part[ "name"]. "\"" : ""). "\nContent-Transfer-Encoding: $encoding\n\n$message\n"; } /* * void build_multipart() * Build a multipart mail */ function build_multipart() { $boundary = "b".md5(uniqid(time())); $multipart = "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary"; for($i = sizeof($this->parts)-1; $i >= 0; $i--) { $multipart .= "\n".$this->build_message($this->parts[$i]). "--$boundary"; } return $multipart.= "--\n"; } /* * void send() * Send the mail (last class-function to be called) */ function send() { $mime = ""; if (!empty($this->from)) $mime .= "From: ".$this->from. "\n"; if (!empty($this->headers)) $mime .= $this->headers. "\n"; if (!empty($this->body)) $this->add_attachment($this->body, "", "text/plain"); $mime .= "MIME-Version: 1.0\n".$this->build_multipart(); mail($this->to, $this->subject, "", $mime); } }; // end of class /* * Example usage */ $attachment = fread(fopen($copy_filename, "r"), filesize($copy_filename)); $mail = new mime_mail(); $mail->from = "jen@jenhallonline.com"; $mail->headers = "Errors-To: jen@jenhallonline.com"; $mail->to = "jhall@conovertuttle.com"; $mail->subject = "Testing email attachment..."; $mail->body = "This is just a test."; $mail->add_attachment("$attachment", $copy_filename, "application/msword"); $mail->send(); ?> <P>end of script. </body></html>

« previous php.general (#44588) next »