Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have written the following PHP code for sending mail from HTML form:
<?php
phpinfo();
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
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");
//get data from form
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  //echo($_POST)  ;
  //echo gettype($_POST) . "hello";
$name =$_POST['name'];
echo $name;
$email =$_POST['email'];
echo $email;
$message = $_POST['message'];
echo $message;
$subject =  $_POST['subject'];
echo $subject;

$email    = filter_var($email, FILTER_SANITIZE_EMAIL);
$message = filter_var($message, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$subject = filter_var($subject, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$from = "From: abhinavkishorem9@gmail.com";
$to='to:'.$email;

//$headers = "MIME-Version: 1.0\r\n";
//$headers = "Content-type: text/html\r\n";
$headers = 'From: index.html' . "\r\n" . 'Reply-To: abhinavkishorem9@gmail.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$headers = 'From: about.html' . "\r\n" . 'Reply-To: abhinavkishorem9@gmail.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
if (mail( $to, $subject, $message, $from )) {
    echo "Sent";
} else {
    echo "Failed";
}
}
?>

The thing is in o/p it is showing sent but this is not being reflected in gmail.

What I have tried:

<?php
phpinfo();
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
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");
//get data from form
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  //echo($_POST)  ;
  //echo gettype($_POST) . "hello";
$name =$_POST['name'];
echo $name;
$email =$_POST['email'];
echo $email;
$message = $_POST['message'];
echo $message;
$subject =  $_POST['subject'];
echo $subject;

$email    = filter_var($email, FILTER_SANITIZE_EMAIL);
$message = filter_var($message, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$subject = filter_var($subject, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$from = "From: abhinavkishorem9@gmail.com";
$to='to:'.$email;

//$headers = "MIME-Version: 1.0\r\n";
//$headers = "Content-type: text/html\r\n";
$headers = 'From: index.html' . "\r\n" . 'Reply-To: abhinavkishorem9@gmail.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$headers = 'From: about.html' . "\r\n" . 'Reply-To: abhinavkishorem9@gmail.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
if (mail( $to, $subject, $message, $from )) {
    echo "Sent";
} else {
    echo "Failed";
}
}
?>
Posted
Updated 20-Dec-22 8:15am
Comments
Member 15627495 19-Dec-22 3:03am    
if the mail is sent, have a look in 'spam/junk' mail section.

your second '$headers' replace the first. use '.=' if you need to chain the two strings.
Abhinav Kishore M 19-Dec-22 5:32am    
Hi can u plz elaborate on the 2nd part of ur comment?
Abhinav Kishore M 19-Dec-22 5:40am    
where to use .= in my example?
Abhinav Kishore M 19-Dec-22 3:33am    
Hi checked in spam also. Mail not there in spam as well.
Richard MacCutchan 19-Dec-22 4:18am    
Did the email arrive at the To address?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900