I have a simple form on a web page. I want the person who fills in the form to be able to upload a file. Then I want the message and attached file to be e-mailed to me. I have been playing with lots of bits of code for days now, but haven't found anything that works for me.
My current code:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$flags = 'style="display:none;"';
$message = $_POST['message'];
$email="me@me.com";
$to = "someone@somewhere.com";
$subject = "test Message with attachment";
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['uploaded_file'])));
$filename = $_FILES['file']['uploaded_file'];
$boundary =md5(date('r', time()));
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: ".$email."\r\n";
$headers .= "Reply-To: ".$email."\r\n" .
$mail_sent = @mail($to, $subject, $message, $headers);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
exit();
} // end of what happens if form submitted
else{
?>
<!-- end of form processing-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test e-mail attachment form</title>
</head>
<body>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
<input type="hidden" name="status" value="sent">
<table >
<tr>
<td>
Message:
</td>
<td>
<input type="text" name="message" size="20" required>
</td>
</tr>
<tr>
<td>
Attach a file:
</td>
<td>
<input type="file" name="uploaded_file">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Send message">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
?>
The message comes through OK, but there is no attachment. I have tried lots of different methods, including a couple found on here, like [Simple PHP form: Attachment to email (code golf), but cannot get what I want.
I also tried something I found at: [http://form.guide/email-form/php-email-form-attachment.html][1], but that involved installing PEAR. I did try that but then discovered it had overwritten my home page, so I had to uninstall it, as it seems to be too complicated and overkill for what I need.
I feel sure that I must be somewhere near getting this to work, but cannot see what is wrong.