2016-07-27 2 views
0

からのメールを取得いけない:私は、フォームにindex.htmlを持つ問い合わせフォーム

<form method="post" action="mail.php"> 
       <div class="col-sm-7"> 
        <div class="row"> 
         <div class="col-sm-6 form-group"> 
          <input class="form-control" id="name" name="name" placeholder="Name" type="text" required> 

          </div> 

         <div class="col-sm-6 form group"> 
          <input class="form-control" id="email" name="email" placeholder="Email" type="email" required> 

         </div> 
        </div> 

        <textarea class="form-control" id="message" name="message" placeholder="Comment" rows="5"></textarea> 

        <div class="row">   
         <div class="col-sm-12 form-group"> 
          <button class="btn btn-default pull-right" type="submit">Send</button> 
         </div> 
        </div> 
       </div> 
       </form> 

とmail.php:

 <?php 

// Check for header injection 
function has_header_injection($str){ 
return preg_match("/[\r\n]/", $str); 
} 

if(isset($_POST['submit'])){ 

$name = trim($_POST['name']); 
$email = trim($_POST['email']); 
$msg = $_POST['message']; 


// Check to see if $name or $email have header injections 
if(has_header_injection($name) || has_header_injection ($email)){ 
    die(); //if true kill the script 
} 

if(!$name || !$email || !$msg){ 
    echo '<h2>All fields required</h2><a href="mail.php" class="button block">Go back and try again</a>'; 
    exit; 
} 


// Add the recipient email to a variable 
$to = "[email protected]"; 

// Create a subject 
$subject = "$name sent you a message via your website"; 

//Construct the message 
$msg = "Name: $name\r\n"; 
$msg .= "Email: $email\r\n"; 
$msg .= "Message:\r\n$msg"; 

$msg = wordwrap($msg, 72); 

// Set the mail headers into a variable 
$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n"; 
$headers .= "From: $name <$email>\r\n"; 
$headers .= "X-Priority: 1\r\n"; 
$headers .= "X-MSMail-Priority: High\r\n\r\n"; 


// Send the email 
mail($to, $subject, $msg, $headers) 
} 

?> 

<script language="javascript"> window.location="index.html";</script> 

間違って何イムを?私はこれをlocalhostとホストされたサイトでテストしました。私はフォームを送信すると、それはちょうど空白のページmail.php、エラーなしで、私の元のページに戻って私を送っているPHPの下のスクリプトで(私がしたいと思っているbcを送信します。私は、追加試験さ:

if (mail($to, $subject, $body, $headers)===false) { echo "Not sent!"; } else { echo "Sent!"; }

が、エラーなしで、それが送信されたことを私に示していないかのどちらかdoesntの...ので、私が間違っているのでしょうか?誰かが自分宛にメールを送ることができないと私に言いました。私は他の人に私のためにメールを送りました...運がない、まだメールは送られていません。

Plsのヘルプイムはすでに3日以上、この時に立ち往生:あなたは最後に;を欠落していた

+1

'if(isset($ _ POST ['submit'])){...}'は決して起こりません。これは未定義のインデックスです。 –

+0

あなたのWebサーバーにsendmailをインストールする必要があります。 –

+3

* "エラーはありません" * - それはあなたがチェックしていないからです。 –

答えて

0

index.htmlを

<form method="post" action="mail.php"> 
<div class="col-sm-7"> 
    <div class="row"> 
     <div class="col-sm-6 form-group"> 
      <input class="form-control" id="name" name="name" placeholder="Name" type="text" required> 

     </div> 

     <div class="col-sm-6 form group"> 
      <input class="form-control" id="email" name="email" placeholder="Email" type="email" required> 

     </div> 
    </div> 

    <textarea class="form-control" id="message" name="message" placeholder="Comment" rows="5"></textarea> 

    <div class="row"> 
     <div class="col-sm-12 form-group"> 
      <button class="btn btn-default pull-right" type="submit" name="submit">Send</button> 
     </div> 
    </div> 
</div> 
</form> 

mail.php

<?php 

// Check for header injection 
function has_header_injection($str){ 
    return preg_match("/[\r\n]/", $str); 
} 

if(isset($_POST['submit'])){ 

    $name = trim($_POST['name']); 
    $email = trim($_POST['email']); 
    $msg = $_POST['message']; 


    // Check to see if $name or $email have header injections 
    if(has_header_injection($name) || has_header_injection ($email)){ 
     die(); //if true kill the script 
    } 

    if(!$name || !$email || !$msg){ 
     echo '<h2>All fields required</h2><a href="mail.php" class="button block">Go back and try again</a>'; 
     exit; 
    } 


    // Add the recipient email to a variable 
    $to = "[email protected]"; 

    // Create a subject 
    $subject = "$name sent you a message via your website"; 

    //Construct the message 
    $msg = "Name: $name\r\n"; 
    $msg .= "Email: $email\r\n"; 
    $msg .= "Message:\r\n$msg"; 

    $msg = wordwrap($msg, 72); 

    // Set the mail headers into a variable 
    $headers = "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n"; 
    $headers .= "From: $name <$email>\r\n"; 
    $headers .= "X-Priority: 1\r\n"; 
    $headers .= "X-MSMail-Priority: High\r\n\r\n"; 


    // Send the email 
    mail($to, $subject, $msg, $headers); 
} 

?> 

<script language="javascript"> window.location="index.html";</script> 

\あなたのmail()機能の Aswell $_POST['submit']は常に未定義あるので、私たちは、私はあなたのPOST配列は、「提出する」キーが含まれているとは思わないのindex.html

0

のボタンにname="submit"を追加する必要がありました。

if(isset($_POST['submit'])){ 

スクリプト全体がスキップされます。

関連する問題