note 53887 added to ref.mail
| From: | me at chrisfryer dot com | Date: | Thu, 16 Jun 2005 06:53:05 +0000 |
| Subject: | note 53887 added to ref.mail | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90510@lists.php.net to get a copy of this message | ||
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 jdephixs 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);
?>
----
Manual Page -- http://www.php.net/manual/en/ref.mail.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53887
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53887&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53887&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53887&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53887&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+53887&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53887&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53887&report=yes
Search -- http://master.php.net/manage/user-notes.php