FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » General » PHP discussions » Email Attaching a file already uploaded
Show: Today's Messages :: Unread Messages :: Show Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
icon5.gif  Email Attaching a file already uploaded [message #31717] Sun, 14 May 2006 18:18 Go to next message
PhilVaz is currently offline  PhilVaz   United States
Messages: 2
Registered: May 2006
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
Say I have a Word .doc file already online on my web host.

I want to attach that file to a blank Email.

I have some code for a user to select a file, then upload, then attach to an Email, which does work.

But how do I simply attach a file already uploaded and online? I don't want the user to select the file, just attach one already online. What are the steps?

Phil P
icon14.gif  solution! [message #31785 is a reply to message #31717] Wed, 17 May 2006 22:22 Go to previous message
PhilVaz is currently offline  PhilVaz   United States
Messages: 2
Registered: May 2006
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
All righty, here's the solution Very Happy

To attach a Word doc that is already online

<?php
$subject
="subject line";
$from = "email(at)something(dot)com";
   
$type 
= "application/msword";
$name = "file.doc";
         
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
   
$message 
= "$toname, your doc is attached.\n\nThank you for visiting the site.";

// open and read the file as binary
$file = fopen($name,'rb');
$data = fread($file,filesize($name));
fclose($file);

// encode and split it into acceptable length lines
$data = chunk_split(base64_encode($data));

// message headers
$headers = "From: $from\r\n" .
           "MIME-Version: 1.0\r\n" .
           "Content-Type: multipart/mixed;\r\n" .
           " boundary=\"{$mime_boundary}\"";

// message body
$message = "This is a multi-part message in MIME format.\n\n" .
           "--{$mime_boundary}\n" .
           "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
           "Content-Transfer-Encoding: 7bit\n\n" .
           $message . "\n\n";

// insert a boundary to indicate start of the attachment
// specify content type, file name, and attachment
// then add file content and set another boundary
$message .= "--{$mime_boundary}\n" .
            "Content-Type: {$type};\n" .
            " name=\"{$name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
            $data . "\n\n" .
            "--{$mime_boundary}--\n";

// send the message
if (@mail($toemail, $subject, $message, $headers)) echo "$toname, your Email with attachment was sent.<br><br>Thank you for visiting the site.";
else echo "Sorry, failed to send Email, please try again in a few minutes.";
?>


Phil P

[Updated on: Wed, 17 May 2006 22:24]

Report message to a moderator

Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
Previous Topic: Shopping Carts
Next Topic: Logging users into the forum from a separate site
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Oct 24 07:30:20 EDT 2017

Total time taken to generate the page: 0.00557 seconds