Forum Moderators: coopster
<?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]
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.
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!
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 filetwice. Once above the first file comment and once below it.
$fileatt_type = "application/msword"; // File Type
$fileatt_name = "attached"; // Filename that will be used for the file as the attachment
To help more we do need to see the error message or result of the script.
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.