Problem with attachments

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Problem with attachments

Post by sulen »

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

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); 

}

?>
**edit by Infolock : added php code colors for eyecandy**
**edited by Infolock : Removed password from mysql connection.. bad, very very bad 8O **
Post Reply