views:

473

answers:

5

I got the form to accept an attachment and send the attachment but if it's a doc it's blank:

   $fileatt = $_FILES['file']['tmp_name'];
   $fileattType = $_FILES['file']['type'];
   $fileattName = $_FILES['file']['name'];
   $file = fopen($fileatt,'rb');
   $data = fread($file,filesize($fileatt));
   fclose($file);

   $semi_rand = md5(time());
   $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

   $headers = "from: $email";

   $headers .= "\nMIME-Version: 1.0\n" .
        "Content-Type: multipart/mixed;\n" .
        " boundary=\"{$mime_boundary}\"";

   $content = "Info about user";

   $content = "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";

   $data = chunk_split(base64_encode($data));

   $content .= "--{$mime_boundary}\n" .
        "Content-Type: {$fileattType};\n" .
        " name=\"{$fileattName}\"\n" .
        "Content-Disposition: attachment;\n" .
        " filename=\"{$fileattName}\"\n" .
        "Content-Transfer-Encoding: base64\n\n" .
        $data . "\n\n" .
        "--{$mime_boundary}--\n";

   $send = mail( "[email protected]", "Form", $content, $headers);
         if($send)
            header('Location: success page');
         else
            header('Location: fail page');

The email sends correctly, there is an attachment with the correct name, file type, and file size. when it opens it is empty.

I got the tutorial from sitepoint: http://articles.sitep... ... mail-php/5

I tested the files they provide and the same thing happens. Any ideas would be great. thanks everyone!

+2  A: 

Welcome to SO.

This is not a direct answer to your question, but I would recommend you use a ready-made mailing class like PHPMailer. It has all the quirks, encoding and attaching functions already built in, which is much less error-prone than doing it by hand. If you want to go that way, maybe this collection of tutorial links on PHPMailer will help you get started.

Pekka
Agreed, just use PHPMailer!
Eduardo
I've seen this suggested multiple times on other forums regarding mail handling. I'll check it out.
dcp3450
+2  A: 

Why not use a decent library for sending emails?

I strongly suggest you start using a library for sending emails, it eases the process of writing emails and makes the code a lot more transient.

I can vouch for Swiftmailer, an actively developed library with excellent features.

Especially complicated tasks like attachments, inline images and multiple recipients are implemented easily.

Marijn Huizendveld
The website of Swiftmailer contains excellent documentation that is both pragmatic and in depth. A quick reference of the library shows you how to create messages: http://swiftmailer.org/docs/message-quickrefGood luck!
Marijn Huizendveld
am I required to upload the attachment to my web server before I can send it?
dcp3450
Yes, you always are.
Pekka
Any attachments you wish to send should be available to your PHP environment, i.e. a readable directory on your webserver.
Marijn Huizendveld
"Why not use a decent library for sending emails?" maybe because decent library like the one you suggested are made by 141 php files, when one short and well written simple script coud be used to send SIMPLE mail with ONE small attachment.
Marco Demajo
@Marco Demaio: I guess you don't fancy using libraries in particular. To me it seems strange that people are trying to reinvent functionality while there are perfect _FREE_ libraries out there. Especially in the context of working in teams where libraries not only provide quality tools but also serve as a common grammar and vocabulary for team communication...
Marijn Huizendveld
@Marijn Huizendveld, read this: http://reprog.wordpress.com/2010/03/03/whatever-happened-to-programming/
Marco Demajo
+2  A: 

I would strongly recommend using a preexisting tool to send emails, such as Swift. There are a lot of caveats that it will take care of for you, and it's quite easy to use.

Dan Breen
+1  A: 

Perhaps you could use \r\n instead of those \n\n

OcuS
+1 the 1st DIRCET answer. It's not enought, but it's a good starting point, you must use "\r\n" and NOT "\n\n".
Marco Demajo
A: 

Answer with code example is here: http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php

Marco Demajo
problem is, the php seems to be stripping the header. I placed the php that processes sending the email with attachment on another server and it works perfect.
dcp3450