2016-10-19 19 views
-2

私は現地でメールを送信するためにPHPフォームをテストしています。PHP送信フォームが正しく動作しない

私はすべての入力を入れますが、送信ボタンを押すと常に「false」とエラーメッセージが返されます。それは私がローカルで働いていて、メールサーバーを持っていないか、コードに何か間違っているからですか?ここ

コード:

<?php 
    if(isset($_POST['submit'])) 
    { 
     if(empty($_POST['nome'])  || 
      empty($_POST['email'])  || 
      empty($_POST['motivo'])  || 
      empty($_POST['messaggio']) || 
      !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) 
      { 
      echo "No arguments Provided!"; 
      // return false; 
      } 
     else 
      { 

       $nome = strip_tags(htmlspecialchars($_POST['nome'])); 
       $email_address = strip_tags(htmlspecialchars($_POST['email'])); 
       $motivo = strip_tags(htmlspecialchars($_POST['motivo'])); 
       $messaggio = strip_tags(htmlspecialchars($_POST['messaggio'])); 

       // Create the email and send the message 
       $to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to. 
       $email_subject = "Website Contact Form: $nome"; 
       $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $nome\n\nEmail: $email_address\n\nOggetto: $motivo\n\nMessaggio:\n$messaggio"; 
       $headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected] 
       $headers .= "Reply-To: $email_address"; 
       if ([email protected]($to,$email_subject,$email_body,$headers)) 
        { 
        // return true; 
         echo "<p>Email error</p>"; 
        } 
        else 
        { 
         echo "<p>Email sent successfully!</p>"; 
        } 
      } 
    } 
    ?> 
    <form class="form-horizontal col-sm-6 col-sm-offset-3" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST"> 
     <div class="form-group"> 
     <div class="row"> 
      <label for="email" class="col-sm-12">Email</label> 
     </div> 
     <div class="row"> 
      <div class="col-sm-12"> 
      <input type="email" name="email" class="form-control" id="email" placeholder="Email"> 
      </div> 
     </div> 
     </div> 
     <div class="form-group"> 
     <div class="row"> 
      <label for="nome" class="col-sm-12">Nome</label> 
     </div> 
     <div class="row"> 
      <div class="col-sm-12"> 
      <input type="text" name="nome" class="form-control" id="nome" placeholder="Nome"> 
      </div> 
     </div> 
     </div> 
     <div class="form-group"> 
     <div class="row"> 
      <label for="motivo" class="col-sm-12">Motivo</label> 
     </div> 
     <div class="row"> 
      <div class="col-sm-12"> 
      <input type="text" name="motivo" class="form-control" id="motivo" placeholder="Motivo"> 
      </div> 
     </div> 
     </div> 
     <div class="form-group"> 
     <div class="row"> 
      <label for="messaggio" class="col-sm-12">Messaggio</label> 
     </div> 
     <div class="row"> 
      <div class="col-sm-12"> 
      <textarea class="form-control" name="messaggio" rows="5" id="messaggio" placeholder="Motivo">Inserisci il tuo messaggio...</textarea> 
      </div> 
     </div> 
     </div> 
     <div class="form-group"> 
     <div class=""> 
      <button type="submit" name="submit" class="btn btn-default">Invia</button> 
     </div> 
     </div> 
    </form> 

任意のヘルプ?

+0

「エラー」とは何ですか? – Jagrati

+0

はあなたの 'error'メッセージに完全に依存します。投稿する! – Pragun

+0

デバッグヘルプ(「なぜこのコードは動作しませんか?」)には、目的の動作、特定の問題またはエラー、および質問自体に再現するのに必要な最短コードが含まれている必要があります。明確な問題文がない質問は、他の読者にとって有用ではありません。参照:[mcve]を作成する方法。 – xenteros

答えて

0

ので、私は地元で働いているし、任意のメールサーバー

はいを​​持っていないということです。

PHPのローカルメールサーバーを実行していない場合はは常に失敗と偽を返しますmail()機能。

This answer has some useful tips

あなたはセットアップに仕事にメール機能のためにあなたのマシン上のメールサーバが必要です。 Windowsの場合(私はWAMPを使用していると推測しています)、Pegasus mail serverを設定することができます。

その他のオプションには、SwiftMailerPHPMailerなどのラッパークラスを使用し、Gmailアカウントなどの別のSMTPサーバーに接続するオプションがあります。自分のlocalhostルートでPegasusメールサーバーに行っても、上記の2つのクラスのいずれかを使用することをお勧めします。彼らはあなたにはるかに柔軟性を与え、より安全です。

ISPのSMTPサーバまたはGmailに接続するか、またはこれから最も簡単な経路があれば接続します。

MailhogまたはMailcatcherを参照して、メールをローカルにキャプチャして内容を確認することもできます。

0

(@mail($に、$ EMAIL_SUBJECT、$ email_body、$ヘッダ))場合

に "@" を削除

<?php 
    if(isset($_POST['submit'])) 
    { 
     if(empty($_POST['nome'])  || 
      empty($_POST['email'])  || 
      empty($_POST['motivo'])  || 
      empty($_POST['messaggio']) || 
      !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) 
      { 
      echo "No arguments Provided!"; 
      // return false; 
      } 
     else 
      { 

       $nome = strip_tags(htmlspecialchars($_POST['nome'])); 
       $email_address = strip_tags(htmlspecialchars($_POST['email'])); 
       $motivo = strip_tags(htmlspecialchars($_POST['motivo'])); 
       $messaggio = strip_tags(htmlspecialchars($_POST['messaggio'])); 

       // Create the email and send the message 
       $to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to. 
       $email_subject = "Website Contact Form: $nome"; 
       $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $nome\n\nEmail: $email_address\n\nOggetto: $motivo\n\nMessaggio:\n$messaggio"; 
       $headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected] 
       $headers .= "Reply-To: $email_address"; 
       if (mail($to,$email_subject,$email_body,$headers)) 
        { 
        // return true; 
         echo "<p>Email error</p>"; 
        } 
        else 
        { 
         echo "<p>Email sent successfully!</p>"; 
        } 
      } 
    } 
    ?> 

それはその後、動作するはずです。

関連する問題