Problem with attachments
Posted: Tue Mar 09, 2004 3:04 pm
I am trying to code a php mail function where the user can send an email with any file they would want to attach to it. This is the code that I am using. What it does is it encodes the attachment and puts it in the body of the email. Please tell me what I am doing wrong. Thanks in advance
**edit by Infolock : added php code colors for eyecandy**
**edited by Infolock : Removed password from mysql connection.. bad, very very bad
**
Code: Select all
<?php
//connecting to the database
@ $db = mysql_pconnect("localhost", "root", "--------");
//connection error
if(!$db)
{
echo "error could not connect to the database. please try again later";
exit;
}
//selecting database
mysql_select_db("axis");
$sql = "select * from email";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
for($i=0; $i <$numrows; $i++)
{
$row = mysql_fetch_array($result);
$email=$row[0];
$to = "webmaster@alturacs.com";
$subject = $sub;
// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
$message = $mssg;
$headers = "From: webmaster@alturacs.com\n";
$headers .= "Bcc: $email\n";
$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; 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: {$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";
mail($to, $subject, $message, $headers);
}
?>**edited by Infolock : Removed password from mysql connection.. bad, very very bad