Page 1 of 1

mail() attachment error - I need Help... Please

Posted: Thu May 24, 2007 5:19 am
by steve8557
I ahve some code the sending an attachement vial the mail() function and although the script executes OK and seems to be doing the right stuff the resulting email does not contain an attachement as such.
The attachement seems to be converted to text.
I really can't see what's wrong heer so any help would be apreciated.

Here's my code

Code: Select all

<?php
session_start();

$_SESSION['yname'] = $_POST['yname'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['phone'] = $_POST['phone'];
$_SESSION['message'] = $_POST['message'];
$_SESSION['cv'] = $_POST['cv'];

$yname = $_SESSION['yname'];
$email = preg_replace("//", "", $_SESSION['email']);
$phone = $_SESSION['phone'];
$message = $_SESSION['message'];
$cv = $_SESSION['cv'];



if(!$_SESSION['yname'] || !$_SESSION['email'] || !$_SESSION['phone'])
{
	$_SESSION['formMessage'] = "Please fill out all the required fields. <br />Fields marked with * are required.\n";
	Header("Location:./contact.php");
	exit();
}
else 
{

//$message = "Message From: $yname\n\nEmail Address: $email\n\nTelephone No.: $phone\n\n" . $message;


//////////////////////////////////////////////////////////////////////
//Handle the attachment

//create a random number for the attachment boundary
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; 

// Get the file attributes
$tmp_name = $_FILES['cv']['tmp_name'];
$type = $_FILES['cv']['type'];
$name = $_FILES['cv']['name'];
$size = $_FILES['cv']['size'];

// Chunk Split the data file so we can send it 

//if the file exists
if (file_exists($tmp_name))
{
	//and its an uploaded file 
	if(is_uploaded_file($tmp_name))
	{
		//then open the file 
		$file = fopen($tmp_name,'rb');
		//read it into a variable called data 
		$data = fread($file,filesize($tmp_name));
		//close the file 
		fclose($file);
		//split up the file into chunks 
		$theFile = chunk_split(base64_encode($data));
	}
}


//adding header info

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

//start constructing the message
/*
$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" . 
$message . "\n\n";
*/

//attach the file 
$message .= "--{$mime_boundary}\n" . "Content-Type: {$type};\n" . 
" name=\"{$name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$name}\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . $theFile . "\n\n" . 
"--{$mime_boundary}--\n"; 

/////////////////////////////////////////////////////////////////////


	$from = stripslashes($yname)."<".stripslashes($email).">";
	$to = "myemailaddress@somedomain.com";
	$subject= "Contact from: ".$_POST['yname'];
	$headers .=" From: $from\n";
	
	if(@mail($to, $subject, $message, $headers))
	{
		$_SESSION["formMessage"] = "Thank you, your email has been sent.";
		header("Location: ../contact.php");	}
	else
	{
		echo "Mail function Error";
	}

}
?>
Thanks
Steve

Posted: Thu May 24, 2007 5:27 am
by volka
I'd rather not code the mime-mail part "by hand" but use a tested mail class like swiftmailer.
see http://sourceforge.net/projects/swiftmailer and viewforum.php?f=52

Posted: Thu May 24, 2007 6:00 am
by steve8557
volka wrote:I'd rather not code the mime-mail part "by hand" but use a tested mail class like swiftmailer.
see http://sourceforge.net/projects/swiftmailer and viewforum.php?f=52
Thanks for the advice but it seems a shame to have to start again from scratch.

Is there any thing in my code that I'm doing wrong.

Cheers
Steve

Posted: Thu May 24, 2007 6:06 am
by Chris Corbyn
I can't see the start of $message. It certainly appears that you are just trying to append an attachment to the end of an existing email -- this won't work. You need to build the entire message right from the top headers using the correct content-types for the multipart data.

Your code is also exploitable for header injection attacks and will not work on windows servers which send over SMTP servers running qMail.

Posted: Thu May 24, 2007 10:38 am
by steve8557
Thanks guys,
I've now implemented Swift and its works a treat!

Cheers
Steve