Forum Moderators: coopster

Message Too Old, No Replies

Attaching a Word Document

         

Saboi

4:23 pm on Oct 9, 2007 (gmt 0)

10+ Year Member



Guys, i have the following code i am using to attach files to an email. It works and does do the attaching, all MS Word documents i attach do not open as they are. Instead they open with unreadable text. Where could i be going wrong? When i change the file type to application/pdf and attach a pdf file, it opens well, just as it is. Please advice!

<?php

$name=$_POST["name"];

$reply_email=$_POST["reply_email"];

$sub=$_POST["subject"];
$attach_file=$_POST["attach_file"];
$sender=$_POST["sender_email"];
$reciever=$_POST["reciever"];
$message_to_send=$_POST["message"];
$multi_receivers=$_POST["bcc_receivers"];

if((empty($name)¦¦empty($reply_email)¦¦empty($sub)¦¦empty($reciever)¦¦ empty($message_to_send)¦¦empty($multi_receivers))){ header("Location:empty.php");}

elseif($attach_file=="Yes")

{

$fileatt = $_FILES['attachment']['tmp_name']; // Path to the file
$fileatt_type = "application/msword"; // File Type
$fileatt_name = "attached"; // Filename that will be used for the file as the attachment

$headers = "From:$name<$sender>\r\nReply-To:<me@email.com>\r\nBcc:$multi_receivers\n";

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

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

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

/* First File*/

$fileatt = $_FILES['attachment']['tmp_name']; // Path to the file
$fileatt_type = "application/msword";// File Type
$fileatt_name = "attached"; // Filename that will be used for the file as the attachment

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

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

$message_to_send .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);

/* End of File Config */

mail($to=$reciever, $subject=$sub, $message="$message_to_send", $headers);

?>

[edited by: dreamcatcher at 11:39 am (utc) on Oct. 10, 2007]
[edit reason] Fixed side scroll. [/edit]

eelixduppy

8:14 pm on Oct 9, 2007 (gmt 0)



Can you give us the email header you are receiving from this script in your actually email?

Also, this line is a little funky. You might want to change it to something like this:


mail($reciever, $sub, $message_to_send, $headers);

And you should really be validating the user-input making sure they aren't injecting headers into your code.

Saboi

9:13 am on Oct 10, 2007 (gmt 0)

10+ Year Member



Hi,

Thanks for reply. Will post the header. As for validation, i am using JavaScript at the client side.

PHP_Chimp

9:31 am on Oct 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So what about people that turn off javascript? Does your validation stop them or do they pass with out validation?

Personally I always validate on the server, as you control over what happens there, but no control over the client side.

Not much help with your headers I know :p

Saboi

10:42 am on Oct 11, 2007 (gmt 0)

10+ Year Member



Hi,

I am doing both client side and server side validation. Thanks.

Yes, not much progress with the header. Anything in mind?

Thanks.

Saboi

PHP_Chimp

11:46 am on Oct 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you post the actual headers or results from the script? If it just generates an error then post that.

Saboi

1:18 pm on Oct 15, 2007 (gmt 0)

10+ Year Member



Hi,

I have not changed anything from the code is posted earlier. However i think that the problem is with the encoding of the attachment.PDF opens fine but MS Word opens with unreadable text and some it states that the file cannot be opened in MS DOS. It asks for me to specify how want the attachment encoded.This is dispite me indicating the application is msword.

Where do you think i am going wrong?

Please advice!

Saboi

9:04 am on Oct 18, 2007 (gmt 0)

10+ Year Member



Guys,

My problems still persists. Anyone figured out a work around?

PHP_Chimp

1:00 pm on Oct 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you run the script what output do you get? Is it one of php's error messages, is so what is the error, or do you get the mail but with a scrambled attachment.

Can you post either the error message or copy the output from the mail if it is working.

You have an elseif loop that doesnt seem to have a closing }
You have

$fileatt = $_FILES['attachment']['tmp_name']; // Path to the file
$fileatt_type = "application/msword"; // File Type
$fileatt_name = "attached"; // Filename that will be used for the file as the attachment
twice. Once above the first file comment and once below it.

To help more we do need to see the error message or result of the script.

Saboi

10:48 am on Oct 22, 2007 (gmt 0)

10+ Year Member



Hi,

Thanks for the reply.

I am getting the email as sent and there are no errors. Yes, the attachment is also received but scrambled. This is the case with word documents. However, pdf documents open just fine.

PHP_Chimp

5:35 pm on Oct 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Im just guessing as the header you are sending seems to be fine, but seeing as MS word can save a file as all sorts of different versions of a word file are you attaching an old version of a word file then trying to read it as a different version?

If this is a cross platform difficulty then try saving the word document as an .rtf version, as this should be compatible with most if not all programs that deal with text.

It may be helpful if you could give the actual output from the attachment, in its raw scrambled mess.

You are base64 encoding, have you tried without this encoding?
I know you are setting
"Content-Transfer-Encoding: base64\n\n" but have you tried not encoding and sending the attachment as is? Just in case there is some issue with encoding the MS word document then having word try to open the base64 encode version.

Saboi

7:58 am on Nov 19, 2007 (gmt 0)

10+ Year Member



Hello PHP_Chimp,

Did as you adviced and it work.It had to do with compatibility. I saved the documents in rtf and that worked just fine. But what do i do when i want to attached pdf files, without always getting to the source code?

Thanks,

Saboi