【问题标题】:Blank PHP Emails空白 PHP 电子邮件
【发布时间】:2014-05-21 23:59:45
【问题描述】:

这里有很多空白的 php 电子邮件帖子,但没有一个为我解决了这个问题。

我调整了这个简单的 php 代码,我发现它只是通过电子邮件发送一个指定的电子邮件地址(在本例中是我客户的电子邮件),并在他们的网站上包含来自客户的反馈消息。通过测试,我只有在没有包含初始 if 语句作为验证时才能让它发送电子邮件,但即使这样,电子邮件也没有主题或正文。

contact.html

<form name="feedback" class="form-horizontal" role="form" action="send_form_email.php" method="post">
    <div class="form-group">
        <label for="inputName" class="col-sm-3 control-label">Name</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" name="inputName" placeholder="Name"><br />
        </div>
    </div>

    <div class="form-group">
        <label for="inputEmail" class="col-sm-3 control-label">Email</label>
        <div class="col-sm-9">
            <input type="email" class="form-control" name="inputEmail" placeholder="Email"><br />
        </div>
    </div>

    <div class="form-group">
        <label for="inputMessage" class="col-sm-3 control-label">Message</label>
        <div class="col-sm-9">
            <textarea type="text" class="form-control" name="inputMessage" placeholder="Message"></textarea><br />
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <input class="btn btn-default" type="submit" value="Submit">
        </div>
    </div>    
</form>

send_form_email.php

<?php

if($_SERVER['REQUEST_METHOD'] == "POST"){


// Contact subject
$name =$_POST['inputName']; 

// Details
$message=$_POST['inputMessage'];

// Mail of sender
$mail_from=$_POST['inputEmail']; 

// From 
$header="from: $name <$mail_from>";

// Enter your email address
$to ='test@gmail.com';
$send_contact=mail($to,$name,$message,$header);

// Check, if message sent to your email 
// display message "We've recived your information"
header("Location: http://wetzelscontracting.com/postcontact.html");
if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}}
?>

好吧,伙计们,长篇大论,但 Mailto 实际上并没有出现在动作属性中,我从帖子中删除了它。

实际上,我不知道我最初发布的是什么样的科学怪人代码,但那充满了不再存在的错误。希望这次我发布了正确的代码。

【问题讨论】:

  • 为什么表格上的MAILTO:send_form_email.phpaction?不应该只是send_form_email.php
  • textarea 缺少名称添加 name="inputMessage"
  • 此外,您的 if() 语句输出永远不会被客户端看到,因为浏览器会消失到 /postcontact.html
  • @LeeS 是的,我只是把标题放在那里并没有删除 if 语句,输出在 postcontact.html
  • @brttwrd 只是检查:)

标签: php html forms


【解决方案1】:

为什么你的表单动作是MAILTO:

<form name="feedback" class="form-horizontal" role="form" action="MAILTO:send_form_email.php" method="post">

这应该只是对 PHP 页面的干净调用,如下所示:

<form name="feedback" class="form-horizontal" role="form" action="send_form_email.php" method="post">

您唯一会使用MAILTO: 是在构造&lt;a href="mailto:someguy@someplace.somedomain"&gt; 时。对于像这样使用 PHP 的 HTML 表单,目标是提交表单,然后由 PHP 解析 $_POST 数据,然后对其进行操作以发送电子邮件。

此外,您没有在任何input 字段中设置name 值,并且您为id 值设置的名称甚至与PHP 尝试执行的操作都不匹配。所以试试这个 HTML:

<form name="feedback" class="form-horizontal" role="form" action="send_form_email.php" method="post">
    <div class="form-group">
            <label for="inputName" class="col-sm-3 control-label">Name</label>
        <div class="col-sm-9">
             <input type="text" class="form-control" id="inputName" placeholder="Name" name="inputName"><br />
        </div></div>

        <div class="form-group">
        <label for="inputEmail" class="col-sm-3 control-label">Email</label>
        <div class="col-sm-9">
            <input name="email" type="email" class="form-control" id="inputEmail" placeholder="Email" name="inputEmail"><br />
    </div></div>

        <div class="form-group">
        <label for="inputMessage" class="col-sm-3 control-label">Message</label>
        <div class="col-sm-9">
             <textarea type="text" class="form-control" id="inputMessage" placeholder="Message"  name="inputMessage"></textarea><br />
        </div></div>

        <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
        <input class="btn btn-default" type="submit" value="Submit">
        </div></div>    
