View Full Version : E-Mail with attachment

11-02-2009, 11:54 AM
//Set up email and attachment details
$to = "myemail@email.com";
$subject = "Application Form";

// Store the file information to variables for easier access
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
$job = $_POST["job"];
$salutation = $_POST["salutation"];
$fromename = $_POST["fromname"];
$address = $_POST["address"];
$phone = $_POST["phone"];
$fromemail = $_POST["fromemail"];
$about = $_POST["about"];

//Form contents
$message = "You have a job enquiry: $name \n
Job Required: $job \n
Salutation: $salutation \n
From: $fromename \n
Address: $address \n
Telephone: $phone \n
E-mail: $fromemail \n
About yourself: $about";

//Read in the attachment
if (file_exists($tmp_name)){
$file = fopen($tmp_name, 'rb');
$data = fread($file, filesize($tmp_name));
//Encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));

//Add the MIME content
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x".md5(mt_rand())."x";
$headers = "From: $fromemail\r\n".
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .

//Message header
$message = "This is a multi-part 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";

// Insert boundary to indicate we're starting the attachment
// Specify the content type, file name and disposition as an attachment.
// Then add the file content and set another boundary to indicate that the end of the file has been reached
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
//Send the e-mail
mail($to, $subject, $message, $headers);


Can anyone help me get this working? If I submit it without an attachment it works, as soon as I add an attachment it sends it but nothing appears in the sent e-mail. I think it is something to d with the uploading of the files…

Any help greatly appreciated!

11-03-2009, 06:36 PM
C'mon someone must have an idea?

Pretty please :)

11-04-2009, 12:03 AM
Post your HTML code for your form that's sending this data. The problem might be there, and might be very simple fix. THe php code looks correct.

11-04-2009, 04:20 AM
Execute this to check if mail() is enabled:


if (function_exists('mail'))
echo "Its enabled, so its something else...";
echo "Function is disabled";


Also post your html, make sure your html form fields name tags all match to their correct $_POST variables.

11-04-2009, 04:25 AM
Getting the MIME encoding right is not easy. It is better to use a library like Pear Mime encoding. See the page below for an example:
How to create PHP email form with file attachment (http://www.html-form-guide.com/email-form/php-email-form-attachment.html)