Attaching a file with php
Posted: Fri Jan 02, 2009 6:51 pm
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
<?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