Hi all,

I re-post my previous thread I sent 2 days ago, because I am still having this problem with a php email application:
the code works fine, sends email messages and picks up the attachment
and its path as well, but instead of placing the attachment to the attachment part
of the mail it is displayed in the body as a gibberish, nonsense text.
I read lots of online threads about it, but have not found the solution for this
problem yet.
Could anyone help me, please? I am new to php, using Windows Vista at home
if that helps.

Many thanks
cycleFun

P.S.
This is my code:

<?php
/* ------------------------------------------------------
mm564_lib.php script
/* ------------------------------------------------------


/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $_POST['recipient'];
$subject = $_POST['subject'];
$message = $_POST['messagebox'];

//Declaring MIME boundary
$mime_boundary = "==Multipart_Boundary_x".md5(time())."x";

// Building message headers
$headers = "Haho";

$headers .= "\nMIME-Version:1.0\n".
			"Content-Type:multipart/mixed;\n".
			"boundary=\"{$mime_boundary}\"";

$message .= "This is a multipart message in MIME format.\n\n".
			"--{$mime_boundary}\n".
			"Content-Type: text/plain;charset=\"iso-8859-1\"\n".
			"Content-Transfer-Encoding:7bit\n\n".
			$message . "\n\n";


foreach($_FILES as $attachment)
{
	//File upload variables
	$tmp_name = $attachment['tmp_name'];
	$name = $attachment['name'];
	$type = $attachment['type'];
	$size = $attachment['size'];

if (file_exists($tmp_name))
{

	if (is_uploaded_file($tmp_name))
	{
		$file = fopen($tmp_name,'rb');
		$data = fread($file, filesize($tmp_name));
		fclose($file);
		$data = chunk_split(base64_encode($data));

	}else
	{
		echo "No attachment";
	}


//Inserting a boundary to indicate the attachment start
$message .= "--{$mime_boundary}\n".
			"Content-Type:{$type};\n".
			"name=\"{$name}\"\n".
			"Content-Disposition:attachment;\n".
			"filename=\"{$tmp_name}\"\n".
			"Content-Transfer-Encoding:base64\n\n".
			$data."\n\n".
			"--{$mime_boundary}--\n";

}

}

$message.= "--{$mime_boundary}--\n";

/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email))
{
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "")
{
echo "<h4>No subject</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}

/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($email,$subject,$message,$headers))
{
echo "<h4>Thank you for sending email</h4>";
} else
{
echo "<h4>Can't send email to $email</h4>";
}
?>

And this is the result:

Haho
MIME-Version:1.0
Content-Type:multipart/mixed;
boundary="==Multipart_Boundary_xd41d2e728b037dd377a6273f72ef3ac6x"

bdfbThis is a multipart message in MIME format.

--==Multipart_Boundary_xd41d2e728b037dd377a6273f72ef3ac6x
Content-Type: text/plain;charset="iso-8859-1"
Content-Transfer-Encoding:7bit

bdfb

--==Multipart_Boundary_xd41d2e728b037dd377a6273f72ef3ac6x
Content-Type:application/msword;
name="Dissertation Proposal.doc"
Content-Disposition:attachment;
filename="/tmp/phppgVTGR"
Content-Transfer-Encoding:base64

0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAAMwAAAAAAAAAA
EAAANQAAAAEAAAD+////AAAAADIAAAD/////////////////////////////////////////////
/////////////////////////////////////

Dani AI

Generated

The output shown by (base64 blob visible in the message body) indicates the MIME structure or headers are malformed. Common causes are LF-only line breaks, boundary lines placed or closed incorrectly, using the PHP temp filename as the attachment filename, and building the message and headers in the same variable (which can inject text into the wrong place). 's suggestion to check the script source is reasonable, but a few concrete fixes usually resolve this.

Key fixes and a minimal example pattern (use CRLF, separate header/body, use original filename, close the boundary only once):

$boundary = "==Multipart_".md5(uniqid(time()));
$headers  = "From: sender@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";

$body  = "This is a multipart message in MIME format.\r\n\r\n";
$body .= "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $plainTextMessage."\r\n\r\n";

foreach ($_FILES as $f) {
    if (!empty($f['tmp_name']) && is_uploaded_file($f['tmp_name'])) {
        $data = chunk_split(base64_encode(file_get_contents($f['tmp_name'])));
        $filename = basename($f['name']);
        $mime = !empty($f['type']) ? $f['type'] : 'application/octet-stream';

        $body .= "--$boundary\r\n";
        $body .= "Content-Type: $mime; name=\"$filename\"\r\n";
        $body .= "Content-Disposition: attachment; filename=\"$filename\"\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $body .= $data . "\r\n\r\n";
    }
}

$body .= "--$boundary--\r\n";

mail($to, $subject, $body, $headers);

Troubleshooting checklist: confirm headers and parts use "\r\n" (especially on Windows), ensure the header string begins with a valid From: line, do not append the terminating --boundary-- inside the file loop, use basename() for the filename, and view the raw/original message in the mail client to inspect boundaries and headers. If manual MIME assembly is error-prone, switch to a maintained library (PHPMailer/SwiftMailer) to avoid low-level bugs.

Member Avatar for Member #120589

Is this your script or somebody else's? If it's a script you picked up from somewhere, try the author or dedicated help forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.