我有一个半工作的PHP联系表格与HTML。 半工作是因为验证不起作用,即使其中一个字段的格式不正确,也会发送邮件。 第二件事是我想从用户上传的附件中添加该联系人(只有一个文件)。
<?php
header('Content-type: text/html; charset=utf-8');
$EmailFrom = "Sap_Prio_Hash@lander.comsign";
$EmailTo = "chenf@comda.co.il";
$Subject = "Lead from Sap/Prio/Hash lander";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Message = Trim(stripslashes($_POST['Message']));
function died($error) {
// your error code can go here
echo "Please check the field error:<br />";
echo $error."<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['Tel']) ||
!isset($_POST['Email']) ) {
died('Error found in fields');
}
$Tel = $_POST['Tel']; // required
$Email = $_POST['Email']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$Email)) {
$error_message .= 'Email is incorrect<br />';
}
$string_exp = "/^[0-9 .'-]+$/";
if(!preg_match($string_exp,$Tel)) {
$error_message .= 'Phone number is incorrect<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
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);
}
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
html中的表单很简单:
<div id="formdiv">
<form id='contactus' name="contactform" method="post" action="contacten.php">
<input type="text" name="Pickpro" id="Pickpro" class="textbox" />
<input type="text" name="Name" id="Name" class="textbox" />
<input type="text" name="Email" id="Text1" class="textbox" />
<input type="text" name="Tel" id="Tel" class="textbox" />
<input type="text" name="Message" id="Message" class="textbox" />
<input type="file" class="file" id="attachment" />
<input type="submit" id="send" title="שלח" value="" />
</form>
</div>
答案 0 :(得分:1)
当你只是迫使你的验证成为&#34; ok&#34;时,你是如何期望你的验证工作的?每一次?
$validationOK=true; <--- your validation never fails because of this
if (!$validationOK) {
如果您想在该电子邮件中发送附件,请不要使用mail()
。它可怜无用&#34;仅在紧急情况下使用&#34;垃圾。使用正确的邮件程序包,如PHPMailer或Swiftmailer。这两个都使得发送哑剧邮件+附件变得微不足道。