mail() attachment error - I need Help... Please
Posted: Thu May 24, 2007 5:19 am
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
Thanks
Steve
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";
}
}
?>Steve