未定义的变量:php邮件脚本中的use_smtp错误

时间:2014-04-07 16:02:07

标签: php html forms email nginx

我在网页上有一个表单,它调用php脚本发送电子邮件,虽然它不能正常工作,但是当我提交表单时,我的nginx错误日志会给出以下内容;

2014/04/07 15:55:42 [error] 10829#0: *1 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined variable: use_smtp in /home/deployer/example/releases/20140407153701/form-handler.php on line 62" while reading response header from upstream, client:
86.131.120.178, server: example.net, request: "POST /form-handler.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "example.net", referrer: "http://example.net/"

第62行是

  

if($ use_smtp ==' 1'){

来自form-handler.php的

    

$use_smtp = '0';
$emailto = 'me@example.net';

    // retrieve from parameters
    $emailfrom = isset($_POST["email"]) ? $_POST["email"] : "";
    $nocomment = isset($_POST["nocomment"]) ? $_POST["nocomment"] : "";
    $subject = 'Apophenic Enquiry';
    $message = '';
    $response = '';
    $response_fail = 'There was an error verifying your details.';

        // Honeypot captcha
        if($nocomment == '') {

            $params = $_POST;
            foreach ( $params as $key=>$value ){

                if(!($key == 'ip' || $key == 'emailsubject' || $key == 'url' || $key == 'emailto' || $key == 'nocomment' || $key == 'v_error' || $key == 'v_email')){

                    $key = ucwords(str_replace("-", " ", $key));

                    if ( gettype( $value ) == "array" ){
                        $message .= "$key: \n";
                        foreach ( $value as $two_dim_value )
                        $message .= "...$two_dim_value<br>";
                    }else {
                        $message .= $value != '' ? "$key: $value\n" : '';
                    }
                }
            }

        $response = sendEmail($subject, $message, $emailto, $emailfrom);

        } else {

            $response = $response_fail;

        }

    echo $response;

// Run server-side validation
function sendEmail($subject, $content, $emailto, $emailfrom) {

    $from = $emailfrom;
    $response_sent = 'Thank you. Your messsage has been received.';
    $response_error = 'Error. Please try again.';
    $subject =  filter($subject);
    $url = "Origin Page: ".$_SERVER['HTTP_REFERER'];
    $ip = "IP Address: ".$_SERVER["REMOTE_ADDR"];
    $message = $content."\n$ip\r\n$url";

    // Validate return email & inform admin
    $emailto = filter($emailto);

    // Setup final message
    $body = wordwrap($message);

    if($use_smtp == '1'){

        $SmtpServer = 'SMTP SERVER';
        $SmtpPort = 'SMTP PORT';
        $SmtpUser = 'SMTP USER';
        $SmtpPass = 'SMTP PASSWORD';

        $to = $emailto;
        $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
        $SMTPChat = $SMTPMail->SendMail();
        $response = $SMTPChat ? $response_sent : $response_error;

    } else {

        // Create header
        $headers = "From: $from\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/plain; charset=utf-8\r\n";
        $headers .= "Content-Transfer-Encoding: quoted-printable\r\n";

        // Send email
        $mail_sent = @mail($emailto, $subject, $body, $headers);
        $response = $mail_sent ? $response_sent : $response_error;

    }
    return $response;
}

// Remove any un-safe values to prevent email injection
function filter($value) {
    $pattern = array("/\n/", "/\r/", "/content-type:/i", "/to:/i", "/from:/i", "/cc:/i");
    $value = preg_replace($pattern, "", $value);
    return $value;
}

exit;

?>

我不懂PHP,所以我不知道错误是什么!

1 个答案:

答案 0 :(得分:0)

您好,

问题在于这一行:

if($use_smtp == '1'){

尝试制作$ GLOBALS [&#39; user_smtp&#39;]而不是$ user_smtp或将值传递给函数。

如果你没有将变量传递给函数PHP不知道它,因为它不在里面。

我会尝试更好地解释它。

您的代码的第一行

$use_smtp = '0'

用这个你创建一个变量名称use_smtp并给它字符串&#34; 0&#34;。 (它在主程序中)。

然后你有一个功能&#34; sendEmail&#34;。在那里你尝试使用这个名为&#34; use_smtp&#34;的变量。但是对于这个功能它不存在,因为只是&#34; main&#34;程序知道它不是功能。你不得不写

$use_smtp = '0'

在函数中或通过变量传递,如:

sendEmail($subject, $content, $emailto, $emailfrom, $use_smtp) 

或更改代码行

if($use_smtp == '1'){

if($GLOBALS['use_smtp'] == '1'){