0

I'm trying to troubleshoot an error with <mail> not being sent. Long story short - I've added this error handler, to the contact form. It's reporting the following:-

E_NOTICE Error in file �EmailScript.php� at line 72: Undefined index: action The error was at line 66 but I've added what I thought was the solution:- lines 65 through 70. I've also tried to give both `$_request and $action empty array "foo bar" values, among other attempted solutions.

This ones above my understanding guys, any chance of a few hints?

<?php

// ----------------------------------------------------------------------------------------------------
// - Display Errors
// ----------------------------------------------------------------------------------------------------
ini_set('display_errors', 'On');
ini_set('html_errors', 0);

// ----------------------------------------------------------------------------------------------------
// - Error Reporting
// ----------------------------------------------------------------------------------------------------
error_reporting(-1);

// ----------------------------------------------------------------------------------------------------
// - Shutdown Handler
// ----------------------------------------------------------------------------------------------------
function ShutdownHandler()
{
    if(@is_array($error = @error_get_last()))
    {
        return(@call_user_func_array('ErrorHandler', $error));
    };

    return(TRUE);
};

register_shutdown_function('ShutdownHandler');

// ----------------------------------------------------------------------------------------------------
// - Error Handler
// ----------------------------------------------------------------------------------------------------
function ErrorHandler($type, $message, $file, $line)
{
    $_ERRORS = Array(
        0x0001 => 'E_ERROR',
        0x0002 => 'E_WARNING',
        0x0004 => 'E_PARSE',
        0x0008 => 'E_NOTICE',
        0x0010 => 'E_CORE_ERROR',
        0x0020 => 'E_CORE_WARNING',
        0x0040 => 'E_COMPILE_ERROR',
        0x0080 => 'E_COMPILE_WARNING',
        0x0100 => 'E_USER_ERROR',
        0x0200 => 'E_USER_WARNING',
        0x0400 => 'E_USER_NOTICE',
        0x0800 => 'E_STRICT',
        0x1000 => 'E_RECOVERABLE_ERROR',
        0x2000 => 'E_DEPRECATED',
        0x4000 => 'E_USER_DEPRECATED'
    );

    if(!@is_string($name = @array_search($type, @array_flip($_ERRORS))))
    {
        $name = 'E_UNKNOWN';
    };

    return(print(@sprintf("%s Error in file \xBB%s\xAB at line %d: %s\n", $name, @basename($file), $line, $message)));
};

$old_error_handler = set_error_handler("ErrorHandler");

// My code


if(isset($_REQUEST['action'])){
    $action = $_REQUEST['action'];
}
if(isset($action)){ 
    echo $action;
}

$action=$_REQUEST['action'];
if ($action=="")    /* display the contact form */
    {
    ?>
    <form  action="" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="action" value="submit">
    Your name:<br>
    <input name="name" type="text" value="" size="30"/><br>
    Your email:<br>
    <input name="email" type="text" value="" size="30"/><br>
    Your message:<br>
    <textarea name="message" rows="7" cols="30"></textarea><br>
    <input type="submit" value="Send email"/>
    </form>
    <?php
    } 
else                /* send the submitted data */
    {
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];
    if (($name=="")||($email=="")||($message==""))
        {
        echo "All fields are required, please fill <a href=\"\">the form</a> again.";
        }
    else{        
        $from="From: $name<$email>\r\nReturn-path: $email";
        $subject="Message sent using your contact form";
        mail("asiyreh@hotmail.co.uk", $subject, $message, $from);


        }
    }  
?>
Kevin Lynn
  • 11
  • 3
  • Do you get the error before submitting the form (i.e. upon page load)? – showdev Jan 28 '20 at 03:04
  • Yes it does @showdev - immediately upon loading the page - before submitting any values to the form – Kevin Lynn Jan 28 '20 at 03:07
  • It seems you're trying to access a `$_REQUEST` key that doesn't exist (i.e. "Undefined index"). This likely happens the second time you define `$action` : `$action=$_REQUEST['action'];`. You only need to define it once, and you might consider using PHP's [ternary operator](https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) to define it regardless of its existence in the `$_REQUEST` (or `$_POST`) array: `$action = !empty($_POST['action']) ? $_POST['action'] : '';` – showdev Jan 28 '20 at 03:10
  • Thx @DroidDev. You guys are the best on the net with out a doubt, but your punishment for anything with a sniff of duplication is also - SWIFT and lethal - :D – Kevin Lynn Jan 28 '20 at 03:11
  • It was only defined once @showdev - Originally the error was at line 66 - I've added lines 65 through 70. But even if I take it away I get the same error – Kevin Lynn Jan 28 '20 at 03:13
  • @KevinLynn Did you get rid of the error? – Boopathi D Jan 28 '20 at 03:14
  • Going to try these Tenary Ops @showdev – Kevin Lynn Jan 28 '20 at 03:15
  • I've removed lines 65 through 70 and in place added - $action = !empty($_POST['action']) ? $_POST['action'] : ''; It's reporting the same error at line 67 – Kevin Lynn Jan 28 '20 at 03:17
  • What code is currently on line 67? – showdev Jan 28 '20 at 03:18
  • replace line 67 as `$action = isset($_POST['action']) ? $_POST['action'] : '';` – Boopathi D Jan 28 '20 at 03:20
  • Guys this is slightly off topic but it may aid some future poor soul that is attempting this general task. The main problem was a server issue as pointed out in some earlier threads. I solved the overall problem of PHP email by installing a solution called PHPmailer. It's available here - https://github.com/PHPMailer/PHPMailer - I suggest you find a recent tutorial for it, as there is a little configuration to do. That config method seems to change often, so go for a recent tut... Best of luck! – Kevin Lynn Jan 28 '20 at 21:43

2 Answers2

0

Please correct the lines from 62 to 70.

// My code

$action='';
if(isset($_REQUEST['action'])){
    $action = $_REQUEST['action'];
}

if ($action=="")    /* display the contact form */
    {

because you need to define the variable $action.

Boopathi D
  • 361
  • 2
  • 21
0

This worked to solve the E report error. Something I found in a thread the site recommended to me.

<?php

// ----------------------------------------------------------------------------------------------------
// - Display Errors
// ----------------------------------------------------------------------------------------------------
ini_set('display_errors', 'On');
ini_set('html_errors', 0);

// ----------------------------------------------------------------------------------------------------
// - Error Reporting
// ----------------------------------------------------------------------------------------------------
error_reporting(-1);

// ----------------------------------------------------------------------------------------------------
// - Shutdown Handler
// ----------------------------------------------------------------------------------------------------
function ShutdownHandler()
{
    if(@is_array($error = @error_get_last()))
    {
        return(@call_user_func_array('ErrorHandler', $error));
    };

    return(TRUE);
};

register_shutdown_function('ShutdownHandler');

// ----------------------------------------------------------------------------------------------------
// - Error Handler
// ----------------------------------------------------------------------------------------------------
function ErrorHandler($type, $message, $file, $line)
{
    $_ERRORS = Array(
        0x0001 => 'E_ERROR',
        0x0002 => 'E_WARNING',
        0x0004 => 'E_PARSE',
        0x0008 => 'E_NOTICE',
        0x0010 => 'E_CORE_ERROR',
        0x0020 => 'E_CORE_WARNING',
        0x0040 => 'E_COMPILE_ERROR',
        0x0080 => 'E_COMPILE_WARNING',
        0x0100 => 'E_USER_ERROR',
        0x0200 => 'E_USER_WARNING',
        0x0400 => 'E_USER_NOTICE',
        0x0800 => 'E_STRICT',
        0x1000 => 'E_RECOVERABLE_ERROR',
        0x2000 => 'E_DEPRECATED',
        0x4000 => 'E_USER_DEPRECATED'
    );

    if(!@is_string($name = @array_search($type, @array_flip($_ERRORS))))
    {
        $name = 'E_UNKNOWN';
    };

    return(print(@sprintf("%s Error in file \xBB%s\xAB at line %d: %s\n", $name, @basename($file), $line, $message)));
};

$old_error_handler = set_error_handler("ErrorHandler");

// My code

$action = $_REQUEST['action'] ?? '';

if ($action=="")    /* display the contact form */
    {
    ?>
    <form  action="" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="action" value="submit">
    Your name:<br>
    <input name="name" type="text" value="" size="30"/><br>
    Your email:<br>
    <input name="email" type="text" value="" size="30"/><br>
    Your message:<br>
    <textarea name="message" rows="7" cols="30"></textarea><br>
    <input type="submit" value="Send email"/>
    </form>
    <?php
    } 
else                /* send the submitted data */
    {
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];
    if (($name=="")||($email=="")||($message==""))
        {
        echo "All fields are required, please fill <a href=\"\">the form</a> again.";
        }
    else{        
        $from="From: $name<$email>\r\nReturn-path: $email";
        $subject="Message sent using your contact form";
        mail("asiyreh@hotmail.co.uk", $subject, $message, $from);


        }
    }  
?>

However now I'm not getting an error at all when I fill the form - It's loading a blank page. From what I read, though don't pretend to completely understand, it's some type of server issue. Apparently installing Xamp fixes it but I'm the server is a raspberry pi so no Xamp only LAMP. I'll start again tomorrow guys long day thx for the assists.

Kevin Lynn
  • 11
  • 3