Hello,
I realize that this topic has been covered before in great depth but I can't seem to find the root of my problem. I am trying to send an email with an attachment. If I try to send the text message with the attachment the email body appears as garbled binary code. If I take the text message out of the email headers, it sends the email attachment but it is garbled and can not be read correctly.
Can someone please help? Here's the code I'm using. I've tried both this method and the chunk_split(base64_encode( methods of adding the binary stream.
<html>
<head>
<title>Test Email</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<?php
$username = "Me";
$message = "This is a test";
$filename = "Abstract_Files/testfile.pdf";
$Myfile = fopen($filename,"rb");
$filedata = fread($Myfile,filesize($filename));//implode(file($filename),'');
$to = 'here@test.com';
$from = 'there@test.com';
$mime_boundary = "ri(s8d9duhjkasrRUIERUIEREIU";
$headers = "From:$from\n";
$headers .= "To:$to\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;";
$headers .= " boundary=\"--".$mime_boundary."\";\n";
// readable message
$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$headers .= "Content-Transfer-Encoding: 7bit\n\n";
$headers .= $message."\n\n";
$headers .= "--$mime_boundary\n";
//attachments
$headers .= "Content-Type: application/pdf;\n";//octect-stream
$headers .= " name=\"$filename\"\n";
$headers .= "Content-Transfer-Encoding: "."base64;\n";
$headers .= "Content-Disposition: attachment\n";
$headers .= " filename=\"$filename\";\n";
$headers .= $filedata;
$headers .= "--$mime_boundary--\n";
// print($message."<BR><HR>".$headers."<br>");
$subject = "Submission from ".$username;
$success = mail($to,$subject,$message,$headers);
?>
<body>
<?php if ($success == true)
print("GOOD");
else
print ("Not Good");
?>
</body>
</html>