PHP发送表单没有';我不能正常工作

PHP发送表单没有';我不能正常工作,php,Php,我正在测试一个PHP表单,以便在本地发送电子邮件 我输入了所有内容,但当我按下提交按钮时,它总是返回“false”,然后返回错误消息。这是因为我在本地工作,没有任何邮件服务器,还是我的代码有问题 代码如下: <?php if(isset($_POST['submit'])) { if(empty($_POST['nome']) || empty($_POST['email']) || empty(

我正在测试一个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 = 'mirkocoppola80@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - 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: mirkocoppola80@gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
                $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>";
                    }
            }
    }
    ?>
    <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>

这是因为我在本地工作,没有任何邮件服务器吗

如果没有本地邮件服务器运行,PHP的
mail()
函数将始终失败并返回false

您需要在计算机上设置邮件服务器,邮件功能才能正常工作。如果您使用的是Windows(我猜您使用的是WAMP),您可以设置一个

其他选项包括使用包装类,如或,并使用它们连接到另一个SMTP服务器,如您的GMail帐户。即使您使用自己的本地主机路径使用Pegasus邮件服务器,我仍然建议您使用上面提到的两个类中的一个。他们给你更多的灵活性和更安全

连接到您的ISPs SMTP服务器或GMail或其他最简单的路由

要扩展上述内容,您还可以在本地查看或捕获邮件并检查其内容。

删除中的“@”

if(@mail($to、$email\u subject、$email\u body、$headers))



那么它应该可以工作了。

你得到了什么
错误
呢?完全取决于你的
错误
信息。发帖!寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建。它总是在echo“Email error”中返回消息,这是mail()不正确的情况。我希望在这行echo“Email sent successful!

”中返回消息。
<?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 = 'mirkocoppola80@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - 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: mirkocoppola80@gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
                $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>";
                    }
            }
    }
    ?>