Page 1 of 1

Attaching a file with php

Posted: Fri Jan 02, 2009 6:51 pm
by ancorso
here is my php code to attach a file with php, when it sends to my email it works perfectly, but when it sends to either of my parents the body is not blank and the attachment is in the body excepts in code:

<?php
if (isset($_SESSION['login']))
{
$subject = $_POST['subject'];
$body = $_POST['body'];
$from = $_POST['from'];
$fileattname = $_POST['fileattname'];
if (trim($subject == '') || trim($body=='') || trim($from=='') )
{
echo "<h2>Sorry, You must enter all of the information</h2>\n";
echo "<br><a href='index.php?content=email'>Return to mail</a>";
}
else
{
include("includes/connect.inc");
$query = "select fname, email from users";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
$name = $row['fname'];
$to = $row['email'];

$fileatt = "attachments/".$fileattname;
$fileatttype = "application/pdf";

$headers = "From: $from";
$file = fopen( $fileatt, 'rb' );
$data = fread( $file, filesize( $fileatt ) );
fclose( $file );
$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 = "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";

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

$message .= "--{$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";
if( mail( $to, $subject, $message, $headers ) )
{
echo "<p>The email was sent to $name.</p>";
}
else
{
echo "<p>There was an error sending the mail to $name.</p>";
}
}
}
}
else
{
echo "<p>Sorry you have to be logged in to view this page</p>";
}
?>





if anyone could help that would be great. *the $message variable is showed in the body of my parents but is not showed in mine. they use outlook, and i use windows mail

Re: Attaching a file with php

Posted: Fri Jan 02, 2009 9:18 pm
by califdon
It looks to me as if you are sending it as MIME, which would be inline, not attached separately, so perhaps the reason that you receive it attached is due to the way your mail client is configured, refusing inline MIME. It's all controlled by those headers, so if you really want it to be a particular way, you need to find the reference for those headers and use the appropriate one(s).