使用PHP发送电子邮件

时间:2014-11-20 04:01:43

标签: php forms validation email frontend

当我输入要提交的信息时,它永远不会通过。我在代码验证下得到了错误。 怎么会有人遇到这种问题?

      <?php

        $EmailTo = "cccxx@hotmail.com";
        $Subject = "Site Contact";
        $Name = Trim(stripslashes($_POST['Name'])); 
        $Email = Trim(stripslashes($_POST['Email'])); 
        $Message = Trim(stripslashes($_POST['Message'])); 

        // Email body text
        $Body = " ";
        $Body .= "Name: "; 
        $Body .= $Name;
        $Body .= "\n";
        $Body .= "Email: ";
        $Body .= $Email;
        $Body .= "\n";
        $Body .= "Message: ";
        $Body .= $Message;
        $Body .= "\n";

        // send email 
        $success = mail($EmailTo, $Subject, $Body, "From: <$Email>");

        if ($_POST['submit']) {
            if ($Name != '' && $Email != '' && $Message != '') {               
                if ($success) { 
                  echo '<p class="bg-success" style="padding:15px 10px;">Your message has been sent!</p>'; 
              } else { 
                  echo '<p class="bg-danger" style="padding:15px 10px;">Something went wrong, go back and try again!</p>'; 
              }
            } else {
                echo '<p class="bg-danger" style="padding:15px 10px;">You need to fill in all required fields!!</p>';
            }
        }
        ?>

1 个答案:

答案 0 :(得分:2)

制作剧本的基本步骤......

  1. 在脚本中启用错误报告。 (将此添加到开头 你的剧本)

    error_reporting(E_ALL);
    ini_set('display_errors',1);
    
  2. 在您的情况下,您可能需要检查中的$ _POST变量 脚本。

    echo "<pre>";print_r($_POST);echo "</pre>";
    
  3. 还要确保你设置了方法=&#34; post&#34;在您的表单标签