Add attachment to PHP email script

Hi, I have a very simple email script where a user fills in the data fields and the data is emailed to the the email address in a text based email.

I want to add 2 extra fields for adding attachments (docuemnt and image).

Here is the code to add the data from the text fields, and how the script works…

<?php $my_email = "name@domain.com";

$continue = "/";

$errors = array();

// Remove $_COOKIE elements from $_REQUEST.

if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}

// Build message.

function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}

$message = build_message($_REQUEST);

$message = stripslashes($message);

$subject = "General Enquiry";

$subject = stripslashes($subject);

$from_Name = "";

if(isset($_REQUEST['Name']) && !empty($_REQUEST['Name'])){$from_Name = stripslashes($_REQUEST['Name']);}

$headers = "From: {$from_Name} <{$_REQUEST['Email']}>";

mail($my_email,$subject,$message,$headers);

$message = "Thank you for your enquiry. We will be in touch with you as soon as possible.

Regards,

Company Name
http://www.domain.com";

$subject = "General Enquiry";

$headers = "From: site_name <noreply@domain.com>";
?>

If any one has any suggestions as to what might needs to be added/modified in the code I would greatly appreciate it.

Thanks,

Paul

These tutes show how to send plain text emails, html emails and emails with attachments usimg php mail()

I would recommend to use PHPMailer which is a well tested and widely used mailer script. Please follow the tutorials and [URL=“http://phpmailer.worxware.com/index.php?pg=exampleamail”]examples there in the page.

Thanks for the reply guys.

I have found a some attachment code, I tried adding it to the PHP code for the email form I already have.

When I send an enquiry no errors show and the correct page loads. - I might be doing some wrong in the code but not 100% sure as its not menitoning any errors. - here is the code I put together, if anyone can spot an error I would really appreciate it.

<?php

$my_email = "name@domain.com";

$continue = "/";

$errors = array();

// Remove $_COOKIE elements from $_REQUEST.

if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}

// Attachment

function email_attachment($my_email, $file_location, $default_filetype='application/zip') {

$fileatt = $file_location;
    if(function_exists(mime_content_type)){
        $fileatttype = mime_content_type($file_location);
    }else{
        $fileatttype = $default_filetype;;
    }
    $fileattname =$_REQUEST['attachment']($file_location);
    //prepare attachment
    $file = fopen( $fileatt, 'rb' );
    $data = fread( $file, filesize( $fileatt ) );
    fclose( $file );
    $data = chunk_split( base64_encode( $data ) );
    //create mime boundary
    $semi_rand = md5( time() );
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Build message.

function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}

$message = build_message($_REQUEST);

$message = stripslashes($message);

$message = "This is a multi-part message in MIME format.\
\
" .
"--{$mime_boundary}\
" .
"Content-type: text/html; charset=us-ascii\
" .
"Content-Transfer-Encoding: 7bit\
\
" .

//create attachment section
$message .= "--{$mime_boundary}\
" .
"Content-Type: {$fileatttype};\
" .
" name=\\"{$fileattname}\\"\
" .
"Content-Disposition: attachment;\
" .
" filename=\\"{$fileattname}\\"\
" .
"Content-Transfer-Encoding: base64\
\
" .
$data . "\
\
" .
"--{$mime_boundary}--\
";

$subject = "General Enquiry";

$subject = stripslashes($subject);

$headers .= "Date: ".date("r")."\
";

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

$from_Name = "";

if(isset($_REQUEST['Name']) && !empty($_REQUEST['Name'])){$from_Name = stripslashes($_REQUEST['Name']);}

$headers = "From: {$from_Name} <{$_REQUEST['Email']}>";

mail($my_email,$subject,$message,$headers);

$message = "Thank you for your enquiry. We will be in touch with you as soon as possible.

Regards,

Company Name
http://www.domain.com";

$subject = "General Enquiry";

$headers = "From: Company Name <noreply@domain.com>";

mail($_REQUEST['Email'],$subject,$message,$headers); }

?>

Thanks.

Paul