Page 1 of 1

Sending multiple attachments

Posted: Thu Mar 11, 2004 1:48 pm
by sulen
I am trying to send multiple attachements in a single PHP mail() function.

This is the code I am using : (but fread and fclose are not getting a value arguments and the attachments are not being sent). I hope someone can help me on this one.

<?php
//connecting to the database
@ $db = mysql_pconnect("localhost", "root", "dbstuff3r");

//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];

$fileatt = ""; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = ""; // Filename that will be used for the file as the attachment

$email_message = $mssg; // Message that the email has in it
$email_to = $email; // Who the email is too
$email_from = "webmaster@alturacs.com"; // Who the email is from
$headers = "From: " .$email_from;
//$headers .= "Bcc: $email\r\n";
$email_subject = $sub; // The Subject of the email

$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}\"";

$email_message .= "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" .
$email_message . "\n\n";

/********************************************** First File ********************************************/


$fileatt = ""; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = ""; // 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));

$email_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";
unset($data,$file,$fileatt,$fileatt_type,$fileatt_name);

/********************************************** Second File ********************************************/

$fileatt = ""; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = ""; // 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));

$email_message .= "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,$file,$fileatt,$fileatt_type,$fileatt_name);

/********************************************** End of File Config ********************************************/

$ok = @mail($email_to, $email_subject, $email_message, $headers);

}
$sql1 = "delete from email";
$result1 = mysql_query($sql1);

Re: Sending multiple attachments

Posted: Thu Mar 11, 2004 1:56 pm
by TheBentinel.com
sulen wrote:

Code: Select all

$fileatt = ""; // Path to the file 
$fileatt_type = "application/octet-stream"; // File Type 
$fileatt_name = ""; // Filename that will be used for the file as the attachment 

$file = fopen($fileatt,'rb');
Did you leave out the path to the file? It sounds like your fopen is failing and passing in a blank path would probably do it.

If you didn't leave it out and just cut it out for the code you pasted here, how are you populating the $fileatt variable?

Posted: Thu Mar 11, 2004 2:05 pm
by sulen
I changed the code to this ........... but still it sends only one attachment

/******** First File *************/


$fileatt_type = "application/octet-stream"; // File Type


$file = fopen($_FILES["fileatt"]["tmp_name"],"rb");
$data =
fread($file,filesize($_FILES["fileatt"]["tmp_name"]));
fclose($file);

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

$email_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";
unset($data,$file,$fileatt,$fileatt_type,$fileatt_name);

/****************** Second File ***********************/

$fileatt_type = "application/octet-stream"; // File Type

$file = fopen($_FILES["fileatt"]["tmp_name"],"rb");
$data =
fread($file,filesize($_FILES["fileatt"]["tmp_name"]));
fclose($file);

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

$email_message .= "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,$file,$fileatt,$fileatt_type,$fileatt_name);

/********************************************** End of File Config ********************************************/

$ok = @mail($email_to, $email_subject, $email_message, $headers);

Posted: Thu Mar 11, 2004 2:24 pm
by TheBentinel.com
sulen wrote:I changed the code to this ........... but still it sends only one attachment
Before the change, you were getting an error. Are you still getting the error, or did the error go away and now you're getting just one attachment?

Your code looks like it's using the same file for both attachments. Maybe you should be saying:

Code: Select all

// First file
$file = fopen($_FILES["fileatt"]["tmp_name"][0],"rb"); 

// ... code ...

// Second file
$file = fopen($_FILES["fileatt"]["tmp_name"][1],"rb");
Just guessing on that one, but it seems reasonable that you need to use two different file names to create the attachments.

Posted: Thu Mar 11, 2004 3:24 pm
by sulen
I have now created 2 separate file upload text areas in my form........thereby having two separate variables...............now the 2 attachments are getting sent...........but with the attachments I have a .DAT file also being sent which I dont know why.

If u see my original code,

the line - $headers .= "Bcc: $email\r\n"; is commented. The main reason I am building this is to send attachments to mutiple recipients in the bcc field. If I put the same list in the To: field it works fine but bcc: screws up the attachments. Thanks for any help in advance.

This is how my code looks now

/**************** First File ******************/

$fileatt_type = "application/octet-stream"; // File Type

$file = fopen($_FILES["fileatt"]["tmp_name"],"rb");
$data = fread($file,filesize($_FILES["fileatt"]["tmp_name"]));
fclose($file);

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

$email_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";

unset($file,$data);

/**************Second File ***************/

$fileatt1_type = "application/octet-stream"; // File Type

$file = fopen($_FILES["fileatt1"]["tmp_name"],"rb");
$data = fread($file,filesize($_FILES["fileatt1"]["tmp_name"]));
fclose($file);

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

$email_message .= "Content-Type: {$fileatt1_type};\n" .
" name=\"{$fileatt1_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";

//unset($data,$file,$fileatt,$fileatt_type,$fileatt_name);

/*********** End of File Config *******************/

$ok = @mail($email_to, $email_subject, $email_message, $headers);