Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么未设置“发件人电子邮件”字段?_Php_Html_Captcha_Recaptcha - Fatal编程技术网

Php 为什么未设置“发件人电子邮件”字段?

Php 为什么未设置“发件人电子邮件”字段?,php,html,captcha,recaptcha,Php,Html,Captcha,Recaptcha,问题解决了!以下是最终代码: <?php $full_name;$email;$subject;$message;$captcha; if(isset($_POST['full_name'])){ $full_name=$_POST['full_name']; }if(isset($_POST['email'])){ $email=$_POST['email']; }if(isset($

问题解决了!以下是最终代码:

<?php
    $full_name;$email;$subject;$message;$captcha;
        if(isset($_POST['full_name'])){
            $full_name=$_POST['full_name'];
        }if(isset($_POST['email'])){
            $email=$_POST['email'];
        }if(isset($_POST['subject'])){
            $subject=$_POST['subject'];
        }if(isset($_POST['message'])){
            $message=$_POST['message'];
        }if(isset($_POST['g-recaptcha-response'])){
            $captcha=$_POST['g-recaptcha-response'];
        }
        if(!$captcha){
            echo 'Check the reCAPTCHA box.';
            exit;
        }
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRET-KEY-HERE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
        if($response.success==false)
        {
            echo 'You are a robot!';
        }else
        {
            $to = "me@domain.com";
            $from = $full_name . ' <'.$email.'>';
            $headers = 'From: ' . $from . "\r\n";
            mail ($to, $subject, $message, $headers);

        echo 'Your message has been sent!';
        }
?>

现在我得到了发件人姓名+电子邮件地址的“发件人”字段


谢谢大家的帮助。

首先,您根本没有初始化$headers变量,但是您有

$headers.='From:'$从…起“\r\n”

在Begging上初始化它,或者将运算符“=”更改为“=”

接下来,只有在send$\u POST数组中存在字段“g-recaptcha-response”时,才会执行上述连接。正如我所看到的,在下面的HTML代码中没有具有该名称的字段。尝试添加该字段或删除此“if”语句

如果(isset($_POST['g-recaptcha-response']))


我认为问题可能在于您从变量构建
的方式:

$from = $full_name . '<'.$email.'>';
$from=$full_name';
请注意,全名和<字符之间没有空格。在其中添加一个空格,您应该表现良好,即:

$from = $full_name . ' <'.$email.'>';
$from=$full_name';

顺便说一句,您的代码结构不是很好,并且有一些逻辑缺陷,正如评论员所提到的。考虑为你自己的理智调整它。

确保你能正确地确认你从$POST收到所有东西。如果失败,你应该在每一步都触发一个错误

在代码中

if(isset($_POST['full_name']) && isset($_POST['full_name'])){
        $full_name = $_POST['full_name'];
        $email = $_POST['email'];
        $from = $full_name . '<'.$email.'>';
}
if(isset($\u POST['full\u name'])和&isset($\u POST['full\u name'])){
$full_name=$_POST['full_name'];
$email=$_POST['email'];
$from=$full_name.';
}

您对$\u POST['full\u name']进行了两次验证;$u POST['email']上没有isset()检查。如果$\u POST['email']无法正常发送,则发件人电子邮件地址将被清空。

将var\u dump($\u POST)添加到contact\u form.php中会得到什么?您确定电子邮件值通过POST传递到该表单吗?请尝试移动此行:$headers.='From:'$从…起“\r\n”;从相应的if语句中删除,看看这是否有帮助。根据当前逻辑,如果未设置验证码,则不会设置from值。