2016-11-17 28 views
2
<?php 
$errors = ''; 
$myemail = '[email protected]';//<-----Put Your email address here. 
if(empty($_POST['name']) || 
    empty($_POST['email']) || 
    empty($_POST['message'])) 
{ 
    $errors .= "\n Error: all fields are required"; 
} 

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address)) 
{ 
    $errors .= "\n Error: Invalid email address"; 
} 

if(empty($errors)) 
{ 
    $to = $myemail; 
    $email_subject = "Contact form submission: $name"; 
    $email_body = "You have received a new message. ". 
    " Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message"; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email_address"; 

    mail($to,$email_subject,$email_body,$headers); 
    //redirect to the 'thank you' page 
    header('Location: contact-form-thank-you.html'); 
} 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <title>Contact form handler</title> 
</head> 

<body> 
<!-- This page is displayed only if there is some error --> 
<?php 
echo nl2br($errors); 
?> 


</body> 
</html> 

これは私の電子メールを送信するために使用している私のPHPファイルです。 以下はHTMLコードです。HTMLフォームから電子メールを送信

私はサーバでも試してみましたが、「すべてのフィールドが必要です」というエラーが表示されます 基本的に、変数はHTMLフォームからデータを取得していません。

<form action="email.php" method="post" name="contact_form" enctype="text/plain"> 
    <div class="form-group" > 
    <label for="name">Name</label> 
    <input class="form-control" type="text" name="name" id="name" placeholder="Full Name" style="border-radius: 0px; margin-bottom: 3%"> 

    <label for="email">Email address</label> 
    <input type="email" class="form-control" name="email" id="email" aria-describedby="emailHelp" placeholder="Enter email" style="border-radius: 0px; margin-bottom: 3%"> 

    <label for="message">Message</label> 
    <textarea class="form-control" rows="5" name="message" id="message" style="border-radius: 0px; margin-bottom: 3%"></textarea> 

    <button type="submit" class="btn btn-default" style="border-radius: 0px; background-color: midnightblue; box-shadow: 2px 2px 2px #888888; color: white;">Submit</button> 
    </div> 
</form> 
+1

ほとんど無効のenctypeのように:このコードを試してみてください(index.phpのとしてこのスクリプトを保存しますか)?この回答を見る:http://stackoverflow.com/questions/7628249/method-post-enctype-text-plain-are-not-compatible –

+0

代わりにtext/plainの代わりに何を使用すればよいですか? @AntonyThompson –

+2

Sooooooooooこれに関するStackoverflowで多くの質問がされている、あなたは検索する気にしましたか? – Xorifelse

答えて

0

私はあなたのフォームからのenctypeを削除しました。同じページにスクリプトも書いてください。

<?php 
if (isset($_POST['send_email'])) { 
    $errors = ''; 
    $myemail = '[email protected]'; //<-----Put Your email address here. 
    if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) { 
     $errors .= "\n Error: all fields are required"; 
    } 
    $name = $_POST['name']; 
    $email_address = $_POST['email']; 
    $message = $_POST['message']; 
    if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address)) { 
     $errors .= "\n Error: Invalid email address"; 
    } 
    if (empty($errors)) { 
     $to = $myemail; 
     $email_subject = "Contact form submission: $name"; 
     $email_body = "You have received a new message. " . 
      " Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message"; 
     $headers = "From: $myemail\n"; 
     $headers .= "Reply-To: $email_address"; 
     if (mail($to, $email_subject, $email_body, $headers)) { 
      echo 'mail sent'; 
     } else { 
      echo 'mail not sent'; 
     } 
    } 
} 
?> 
<form action="index.php" method="post" name="contact_form"> 
<div class="form-group" > 
    <label for="name">Name</label> 
    <input class="form-control" type="text" name="name" id="name" placeholder="Full Name" style="border-radius: 0px; margin-bottom: 3%"> 
    <label for="email">Email address</label> 
    <input type="email" class="form-control" name="email" id="email" aria-describedby="emailHelp" placeholder="Enter email" style="border-radius: 0px; margin-bottom: 3%"> 
    <label for="message">Message</label> 
    <textarea class="form-control" rows="5" name="message" id="message" style="border-radius: 0px; margin-bottom: 3%"></textarea> 
    <button type="submit" name="send_email" class="btn btn-default" style="border-radius: 0px; background-color: midnightblue; box-shadow: 2px 2px 2px #888888; color: white;">Submit</button> 
</div> 

+0

あなたのコードはヘッダインジェクションに脆弱だと思います。 –

+0

@RazibAlMamun、はい、私はAkshatコードを編集したばかりです。 –

0

フォーム田下のenctypeに削除してください= "text/plainの"

関連する問題