below page is process.php of a script.
This page sends emails at PHP 5.4 but not at PHP7.

ı couldnt find the error , help please.

<?php
include("functions/settings.php");
$errors=0;
$error="The following errors occured while processing your form input.<ul>";
$name=$_POST["name"];
$email=$_POST["email"];
$yourfile=$_FILES['yourfile'];
if($name=="" || $email=="" ){
$errors=1;
$error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
}
if($_FILES['yourfile']['tmp_name']==""){ }
 else if(!is_uploaded_file($_FILES['yourfile']['tmp_name'])){
$error.="<li>The file, ".$_FILES['yourfile']['name'].", was not uploaded!";
$errors=1;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error.="<li>Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
	if (
preg_match('~^application/msword$~i', $_FILES['yourfile']['type']) or
preg_match('~^application/pdf$~i', $_FILES['yourfile']['type']) or 
preg_match('~^text/plain$~i', $_FILES['yourfile']['type']) or
preg_match('~^application/vnd.openxmlformats-officedocument.wordprocessingml.document$~i', $_FILES['yourfile']['type'])
	) 
{ 
// Handle the file...
$image_part = $_FILES['yourfile']['name'];
$image_list[2] = $image_part;
copy($_FILES['yourfile']['tmp_name'], "../files/".$image_part);
$where_form_is="http"."://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
$where_form_is = dirname($where_form_is);
$message="
name : ".$name."
email : ".$email."
your file : ".$where_form_is."/files/".$image_list[2]."
";
$message = stripslashes($message);
mail($notify_email,"Form Submitted at your website",$message,"From: Sheryl");
$make=fopen("data.dat","a");
$to_put="";
$to_put .= $name."|".$email."|".$where_form_is."/files/".$image_list[2]."
";
fwrite($make,$to_put);
} 
else 
{ 
include 'error.php'; 
exit(); 
}
?>
<!-- This is the content of the Thank you page, be careful while changing it -->
<?php include "header.php" ?>
<h3 align="center">Thank you <?php echo $name; ?> !</h3>
<h3 align="center">  Your text file has been successfully uploaded.</h3>
<hr align="center" width="300" size="2" />
<h4 align="center"> <a href="edit.php" title="List of Uploaded files" target="_self">List of Uploaded files</a> | <a href="index.php" title="Upload again?" target="_self">Upload again?</a> | <a href='logout.php'>Logout</a> </h4> 
<!-- Do not change anything below this line -->
<?php 
}
?>
<?php include "footer.php" ?>

    Couple things to try to debug:

    1. Test whether or not the mail() call thinks it's successful or not, maybe something like:
      $result = mail(/* your parameters here */);
      if($result == false) {
          // log or echo something here indicating it failed
      }
      
      For debugging, it can help to turn on all error message output (though you want to turn off diplay_errors on the production site once you've fixed this):
      <?php
      ini_set('display_errors', true);
      error_reporting(E_ALL);
      // rest of code . . .
      

      AFAIK, there's nothing in the posted code that changed with php7. Where sort of error or symptom are you getting that leads you to believe that the code doesn't work? Do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would help by reporting and displaying all the errors it detects?

      Also, what part of the code is working? Is the code running at all, so that you are getting some output from it? Is the file upload moving the file to the ../files/ folder?

      Lastly, the code wasn't the best, before the php version upgrade, in terms of detecting and reporting problems, so it may not be helping you to find where in it the problem is occurring it. Some of the issues -

      1) It's not detecting if a form was submitted at all, before trying to reference any of the form data.

      2) It's not detecting if the upload worked before trying to use the uploaded file information. There's a condition that commonly occurs with uploaded files where both the $POST and $FILES arrays are empty. Your code must detect and handle this.

      3) The code is testing if the ['tmp_name'] is empty, but it's not reporting any error if it is. After testing for items #1 and #2 in this list, the code should test the uploaded ['error'] element to find if the file was uploaded or not and for some of the possible errors, such as the size or not selecting a file at all, the code should set up a user error message telling why the upload didn't work.

      4) copy(), because it doesn't test if the file being operated on was actually the result of a file upload, shouldn't be used. Use move_uploaded_file() instead. The code should also test and report an error (log the actual php error and set up a generic failure message for the visitor) if the file cannot be moved.

      5) The ['type'] element from the uploaded file information can be set to anything and shouldn't be relied on. Also, different browsers (and probably different versions of the same browser) will give different values for the same file. The current best advice is to use the php function mime_content_type() to find the mime type from the file itself. You should also move the uploaded files into a folder that doesn't have direct http access so that someone cannot upload a .php script (which can be made to appear as though it has one of the expected/safe mime types) and be able to browse to it.

      6) And this is probably the most important point for the current problem, the value returned by the mail() function is not being tested. The code should test the value from the mail() function, capture and log the last php error if the mail() call failed, and output a generic failure message to the visitor. The code should only output the 'success' thankyou content if the mail() call returned a true value (this doesn't guarantee that the email will be sent and received, just that php was able to give the email to the sending mail server.)

        I changed mail() line code
        from

        mail($notify_email,"Form Submitted at your website",$message,"From: Sheryl");

        to

        $result = mail(/ your parameters here /);
        if($result == false) {
        // log or echo something here indicating it failed
        }

        and also put

        ini_set('display_errors', true);
        error_reporting(E_ALL);

        to the beginning of the page,

        if i run process.php only at browser page gives no error about mail.
        But only gives following errors :

        Notice: Undefined index: name in /home/...../process.php on line 7

        Notice: Undefined index: email in /home/...../process.php on line 8

        Notice: Undefined index: yourfile in /home/...../process.php on line 9

        Notice: Undefined index: yourfile in /home/...../process.php on line 14
        The following errors occured while processing your form input.

        You did not enter one or more of the required fields. Please go back and try again.
        Invalid email address entered 

        If I run index.php at browser where upload form is then process.php opens but there is no any error showing.
        Form uploads the file but dont email to us anything. same form emails at php 5.4 but not at php7

        if it helps here is index.php codes also.

        <?php
        ini_set('display_errors', true);
        error_reporting(E_ALL);

        include('check.php');
        check_login('3');
        include('header.php');

        echo "<form enctype='multipart/form-data' action='process.php' method='post'>
        <table width='550' border='0' align='center' cellpadding='3' cellspacing='1' bordercolor='#000066' >
        <tr>
        <td colspan='2' bgcolor='#B5CBEF' height='30' width='100%' bordercolor='#FFFFFF' background='tile_back.gif'>
        <div align='center'><strong><font size='+1' face='Verdana, Geneva, sans-serif'>File Upload Form</font></strong></div></td>
        </tr><tr><td width='100%' height='16' colspan='2' align='center' bordercolor='#FFFFFF' bgcolor='#D6DFEF'><font size='1' face='Verdana'>Please fill in all fields marked with a </font></td></tr><tr>
        <td width='100' height='30' align='center' bordercolor='#FFFFFF' bgcolor='#EFF3F7'>
        <font face='Verdana' size='2'>Name</font></td>
        <td width='300' height='30' bordercolor='#FFFFFF' bgcolor='#EFF3F7'>
        <font face='Verdana'><input type=text name='name' size='25' />
        </font></td>
        </tr><tr>
        <td width='100' height='30' align='center' bordercolor='#FFFFFF' bgcolor='#EFF3F7'>
        <font face='Verdana' size='2'>Email</font></td>
        <td height='30' width='300' bgcolor='#EFF3F7' bordercolor='#FFFFFF'>
        <font face='Verdana'><input name='email' type=text size='25' />
        *</font></td>
        </tr><tr>
        <td width='170' height='30' align='center' bordercolor='#FFFFFF' bgcolor='#EFF3F7'>
        <font face='Verdana' size='2'>Upload a file</font></td>
        <td height='30' width='300' bgcolor='#EFF3F7' bordercolor='#FFFFFF'>
        <font face='Verdana'><input name='yourfile' type='file' size='25' /></font></td></tr><tr><td colspan='2' height='30' bgcolor='#D6DFEF' ><div align='center'><input type=submit value='Submit' style='width: 100px; height: 30px;'/>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type=reset value='Reset' style='width: 100px; height: 30px;'/>
        </div></td></tr>
        <tr><td width='100%' height='30' colspan='2' align='center' bordercolor='#FFFFFF' bgcolor='#FFCE40'><font size='2' face='Verdana'>Note : Only .pdf, .doc, .docx and .txt file uploads are allowed.</font></td></tr>
        </table>
        </form>";

        include "footer.php";
        ?>

          Uh..."/ your parameters here /" was just me typing in a place-holder because I was too lazy to copy/paste the original code. You need to use what you had before, just assigning the result to a variable you can test for failure. Similarly, within the if block, you need to replace my comment with whatever you want to do to notify yourself that it failed, which could be logging something to the php error log, or just outputting something to the browser for now.

            Uh,

            Infact i wrote worngly to my above message
            I changed my line from

            mail("$notify_email","Form Submitted at your website",$message,"From: Sheryl");

            to

            $result = mail("$notify_email","Form Submitted at your website",$message,"From: Sheryl");
            if($result == false) {
            // log or echo something here indicating it failed
            echo "mail failed";
            include 'error.php';
            }

            the rest of my message above is true, page gives no any error about email it doesn't give error "mail failed" or doesnt open error.php..

              I tested much

              following codes send me emils at PHP7.

              1st page :

              ini_set('display_errors', true);
              error_reporting(E_ALL);
              
              $to_address = "VALID EMAIL1 HERE";
              $subject = "This goes in the subject line of the email!";
              $message1 = "This is the body of the email.\n\n";
              $message1 .= "More body: probably a variable.\n";
              $headers = "From: VALID EMAIL2 HERE\r\n";
              $result = mail("$to_address","$subject","$message1","$headers");
              if($result == false) {
                  echo "mail failed";
              } else {
              echo "Mail Sent.";
              }
              

              the above page sends me email.
              But e.g. if i add simple code to the end of above codes e.g.

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

              then it doesn't send me emails.

              2nd page :

              ini_set('display_errors', true);
              error_reporting(E_ALL);
              
              $to_address = "VALID EMAIL1 HERE";
              $subject = "This goes in the subject line of the email!";
              $message1 = "This is the body of the email.\n\n";
              $message1 .= "More body: probably a variable.\n";
              $headers = "From: VALID EMAIL2 HERE\r\n";
              $result = mail("$to_address","$subject","$message1","$headers");
              if($result == false) {
                  echo "mail failed";
              } else {
              echo "Mail Sent.";
              }
              if ($_SERVER["REQUEST_METHOD"] == "POST") {}

              at both pages there is no error and page says "Mail Sent"

              But from second page i dont get any emails.

              What can be the problem...?

                I cannot imagine why that last line would affect the mail, assuming that is truly the only difference. 😕

                  it is really interesting NogDog,
                  If i change that last line with

                  if(isset($_POST['submit'])) { }

                  I get emails from above codes.

                    I corrected my page codes better..

                    But still can not get emails to my gmail, hotmail, although script says "email is sent"

                    <?php
                    ini_set('display_errors', true);
                    error_reporting(E_ALL);
                    // Control name field from form
                    if(isset($_POST['name'])){
                    	if (empty($_POST["name"])) {
                        echo "Name is required";
                      } else {
                    $name = $_POST['name'];
                    // check if name only contains letters and whitespace
                        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                          echo "Only letters and white space allowed";
                    	  } } }
                    
                    // Control email field from form
                    if(isset($_POST['email'])){
                    	if (empty($_POST["email"])) {
                        echo "Email is required";
                      } else {
                    	$email = $_POST['email'];
                    	// check if e-mail address is well-formed
                        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                          echo "Invalid email format";
                        } } } 
                    
                    
                    if(isset($_FILES['yourfile'])){
                          $errors= array();
                          $file_name = $_FILES['yourfile']['name'];
                          $file_tmp =$_FILES['yourfile']['tmp_name'];
                          $file_type=$_FILES['yourfile']['type'];
                    	  $a=explode('.',$_FILES['yourfile']['name']);
                          $file_ext=strtolower(end($a));
                          $extensions= array("pdf","txt","doc","docx");
                    
                      // check ile extension
                      if(in_array($file_ext,$extensions)=== false){ $errors[]="extension not allowed, please choose a pdf, txt or doc file."; }
                      if(empty($errors)==true){
                    	  // Handle the file...
                      if(!is_uploaded_file($file_tmp)){ echo "<li>The file, ".$file_name.", was not uploaded!"; }
                      else { move_uploaded_file($file_tmp,"../files/".$file_name); 	  
                    		// copy($_FILES['yourfile']['tmp_name'], "../files/".$file_name);
                    include("functions/settings.php");
                    $where_form_is="http"."://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
                    $where_form_is = dirname($where_form_is);
                    $message="
                    name : ".$name."
                    email : ".$email."
                    your file : ".$where_form_is."/files/".$file_name."
                    ";
                    $message = stripslashes($message);
                    $result = mail("$notify_email","Form Submitted at your website",$message,"From: Sheryl");
                    if($result == false) { echo "mail not sent"; } 
                    else { echo "Mail is sent."; }
                    
                    $make=fopen("data.dat","a");
                    $to_put="";
                    $to_put .= $name."|".$email."|".$where_form_is."/files/".$file_name."
                    ";
                    fwrite($make,$to_put);
                    // This is the content of the Thank you page
                    include "header.php";
                    echo "<h3 align='center'>Dear <font color=\'#333399\'>$name</font></h3>";
                    echo "<h3 align='center'>  Your text file has been successfully uploaded.</h3>";
                    echo "<hr align='center' width='300' size='2' />";
                    echo "<h4 align='center'> <a href='edit.php' title='List of Uploaded files' target='_self'>List of Uploaded files</a> | <a href='index.php' title='Upload again?' target='_self'>Upload again?</a> | <a href='logout.php'>Logout</a> </h4> ";
                    echo "Success";
                    } }
                    else { 
                    print_r($errors);
                    // include 'error.php'; 
                    // exit();  
                    } } ?> <!-- This is the content of the Thank you page, be careful while changing it --> <!-- Do not change anything below this line --> <?php include "footer.php" ?>

                      I applied headers to mail() function to my above code also.
                      like that :

                      $headers = array("From: valid email from hotmail",
                      "Reply-To: replyto@example.com",
                      "X-Mailer: PHP/" . PHP_VERSION
                      );
                      $headers = implode("\r\n", $headers);
                      $result = mail($notify_email,"Form Submitted at your website",$message,$headers);

                      and didnt get emails to my gmail,yahoo,hotmail .. etc.

                      My hosting is at HOSTGATOR USA.
                      after some trials, i realized that above codes send emails if receiver email address is hosted HOSTGATOR, but i can't get any email from the above codes if i change receiver email outside of HOSTGATOR.

                      I talked with Hostgator, But they say "it is not related with the server"

                      What do you think? is there anything that i can do at php.ini from cpanel, or any other thing ?

                      Thanks

                        Many mail servers require that the "From:" value be a valid email address on that server. Therefore, it may not allow you to use a hotmail address for it. You would either need to change it to your hostgator email, or use SMTP to send the mail (which can be done with a PHP mail package like phpMailer.)

                          Okay, I tested again with your offer NogDog,

                          I changed from address to email address at Hostgator.
                          And tested my final same script above at PHP 5.4, PHP 5.6 and PHP 7 (by changing with php configuration at CPanel for the folder where the script is)

                          Results :
                          At PHP 5.4 and PHP 5.6 Script sends emails to any email (in and outside of of Hostgator emails) even if i change from email to outside of Hostgator. works well.
                          at PHP 7 same problem, I can't get any email even if i change from email to email address at Hostgator

                            Well, you've stumped me, particularly since I don't currently have an easy way to test it on PHP 7. I always use phpMailer using the SMTP method for those occasions when I need to send mail from PHP scripts, so if I were in your shoes, that's probably what I'd be doing. :bemused:

                              🙂
                              Maybe there is a bug at PHP7

                              Some other people has same problems.
                              http://stackoverflow.com/questions/37431095/php-mail-sendmail-not-working-since-apache-2-4-php7-upgrade

                              Anyway, i think it is the time for me to learn that phpMailer and SMTP.
                              I never used it and dont know now how to do.

                              if it will not be difficult for you, can you adapt those php mailer-smtp things to my above script,
                              or can you give me an example codes by which i may test at my server.?

                              Thanks,

                                Here's an example, copy pasted directly from my application. Nothing has been modified, let me know if you have questions / need explanations. The PHPMailer stuff is pretty straight forward, and there are plenty of exmaples depending on how you want to connect. My example below is for connection to GMail using OAuth over SMTP.

                                        $oMailer = new \PHPMailerOAuth();
                                        $oMailer->isSMTP();
                                        $oMailer->SMTPDebug = 0;
                                        $oMailer->Host = 'smtp.gmail.com';
                                        $oMailer->Port = 587;
                                        $oMailer->SMTPSecure = 'tls';
                                        $oMailer->SMTPAuth = true;
                                        $oMailer->AuthType = 'XOAUTH2';
                                        $oMailer->oauthUserEmail = Config::getValue('email', 'user', 'email');
                                        $oMailer->oauthClientId = Config::getValue('email', 'user', 'client_id');
                                        $oMailer->oauthClientSecret = Config::getValue('email', 'user', 'client_secret');
                                        $oMailer->oauthRefreshToken = Config::getValue('email', 'user', 'refresh_token');
                                        $oMailer->setFrom(Config::getValue('email', 'from'), Config::getValue('email', 'from_name'));
                                        $rt = Config::getValue('email', 'reply_to');
                                        if (isset($rt)) {
                                            $oMailer->addReplyTo($rt);
                                        }
                                        $oMailer->addAddress(Config::getValue('website', 'notification_email'));
                                        $oMailer->Subject = $sMessage;
                                        $oMailer->msgHTML(
                                            "<h1>$level</h1><h3>$sMessage</h3><p>Logged from $sCaller</p>"
                                        );
                                        $oMailer->AltBody = "$level: $sMessage \nLogged from $sCaller";
                                        if (!$oMailer->send()) {
                                            $bMailFailed = true;
                                            Factory::buildClass(Logger::class)->emergency('Failed sending email: ' . $oMailer->ErrorInfo);
                                        }
                                
                                  Write a Reply...