edit: this question is different as i am specifically asking about the attachment feature not working in my code otherwise y php and html code run fine.
I have tried to create a submission form which attaches a file of any type and sends it through to my email. Unfortunately, it doesn't deliver to my email address.
here is my PHP & HTML code(I have removed my email address purposely):
<?php if (isset($_POST)) {
//form validation vars
$formok=true;
$errors=array();
$ipaddress=$_SERVER['REMOTE_ADDR'];
$date=date('d/m/Y');
$time=date('H:i:s');
$name=$_POST['name'];
$email=$_POST['email'];
$telephone=$_POST['telephone'];
$message=strip_tags($_POST['message']);
$attachment=chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename=$_FILES['file']['name'];
$boundary=md5(date('r', time()));
if (empty($name)) {
$formok=false;
$errors[]="You have not entered a name";
}
if (empty($email)) {
$formok=false;
$errors[]="You have not entered an email address";
}
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$formok=false;
$errors[]="You have not entered a valid email address";
}
if ($formok) {
$headers="From: " . $email ."\r\n";
$headers .="\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message="This is a multi-part message in MIME format.
--_1_$boundary Content-Type: multipart/alternative;
boundary=\"_2_$boundary\"
--_2_$boundary Content-Type: text/plain;
charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$message --_2_$boundary-- --_1_$boundary Content-Type: application/octet-stream;
name=\"$filename\"
Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --_1_$boundary--";
mail("fdgfdg@gfdg.com ", "New Submission", $emailbody, $headers);
}
if ($formok) {
$headers="From: gfdg@gfd.com" ."\r\n";
$headers .='Content-type: text/html; charset=iso-8859-1' ."\r\n";
$emailbody="<p>We have recieved your submission and will be in contact with you shortly.";
mail($email, "Thank You", $emailbody, $headers);
}
//what we need to return back to our form
$returndata=array('posted_form_data'=> array('name'=> $name,
'email'=> $email,
'telephone'=> $telephone,
'file'=> $attachment,
'message'=> $message),
'form_ok'=> $formok,
'errors'=> $errors);
//if this is not an ajax request
if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !=='xmlhttprequest') {
//set session variables
session_start();
$_SESSION['cf_returndata']=$returndata;
//redirect back to form
header('location: ' . $_SERVER['HTTP_REFERER']);
}
}
<form enctype="multipart/form-data" method="post" action="processsubmit.php">
<label for="name">Name: <span class="required">*</span>
</label>
<input type="text" id="name" name="name" value="" placeholder="John Doe" required="required" autofocus="autofocus" />
<label for="email">Email Address: <span class="required">*</span>
</label>
<input type="email" id="email" name="email" value="" placeholder="johndoe@example.com" required="required" />
<label for="telephone">Telephone:</label>
<input type="tel" id="telephone" name="telephone" value="" />
<label for="file">File</label>
<input type="file" name="file" id="file">
<label for="message">Message: <span class="required">*</span>
</label>
<textarea id="message" name="message" placeholder="Your message must be greater than 20 charcters" required="required" data-minlength="20"></textarea>
<span id="loading"></span>
<input type="submit" value="Holla!" id="submit-button" />
<p id="req-field-desc"><span class="required">*</span> indicates a required field</p>
</form>