-1

I have a form for that a user fills out and submits. Once submitted The form needs to create a doc from the input, attach the doc to an email with the mail() function and email it to the person I specify.

Currently I am creating the doc file this way:

HEADER("Content-Type: application/vnd.ms-word; name='word'"); 
HEADER("Content-type: application/octet-stream"); 
HEADER("Content-Disposition: attachment; filename=filename_here.doc"); 
HEADER("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
HEADER("Pragma: no-cache"); 
HEADER("Expires: 0"); 

ECHO $header."\n".$body; 
EXIT;

Obviously this outputs the document. I want to attach it to an email since I don't want the user to get the document. The user will be redirected to a confirmation or error page depending on the situation.

Thanks!

-------------- EDIT -----------------

I have the script sending an email with an attachment now. However, the attachment is only 3k is size. This is the script I'm using to attach the data and send the email. Its the same script I use when a user provides the file. I changed the script from reading in the data from a file to base ecode the "body" directly.

$headers = "From: fromperson@email.com";

    // Generate a boundary string
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    // Add the headers for a file attachment
    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";

    // Add a multipart boundary above the plain message
    $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" .
    $content . "\n\n";

    // Base64 encode the file data
    $data = chunk_split(base64_encode($body));

    // Add file attachment to the message
    $message .= "--{$mime_boundary}\n" .
    "Content-Type: application/ms-word;\n" .
    " name=\"testfile.doc\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"testfile.doc\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";

    // Send the message
    $ok = @mail('toperson@email.com', 'test file', $message, $headers);
    if ($ok) 
    {
        echo 'yes';
    } else 
    {
        echo 'no';
    }

I'm not seeing why the file is only 3k.

dcp3450
  • 10,959
  • 23
  • 58
  • 110
  • You should use the search feature: http://stackoverflow.com/questions/565082/attach-file-in-formmail-php-formmail http://stackoverflow.com/questions/1330626/how-can-i-send-an-email-with-attachments-from-a-php-form http://stackoverflow.com/questions/1578389/attach-file-to-email-using-php http://stackoverflow.com/questions/3582796/email-attachment-in-php http://stackoverflow.com/questions/5480383/php-create-file-from-string-and-attach-to-email – Lawrence Cherone Nov 20 '11 at 08:53
  • I searched first. All the questions I came across seemed to talk about a user provided file OR a file from a location. Nothing appeared to discuss a file that was dynamically created via php. – dcp3450 Nov 20 '11 at 08:56
  • Whether its a supplied file or a file generated by php its irrelevant, as in most cases it would need be handled the same when attached to a mail message. – Lawrence Cherone Nov 20 '11 at 09:03
  • Ok. I think the issue I'm having then is, how do I capture the dynamically created file since it's not saved anywhere? I have experience with the above: outputting a file. As well as sending a file provided by the user but not creating the file then emailing. The above will always output. – dcp3450 Nov 20 '11 at 09:19
  • You already have the file's binary data in `$body`. – hakre Nov 20 '11 at 10:00
  • I updated the question to include the code I'm using to attach and send the email. The file being sent is only 3k. – dcp3450 Nov 20 '11 at 17:57

1 Answers1

0

The script that was added as the edit is a functional script. The blank file was an issue on my end (user error from lack of sleep). For those that come across this question: The script after the edit is a working script.

dcp3450
  • 10,959
  • 23
  • 58
  • 110