Hi,

  • In my site I have a form which anyone can submit details via e-mail.
  • Since I want to get more details, I leave a space to upload a document.then visitor can browse & upload the details.

Please can anyone tell me how can I receive the uploaded document as a e-mail attachment???
This is my code. please help me by modifying my code to receive the document as e-mail attachment.

code from contact.html

<form method="post" action="sendeail.php">
Your Name: <br />
<input type="text" name="visitor" size="35" />
<br />
Your Email:<br />
<input type="text" name="visitormail" size="35" />
<br />
Please attach your details:<br>
<input type="file" name="attachment" size="23">
<br />
Mail Message:
<br />
<textarea name="notes" rows="4" cols="40"></textarea>
<br />
<br />
<input type="submit" value="Send Mail" />
<br />
</form>

code from sendeail.php

<?php

$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];

$todayis = date("l, F j, Y, g:i a") ;
$attn = $attn ;
$subject = $attn;
$notes = stripcslashes($notes);
$message = " $todayis [EST] \n

Attention: $attn \n
Message: $notes \n
From: $visitor ($visitormail)\n";
$from = "From: $visitormail\r\n";

mail("hadunperera@yahoo.com", $subject, $message, $from);

?>

please help me. thank you

Dani AI

Generated

Short answer: the form needs enctype="multipart/form-data", the uploaded file arrives in $_FILES (not $_POST), and mail() will not magically attach a file — you must build a multipart/mixed message (or use a library such as PHPMailer). ’s form HTML is missing the enctype and the PHP reads only $_POST; that’s why attachments never arrive. and pointed to resources — here is a concise, safe workflow and a minimal PHP example to build a proper multipart email with an attachment.

Example (core parts — validate, read, encode, build MIME body, then mail):

<?php
$visitor = substr(strip_tags($_POST['visitor'] ?? 'Anonymous'),0,255);
$visitormail = filter_var($_POST['visitormail'] ?? '', FILTER_VALIDATE_EMAIL);
$notes = strip_tags($_POST['notes'] ?? '');

if (!$visitormail) { exit('Invalid sender email'); }

// basic attachment check
if (!empty($_FILES['attachment']) && $_FILES['attachment']['error'] === UPLOAD_ERR_OK) {
  $tmp = $_FILES['attachment']['tmp_name'];
  $name = str_replace(["\r","\n"],'',basename($_FILES['attachment']['name']));
  $type = $_FILES['attachment']['type'] ?: 'application/octet-stream';
  $data = chunk_split(base64_encode(file_get_contents($tmp)));

  $boundary = md5(uniqid());
  $headers = "From: {$visitor} <{$visitormail}>\r\n"
           . "MIME-Version: 1.0\r\n"
           . "Content-Type: multipart/mixed; boundary=\"{$boundary}\"\r\n";

  $body  = "--{$boundary}\r\n"
         . "Content-Type: text/plain; charset=\"utf-8\"\r\n\r\n"
         . $notes . "\r\n\r\n"
         . "--{$boundary}\r\n"
         . "Content-Type: {$type}; name=\"{$name}\"\r\n"
         . "Content-Transfer-Encoding: base64\r\n"
         . "Content-Disposition: attachment; filename=\"{$name}\"\r\n\r\n"
         . $data . "\r\n"
         . "--{$boundary}--\r\n";

  mail('you@domain.tld', 'Subject here', $body, $headers);
} else {
  mail('you@domain.tld', 'Subject here', $notes, "From: {$visitor} <{$visitormail}>\r\n");
}
?>

Notes and quick troubleshooting:

  • Add enctype="multipart/form-data" to the form tag.
  • Check php.ini: file_uploads, upload_max_filesize, post_max_size, and ensure tmp directory writable. Use var_dump($_FILES) while debugging.
  • Always validate/sanitize filenames and emails to avoid header injection and do not trust MIME type sent by browser. Enforce size/type limits.
  • For reliability and ease (SMTP, attachments, authentication), prefer a maintained library: PHPMailer (). For PHP docs see the file-upload and mail pages: https://www.php.net/manual/en/features.file-upload.post-method.php and https://www.php.net/manual/en/function.mail.php.

Recommended Answers

All 2 Replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.