note 53887 rejected from ref.mail by mazzanet

From: Date: Thu, 16 Jun 2005 07:03:04 +0000
Subject: note 53887 rejected from ref.mail by mazzanet
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-90512@lists.php.net to get a copy of this message
Note Submitter: me at chrisfryer dot com ---- I spent all day trying to assemble a piece of code that would actually send an Email with a word attachment. I managed to rewrite jdephix’s code and I now got a program that sends an attachment and actually works when directly pasted out of here. Hopefully this will save people some time. <?php $boundary = '-----=' . md5( uniqid ( rand() ) ); $message = ""; $theFile = "thefilesname.doc"; $path = "http://path/to/file/" . $theFile; $fp = fopen($path, 'r'); $content = ''; do //we loop until there is no data left { $data = fread($fp, 8192); if (strlen($data) == 0) break; $content .= $data; } while (true); $content_encode = chunk_split(base64_encode($content)); // The first part of the message this is the content part of your email. // Boundaries need to be above and below every part of the email. $message .= "--" . $boundary . "\n"; $message .= "Content-Type: text/plain; format=flowed\n\n"; $message .= "hi foo\n from bar\n"; $message .= "--" . $boundary . "\n"; // The second part of the message or the "attactment" $message .= "Content-Type: application/msword; name=\"$theFile\"\n"; $message .= "Content-Transfer-Encoding: base64\n"; $message .= "Content-Disposition: attachment; filename=\"$theFile\"\n\n"; $message .= $content_encode . "\n"; // This last boundary needs to have two - after it like this -- unlike the other boundaries $message .= "--" . $boundary . "--\n"; $headers = "From: \"Me\"<me@here.com>\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\""; mail('send@from.com', 'Email with attachment from PHP', $message, $headers); ?>

« previous php.notes (#90512) next »