</form>

这里还有重新设计的 PHP 代码。

我做的第一件事是将所有$_POST 检查放入一个使用一个主数组 ($post_array) 的结构中,然后遍历该数组以处理值并将它们分配给类似命名的变量。您之前绝对没有输入验证。从技术上讲,这甚至不是很好的“验证”,因为isset() 只是检查$_POST 值是否存在。但这是一步到位。

最后我还重新设计了您的错误检查逻辑,因为这一切都发生在发送标头之后。这意味着整个"We've recived your information" 都不会起作用。这是我能用你提供的信息做的最好的事情,但我这样做是为了传达基本概念:

<?php

if ($_SERVER['REQUEST_METHOD'] == "POST"){

  // Set the post values array.    
  $post_array = array('inputName','inputEmail','inputMessage');

  // Roll through the post values array.
  foreach($post_array as $post_key => $post_value) {
    $$post_key = isset($_POST[$post_key] ? $_POST[$post_key] : null; 
  }

  // From 
  $header="from: $name <$mail_from>";

  // Enter your email address
  $to ='test@gmail.com';
  $send_contact=mail($to,$name,$message,$header);

  // Check, if message sent to your email 
  // display message "We've recived your information"
  if($send_contact){
     header("Location: http://wetzelscontracting.com/postcontact.html");
  }
  else {
    echo "ERROR";
  }
}
?>

【讨论】:

  • 另外,您很容易受到从您的脚本中发送的垃圾邮件的攻击。请考虑增加一些安全性,因为我们不需要 83% 的其他贡献者。
  • @Ohgodwhy 你是对的。但如果原发帖人甚至不了解基本的 HTML 和 PHP 表单的工作原理,那么他们将如何理解验证表单输入以防止误用的概念。
  • 但这不会阻止表单工作而不是以某种方式发送空白电子邮件吗?
  • @Popnoodles 我没有尝试过代码,但我猜是 PHP 表单被加载了,但是因为它是通过mailto:$_POST 数据没有得到处理。
  • @JakeGould 你在 OP 上的权利不理解,因为它看起来像是来自这个答案的普通复制和粘贴 stackoverflow.com/questions/22545312/php-form-firing-blanks Fred -ii- 应该知道得更好,坏代码会产生坏代码,就像 83%网络上的教程;-s
【解决方案2】:

因为除了接受的答案之外,其他答案都没有涵盖验证问题,但如果你打算这样做,你不妨只使用extract() 函数,(它也不会防止标头注入或电子邮件验证)。

验证用户输入和简单的 CSRF 保护层非常重要,否则机器人或垃圾邮件发送者可以直接 POST 到您的 PHP,它会向您发送大量电子邮件,您不会只见树木不见森林(合法电子邮件),或者更糟糕的是,将标头注入您的 inputEmail 字段并使用您的服务器发送他们自己的电子邮件,这显然是您不希望发生的事情。

我还添加了一种简单的方法,您可以从 PHP 脚本中传递错误,将用户发送回表单供您回显。

send_form_email.php 文件也是如此。

<?php
session_start();

if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_SESSION['csrf'])){
    //set error array to fill
    $errors = array();

    // Validate Contact subject
    if(!empty($_POST['inputName'])){
        $name = $_POST['inputName'];
    }else{
        $error['inputName'] = 'Required!';
    }

    // Validate Details
    if(!empty($_POST['inputMessage'])){
        $message = $_POST['inputMessage'];
    }else{
        $error['inputMessage'] = 'Required!';
    }

    // Validate Mail of sender
    if(!empty($_POST['inputEmail'])){
        if(filter_var($_POST['inputEmail'], FILTER_VALIDATE_EMAIL)){
            $mail_from = $_POST['inputEmail'];
        }else{
            $error['inputEmail'] = 'Invalid Email!';
        }
    }else{
        $error['inputEmail'] = 'Required!';
    }

    if(!isset($_POST['csrf']) || $_SESSION['csrf'] != $_POST['csrf']){
        $_SESSION['email_status'] = 'Invalid csrf token!';
        $error = true;
    }

    //stop multiple attempts - just remove csrf token
    unset($_SESSION['csrf']);

    //no errors send mail
    if(empty($error)){
        $headers ='MIME-Version: 1.0'."\r\n";
        $headers.='Content-type: text/html; charset=utf8'."\r\n";
        $headers.='From:<'.$mail_from.'>'."\r\n";
        $headers.="X-Mailer: PHP"."\r\n";

        if(mail('test@gmail.com', 'Website email form: '.$name, $message, $headers)){
            $_SESSION['email_status'] = "We've received your contact information";
            //send to success page
            exit(header("Location: http://wetzelscontracting.com/postcontact.html"));
        }else {
            $_SESSION['email_status'] = 'There was an error sending the mail';
            //backup to file
            file_put_contents('mail.log.txt',print_r($_POST, true).PHP_EOL, FILE_APPEND);
        }
    }else{

        //assuming its this url
        exit(header("Location: http://wetzelscontracting.com/contact.php"));
        $_SESSION['email_error'] = $error;
    }

}else{
    //stop multiple attempts
    unset($_SESSION['csrf']);

    //dont allow GET request/direct access
    exit(header("Location: http://wetzelscontracting.com/contact.php"));

}
?>

