我对 PHP 很陌生,我需要一个联系表,所以我从互联网上取下了一个并编辑了我需要/想要的所有内容。邮件不会发送,所以如果有人能帮我解决这个问题,我会很高兴!
HTML:
<form name="contactform" method="post"action="send_form_email.php">
<p class="name">
<input type="text" name="first_name" id="name" placeholder="Fullständigt namn" />
</p>
<p class="foretag">
<input type="text" name="foretag" id="foretag" placeholder="Företag" />
</p>
<p class="email">
<input type="text" name="email" id="email" placeholder="Email" />
</p>
<p class="telefon">
<input type="text" name="telephone" id="telephone" placeholder="Telefonnummer" />
</p>
<p class="text">
<textarea name="text" id="text" placeholder="Meddelande" /></textarea>
</p>
<p class="robotic" id="pot">
<label>Om du är människa, fyll inte i denna ruta:</label>
<input name="robotest" type="text" id="robotest" class="robotest" />
</p>
<p class="submit">
<input type="submit" value="Skicka" />
</p>
</form>
</div>
PHP:
<?php
if(isset($_POST['email'])) {
$email_to = "EMAIL HERE";
$email_subject ="subject";
function died($error) {
// your error code can go here
echo "Fel uppstog";
echo "Error kod:.<br /><br />";
echo $error."<br /><br />";
echo "Gå tillbaka och rätta till problemen innan du försöker igen.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['foretag']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['text'])) {
died('Det verkar vara ett problem med formuläret du angav');
}
$first_name = $_POST['first_name'];
$foretag = $_POST['foretag'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$text = $_POST['text'];
$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 .= 'Emailadressen du angav verkar inte vara giltig.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'Namnet du angav verkar inte vara giltig.<br />';
}
if(!preg_match($string_exp,$foretag)) {
$error_message .= 'Företaget du angav är inte giltig..<br />';
}
if(strlen($text) < 2) {
$error_message .= 'Meddelandet du angav är inte giltig..<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 .= "Comments: ".clean_string($comments)."\n";
if (preg_match("/http/i", "$first_name")) {echo "$spamErrorMessage"; exit();}
if (preg_match("/http/i", "$email")) {echo "$spamErrorMessage"; exit();}
if (preg_match("/http/i", "$telephone")) {echo "$spamErrorMessage";exit();}
if (preg_match("/http/i", "$text")) {echo "$spamErrorMessage"; exit();}
$SpamErrorMessage = "Ingen URL tillåten";
$headers = 'From: '.$email_from."\r\n";
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if($robotest)
$error = "You are a gutless robot.";
else {
@mail($email_to, $email_subject, $email_message, $headers);
}
}
?>