I have an existing html contact form, but I would like to add the feature to upload a photo to be sent as an attachment. How can I add that to my php script. I have already added the file upload field in the html form name=datafile and I have also set the attribute enctype="multipart/form-data"
<?php
$yourEmailAddress = '123@123.com'; //Put your own email address here.
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = $yourEmailAddress;
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nMessage:\n $comments";
$headers = 'From: Interion Template <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
echo'<div id="success" class="sent success"><p><strong>Email Successfully Sent!</strong><br>Thanks for contacting Us. Your email was successfully sent and we \'ll be in touch with you soon.</p></div>';
} else { //If errors are found
echo '<p class="error">Please check if you\'ve filled all the fields with valid information and try again. Thank you.</p>';
}
?>
Here is my HTML part
<form method="post" action="sendMail.php" enctype="multipart/form-data" id="contactform">
<div class="response"> </div>
<p ><label for="contactname">What is your full legal Name?<span>*</span></label>
<input id="contactname" type="text" value="" name="contactname" placeholder="Full Name" class="textflied">
<i class="icon fa fa-user"></i></p>
<p><label for="email" >What is the best email address for us to contact you on?<span>*</span></label>
<input id="email" type="text" value="" name="email" placeholder="Email Address" class="textflied">
<i class="icon fa fa-envelope"></i></p>
<p><label for="subject" >Now tell us the best phone number to reach you at:<span>*</span></label>
<input id="subject" type="text" value="" name="subject" placeholder="Your phone number including area code" class="textflied">
<i class="icon fa fa-phone "></i></p>
<p><label for="message" >In a short paragraph how would you discribe your self?<span>*</span></label>
<textarea id="message" type="text" name="message" value="" placeholder="Your short paragraph" rows="8" class="texttextarea"></textarea>
<i class="icon fa fa-comments"></i></p>
<label for="datafile" >Finally lets match the name with a face, upload your most recent photo<span>*</span></label>
<input type="file" name="datafile" size="40">
<br>
<br>
<p>
<button type="submit" name="submit" id="submitButton" title="Click here to submit your message!" class="btn btn-disabled">SEND message</button>
</p>
</form>