php form, send email with an attachment
I need some help with this form please. i've already created a form and
everything is working, but when I submit the email is recived perfectly
and i don't have any problems with it. my only problem is that i need to
add an upload field and when the user submit they receive it as an
attachment, i've looked at many examples but i couldnt make it work, i
really need this to work as soon as possible, if anyone has any ideas of
how it could make it work it would be so great. also i've always came to
this website to look for solutions and its very great and this is my first
question here, i hope i could find the answer and if anyone else is
looking for the same thing then they can be helped as well thank you
this is the form
<form name="contactform" method="post" action="form.php">
<table width="450px">
<tr>
<td><label for="first_name">Title *</label></td>
<td><input type="text" name="first_name" maxlength="4" size="4"></td>
</tr>
<tr>
<td><label for="last_name">last Name *</label></td>
<td><input type="text" name="last_name" maxlength="50" size="30"></td>
</tr>
<tr>
<td><label for="email">Email Address *</label></td>
<td><input type="text" name="email" maxlength="80" size="30"></td>
</tr>
<tr>
<td><label for="telephone">Telephone Number *</label></td>
<td><input type="text" name="telephone" maxlength="30" size="30"></td>
</tr>
<tr>
<td><label for="what_trade">What trade *</label></td>
<td><input type="text" name="what_trade" maxlength="50" size="30"></td>
</tr>
<tr>
<td><label for="cscs">CSCS Card Number *</label></td>
<td><input type="text" name="cscs" maxlength="50" size="30"></td>
</tr>
<tr>
<td><label for="file">CV upload*</label></td>
<td><input type="file" name="attachment"></td>
</tr>
<tr>
<td><center><input type="submit" value="Submit"></center></td>
</tr>
</table>
</form>
and this is the php code... the only thing am missing in here is the
attachment code, please thats the only part i need, if you could only help
me with that part it would be really great
<?php
if(isset($_POST['email'])) {
$email_to = "someemail@website.com";
$email_subject = "Operative form";
function died($error) {
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['what_trade']) ||
!isset($_POST['cscs'])) {
died('We are sorry, but there appears to be a problem with the form
you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$what_trade = $_POST['what_trade']; // required
$cscs = $_POST['cscs']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be
valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be
valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be
valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Trade: ".clean_string($what_trade)."\n";
$email_message .= "CSCS Card Number: ".clean_string($cscs)."\n";
"some code for the sending the file upload as an attachment"
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<?php
}
?>
No comments:
Post a Comment