Reputation: 63
I've got an HTML sign up/contact form that currently sends the fields in an email, no problem. The problem I'm having is attaching an uploaded file to the email.
With the PHP I'm using at the moment for the file, the email is sending, but the MIME information is within the email and I got an error indicating that the message is 'badly structured and could not be fully examined' and the file isn't attached to the email.
<?php
if (isset($_POST['submit'])) {
//if ($_SERVER['submit']=="POST"){
$to = "[email protected]"; // Email address
$from = $_POST['text_96']; // sender's Email
$fullname = $_POST['text_95']; // users fullname
$email = $_POST['text_96']; // users email address
$dob = $_POST['date_12']; // Date of birth
$addr = $_POST['textarea_31']; // Address
$maths = $_POST['text_93']; // Maths
$english = $_POST['text_31']; // English
$holidayst = $_POST['date_74']; // Holiday1
$holidayend = $_POST['date_91']; // Holiday2
$distance = $_POST['number_58']; // Dist
$awayhome = $_POST['selectlist_90']; // AFW [boolean]
$reasontext = $_POST['textarea_60']; // 100 word answer
$subject = "New application submission"; // subject of email
$mssg = "New submission from " . $email . "\n\n" . "Full name: " . $fullname . "\nDOB: " . $dob . "\nAddress:\n" . $addr . "\n\nMaths: " . $maths . "\nEnglish: " . $english . "\nHoliday start: " . $holidayst . "\nHoliday end: " . $holidayend . "\nDistance to travel: " . $distance . "km" . "\nAFH?: " . $awayhome . "\nAppr choices: " . $apprent . "\n\nReason for applying: " . $reasontext . "\n";
foreach ($_POST['checkbox'] as $key => $val) { // Gets list of appr
$apprent .= $val . ' '; //
}
// -- -- -- \\
$fileatt = $_FILES['file'];
//$fileatttype = "application/pdf";
//$fileattname = "newname.pdf";
$headers = "From: $from";
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$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";
$data = chunk_split(base64_encode($data));
$message .= "–{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $mssg .
$data . "\n\n" .
"-{$mime_boundary}-\n";
if (mail($to, $subject, $message, $headers)) {
$message = NULL;
$headers = NULL;
header('Location: application_submitted.html');
} else {
header('Location: index.php');
//\\ -- -- -- //
//$headers = "From:" . $from;
//@mail($to,$subject,$message,$headers);
// You can also use header('Location: thank_you.php'); to redirect to another page.
//$message = NULL;
//$headers = NULL;
//header('Location: application_submitted.html');
//if (@mail($to, $subject, $message, $headers))
//$message = NULL;
//$headers = NULL;
//header('Location: application_submitted.html');
//else
//echo "Failed to send";
}
}
?>
How can I attach a file to this email and send it without getting the error explained above, please?
Many thanks for any help and assistance with this!
Upvotes: 0
Views: 213
Reputation: 1287
Your code is too broken to be fixed here, but here are two main errors for you to focus on:
Firstly, you do not reference an uploaded file using $_FILES['file']
, its location is stored in $_FILES['file']['tmp_name']
. Here is a useful tutorial: PHP 5 File Upload on W3schools.
Secondly, headers should be separated using \r\n
. The mail() function documentation provides useful information on this, along with security tips.
Upvotes: 1