Hi
I am trying to create a web page that gives a user the oportunity to email an attachment
to a single preset email address.
The code currently does not show any errors however no attachment is sent.
There is 3 files involved in the process a main file containing the form and 2 other included files

Main file containing form

send_email.php

error_reporting(E_ALL); 
include('core/init.inc.php') or die(mysql_error());

if(isset($_POST['name'], $_FILES['file'])){
$body = <<<BODY
From:{$_POST['name']}
Details:
Name: {$_FILES['name']}
Size: {$_FILES['size']}
Type: {$_FILES['type']}
BODY;
mail_file('michael@email.fsnet.co.uk', 'michael@email.fsnet.co.uk', 'A File Upload', '$body', $_FILES['file']);
}

?>
<html>
<head></head>
<title></title>
<body>
<div>
<form>
<p>
<label for="name">Name</label>
<input type="text" name="name" id="name" /></p>
<p>
<label for="file"></label>
<input type="file" name="file" id="file" /></p>
<p>
<input type="submit" value="Email file" /></p>
</form>
</div>
</body>
</html>

Included file

mail.inc.php

error_reporting(E_ALL); 
function mail_file($to, $from, $subject, $body, $file){
$boundary = md5(rand());
$headers = array(
'MIME-Version: 1.0', 
"Content-Type: multipart/mixed: boundary=\'{$boundary}\'", 
"From: {$from}"
);

$message = array(
"--{$boundary}",
'Content-Type: text/plain',
'Content-Transfer-Encoding: 7bit',
'', 
chunk_split($body),
"--{$boundary}",
"Content-Type: {$file['type']}; name=\"{$file['name']}\"",
"Content-Disposition: attachment; filename=\"{$file['name']}\"",
"Content-Transfer-Encoding: base64",
'', 
chunk_split(base64_encode(file_get_contents($file['tmp_name']))),
"--{$boudary}--"
);
mail($to, $subject, implode("\r\n", $message), implode("\r\n", $headers));

Included file

init.inc.php

error_reporting(E_ALL); 
$path = dirname(__FILE__);

include("{$path}/inc/mail.inc.php") or die(mysql_error());

Best regards Maxwel

    do your self a favour don't use php's very under powered mail() function use phpmailier (http://phpmailer.worxware.com/) or one of the others.

    $_FILES["file"] is not the field you want here, its an array

      Write a Reply...