php: send mail with attachment?

  • Thread starter Thread starter cutegirl666888222
  • Start date Start date
C

cutegirl666888222

Guest
I wrote a php code to send email with attachment, my problem is that the format for the text dont work anymore when I added the code for attachment, I mean <br>, \n dont work, please help

here is the code:


$fileatt = "mypdffile.pdf";
$fileatt_type = "application/pdf";
$fileatt_name = "mypdffile.pdf";

$subject = "Your attached file"; // The Subject of the email
ob_start();
require("emailbody.php"); // i made a simple & pretty page for showing in the email
$body=ob_get_contents(); ob_end_clean();
$description = "Thanks for visiting mysite.com! <br>Here is your free file.<br>";
$description .= "Thanks for visiting.<br> come again";
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$subject .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

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

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

$description .= "--{$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";
 
Ok 2 things i notice are wrong,

1) You used the description variable twice, using it twice doesn't assign it two variables, it replaces the first with the second, be careful of that

2) the <br> tag is deprecated, and depending what browser or e mail reader theyre using it may or may not show up because it is an unended tag. Instead use <br />. they both have the same exact effect.

Not completely sure on what \n means O.o in the two years ive been writing PHP i've never heard of it! Sorry!

Hope this helps!!
 
Back
Top