然后在带有表单的页面中,启动一个会话以从$_SESSION 数组中读取数据,如果有任何错误,则回显您的错误。

<?php 
session_start(); 
//make a session key that we will check against in send_form_email.php
$_SESSION['csrf'] = sha1(uniqid(true));
?>

<?php echo isset($_SESSION['email_status']) ? $_SESSION['email_status'] : null ?>
<form name="feedback" class="form-horizontal" role="form" action="send_form_email.php" method="post">

    <input type="hidden" name="csrf" value="<?php echo $_SESSION['csrf'];?>"/>
    <div class="form-group">
        <label for="inputName" class="col-sm-3 control-label">Name <?php echo isset($_SESSION['email_error']['inputName']) ? $_SESSION['email_error']['inputName'] : null?></label>
        <div class="col-sm-9">
             <input type="text" class="form-control" id="inputName" placeholder="Name" name="inputName"><br />
        </div>
    </div>

    <div class="form-group">
        <label for="inputEmail" class="col-sm-3 control-label">Email <?php echo isset($_SESSION['email_error']['inputEmail']) ? $_SESSION['email_error']['inputEmail'] : null?></label>
        <div class="col-sm-9">
            <input type="email" class="form-control" id="inputEmail" placeholder="Email" name="inputEmail"><br />
        </div>
    </div>

    <div class="form-group">
        <label for="inputMessage" class="col-sm-3 control-label">Message <?php echo isset($_SESSION['email_error']['inputMessage']) ? $_SESSION['email_error']['inputMessage'] : null?></label>
        <div class="col-sm-9">
             <textarea type="text" class="form-control" id="inputMessage" placeholder="Message"  name="inputMessage"></textarea><br />
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <input class="btn btn-default" type="submit" value="Submit">
        </div>
    </div>    
</form>
<?php
//unset the errors so there only shown once
unset($_SESSION['email_status']);
unset($_SESSION['email_error']); ?>

【讨论】:

  • hmmm 我有 2 个错误,无法加载资源:net::ERR_BLOCKED_BY_CLIENT cdn.dsultra.com/js/registrar.js 和未捕获的 ReferenceError:registrar_frameset 未定义
  • @brttwrd 这与您的广告块有关,与答案中的代码无关。
  • @brttwrd 你也应该把wetzelscontracting.com/contact.html改成wetzelscontracting.com/contact.php注意php扩展否则php不会被解析..
  • 如果客户端有广告屏蔽怎么办?他们不会得到同样的错误吗?
  • 当页面尝试向我推送垃圾广告时,我会遇到所有类型的错误JustHosts 404 错误页面中的谷歌广告可能是您接收的 js 代码中的http://pagead2.googlesyndication.com/apps/domainpark/show_afd_ads.js
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
相关资源
最近更新 更多