Page 1 of 1

Sending attachement to Bcc

Posted: Tue Mar 09, 2004 5:01 pm
by sulen
I have a mail script to send attachments that works perfectly when the addreses are in the to: field. But if the same addresses are in the bcc: it mails the attachment as garbled text along with the body of the email. I am also pasting my code. Any help will be appreciated. Thanks

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

if($_FILES["fileatt"] != NULL)
{
$fileatt_type = "application/octet-stream"; //File Type
$email_from = "webmaster@alturacs.com"; // Who the email is from
$email_subject = $sub; // The Subject of the email
$email_message = $mssg; // Message that the email has in it

$email_to = $email; // Who the email is too

$headers = "From: ".$email_from;
//$headers .= "Bcc: webmaster@alturacs.com\n";

$file = fopen($_FILES["fileatt"]["tmp_name"],"rb");
$data =
fread($file,filesize($_FILES["fileatt"]["tmp_name"]));
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}\"";

$email_message .= "This is a multi-partmessage 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";

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

//$email_message = $email_message.$email_txt;

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

}
}
?>

Posted: Tue Mar 09, 2004 5:06 pm
by markl999
Tried...

headers = "From: ".$email_from."\r\n";
$headers .= "Bcc: webmaster@alturacs.com\r\n";

?

Posted: Tue Mar 09, 2004 5:08 pm
by sulen
I did not understand what u meant............i commented that line as it doesnt work but if the line is included it messes up the attachement and doesnt send it to anybody.

Posted: Tue Mar 09, 2004 5:11 pm
by markl999
Well you should separate headers with \r\n and you wern't doing that, so that was the first thing to try, if you tried my suggestion and it still didn't work then we can rule that out and move on to trying something else.

Posted: Tue Mar 09, 2004 5:15 pm
by sulen
I tried that but it still doest work

I am goin crazy with this crap..................

Posted: Tue Mar 09, 2004 5:15 pm
by markl999
What version of PHP are you using and on what OS/webserver? (windows, *nix, apache, IIS etc..) ?

Posted: Tue Mar 09, 2004 5:18 pm
by sulen
Version : PHP Version 4.3.2
Webserver : Apache
OS : Linux

Posted: Tue Mar 09, 2004 5:33 pm
by markl999
Well, i'm all out of ideas then, but i suspect malformed headers.
I tend to cheat and use something like phpmailer for sending 'complex' emails.