Ciao a tutti,
premettendo che sono alle prime armi con il php; ho realizzato un form contatti, dove c'è da inserire alcune info e allegare il curriculum.
Il primo problema è: tutti i campi sono obbligatori, ho fatto tutti i controlli del caso; lo anche testato ed è impossibile inviare l'email se non hai compilato tutti i campi; nonostante questo, mi arrivano, "sporadicamente" delle email non contenente nulla; cioè, visualizzo l'oggetto, il mittente dell'email ma è come se l'email non si fosse composta. Non so se mi sono spiegata!!! :-)
Un altro problema e che l'email ogni tanto le ricevo doppie; quindi sono le persone che mi mandano due volte i loro curriculum o sto sbagliando qualcosa?
Vi posto il mio codice. Fatemi sapere qualcosa!!!Grazie mille a tutti....
Codice PHP:
<?php
// prints form
function print_form(){
?>
<p></span><strong>Your email address will not be published.<br/> Required fields are marked *</strong><br/><br/></p>
<form method="post" action="<?php echo $PHP_SELF;?>" id="uploadform" enctype="multipart/form-data">
<p><label for="namefrom">Name <span class="required">*</span></label>
<br/>
<input name="namefrom" id="namefrom" type="text" class="field" tabindex="1"/></p>
<p><label for="surname">Surname <span class="required">*</label>
<br/>
<input name="surname" id="surname" type="text" class="field" tabindex="2"/></p>
<p><label for="emailfrom">Email <span class="required">*</span></label>
<br/>
<input name="emailfrom" id="emailfrom" type="text" class="field" tabindex="3"/></p>
<p><label for="phone">Phone <span class="required">*</label>
<br/>
<input name="phone" id="phone" type="text" class="field" tabindex="4"/></p>
<p><label for="subject">Subject <span class="required">*</span></label>
<br/>
<input name="subject" id="subject" type="text" class="field" tabindex="5"/></p>
<br/>
<p align="left"><label for="attachment" style="font-size:12px;"><strong>Allowed file formats: .doc, .pdf<br/></strong></label>
<input name="attachment" id="attachment" type="file" tabindex="7">
<br/><br/>
<p style="float:left; margin-left:10px; margin-right:10px;"><input type="submit" name="submit" id="submit" value="Send" tabindex="8"/></p>
<p style="float:left; margin-right:10px;"><input type="reset" name="reset" id="reset" value="Delete" tabindex="8"/></p>
<p><input type="hidden" name="submitted" value="true" /></p>
</form>
<?php
}
// enquiry form validation
function process_form() {
// Read POST request params into global vars
$to = "cv@pippo.it";
$subject = trim($_POST['subject']);
$namefrom = trim($_POST['namefrom']);
$surname = trim($_POST['surname']);
$phone = trim($_POST['phone']);
$emailfrom = trim($_POST['emailfrom']);
$mitt = "-f noreply@pippo.it";
// Allowed file types. add file extensions WITHOUT the dot.
$allowtypes=array("doc", "pdf");
// Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it,
// post_max_size, upload_max_filesize, file_uploads, max_execution_time!
// 2048kb = 2MB, 1024kb = 1MB, 512kb = 1/2MB etc..
$max_file_size="2048";
// Thank you message
$thanksmessage="Your email has been sent, we will respond shortly.";
$errors = array(); //Initialize error array
//checks for a name
if (empty($_POST['namefrom']) ) {
$errors[]='You forgot to enter your name';
}
//checks for an email
if (empty($_POST['emailfrom']) ) {
$errors[]='You forgot to enter your email';
} else {
if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['emailfrom'])))) {
$errors[]='Please enter a valid email address';
} // if eregi
} // if empty email
//checks for a phone
if (empty($_POST['phone']) ) {
$errors[]='You forgot to enter a phone';
}
//checks for a subject
if (empty($_POST['subject']) ) {
$errors[]='You forgot to enter a subject';
}
//checks attachment file
// checks that we have a file
if((!empty($_FILES["attachment"])) && ($_FILES['attachment']['error'] == 0)) {
// basename -- Returns filename component of path
$filename = basename($_FILES['attachment']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
$filesize=$_FILES['attachment']['size'];
$max_bytes=$max_file_size*2048;
//Check if the file type uploaded is a valid file type.
if (!in_array($ext, $allowtypes)) {
$errors[]="Invalid extension for your file: <strong>".$filename."</strong>";
// check the size of each file
} elseif($filesize > $max_bytes) {
$errors[]= "Your file: <strong>".$filename."</strong> is to big. Max file size is ".$max_file_size."kb.";
}
} // if !empty FILES
if (empty($errors)) { //If everything is OK
// send an email
// Obtain file upload vars
$fileatt = $_FILES['attachment']['tmp_name'];
$fileatt_type = $_FILES['attachment']['type'];
$fileatt_name = $_FILES['attachment']['name'];
// Headers
//$headers = "From: $emailfrom";
$headers = "From: " . $_POST['email'];
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
// create a boundary string. It must be unique
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$message ="This is a multi-part message in MIME format.\n\n";
$message.="--{$mime_boundary}\n";
$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: 7bit\n\n";
$message.="Name: ".$namefrom."\n";
$message.="Surname: ".$surname."\n";
$message.="Email: ".$emailfrom."\n";
$message.="Phone: ".$phone."\n";
$message.="Subject: ".$subject."\n\n\n\n";
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
// Send the completed message
if(!mail($to,$subject,$message,$headers, $mitt)) {
exit("Mail could not be sent. Sorry! An error has occurred, please report this to the website administrator.\n");
} else {
echo '<div id="formfeedback"><h3>Thank You!</h3><p>'. $thanksmessage .'</p><br/></div>';
echo "<META content=\"3; URL=peopleAndCareers.php\" http-equiv=\"Refresh\">";
} // end of if !mail
} else { //report the errors
echo '<div id="formfeedback"><h3>Error!</h3><p>The following error(s) has occurred:<br />';
foreach ($errors as $msg) { //prints each error
echo " - $msg<br />\n";
} // end of foreach
echo '</p><p>Please try again</p><br/></div>';
echo "<META content=\"3; URL=peopleAndCareers.php\" http-equiv=\"Refresh\">";
} //end of if(empty($errors))
} // end of process_form()
?>