skip to Main Content

I’m just working through sending e-mails from form (XAMPP on MACOS) and hitting a snag when using a nested ‘if’ to check forwarding to recipient and sender. I borrowed some code from other similar threads thank you. There are no PHP errors described in the browser when running the program but there is also no ‘echo’ executed and the form just resets. I suspect I just made an error with the nesting? Also I’m not using an ‘else’ HEREDOC at the end of the PHP code but that wasn’t an issue with single message/email version of the file. Any time saving suggestions appreciated!

<?php
//check form submit
if (filter_has_var(INPUT_POST, "Submit")){
  $submit = filter_input(INPUT_POST, "Submit");
  $to  = "[email protected]";
  $from = filter_input(INPUT_POST, "Email");
  $first_name = filter_input(INPUT_POST, "First_Name");
  $last_name = filter_input(INPUT_POST, "Last_Name");
  $subject = filter_input(INPUT_POST, "Subject");
  $subject2 = "Copy of your form submission;". " " . $subject;
  $message= $first_name . " " . $last_name . " wrote the following:" . "nn" . filter_input(INPUT_POST, "Message");
  $message2 = "Here is a copy of your message " . $first_name . "nn" . filter_input(INPUT_POST, "Message");
  $headers = "From:" . $from;
  $headers2 = "From:" . $to;
    //check first mail sent
    if (mail($to,$subject,$message,$headers)) {
        //check second mail sent
        if (mail($from,$subject2,$message2,$headers2)) {
            return true;
            //if both conditions met
            if (true) {echo "SUCCESS";
                      } else {
                echo "ERROR";
            }
        //close second mail check
        }
        //close first mail check
    }
    //close form submit check
}
?> 

Utilising the code suggested in below answer appears to make no difference to the outcome described above.

if (mail($to,$subject,$message,$headers)) {
    //check second mail sent
    if (mail($from,$subject2,$message2,$headers2)) {
        echo "SUCCESS";
    } else {
        echo "ERROR";
    }
  //end check mails
    }
//end check form submit
}
?> 

It was exactly that which prompted a search of the topic with ‘return true’ being the more common suggestion for handling nested ‘if’ statements. It appeared to work ok with a similar file that sent the INPUT_POST from a seperate HTML file (code below) but it’s possible that has the error also? I also tried a few other strategies like ‘&&’, ‘and’ with the two ‘mail’ calls but those consistently caused parsing errors.

<?php
$to  = "[email protected]";
$from = filter_input(INPUT_POST, "Email");
$first_name = filter_input(INPUT_POST, "First_Name");
$last_name = filter_input(INPUT_POST, "Last_Name");
$subject = filter_input(INPUT_POST, "Subject");
$subject2 = "Copy of your form submission;". " " . $subject;
$message= $first_name . " " . $last_name . " wrote the following:" . "nn" . filter_input(INPUT_POST, "Message");
$message2 = "Here is a copy of your message " . $first_name . "nn" . filter_input(INPUT_POST, "Message");
$headers = "From:" . $from;
$headers2 = "From:" . $to;
if (mail($to,$subject,$message,$headers)) {
    if (mail($from,$subject2,$message2,$headers2)) {
        return true;
    }
}
if (true) { echo "SUCCESS";
           //echo "<script type='text/javascript'>alert('SUCCESS');</script>";
          } else { echo "ERROR";
                  //echo "<script type='text/javascript'>alert('ERROR');</script>";
                 }
?> 

2

Answers


  1. Chosen as BEST ANSWER

    PEAR Mail appears to provide a solution that works:

    if (filter_has_var(INPUT_POST, "Submit")){
    $submit = filter_input(INPUT_POST, "Submit");
    require "Mail.php";
    $to      = "[email protected]";
    $from = filter_input(INPUT_POST, "Email");
    $first_name = filter_input(INPUT_POST, "First_Name");
    $last_name = filter_input(INPUT_POST, "Last_Name");
    $subject = filter_input(INPUT_POST, "Subject");
    $message= $first_name . " " . $last_name . " wrote the following:" . "nn" . filter_input(INPUT_POST, "Message");  
    $host    = "smtp.gmail.com";
    $port    =  "587";
    $user    = "[email protected]";
    $pass    = "EnterYourPassword";
    $headers = array("From"=> $from, "To"=>$to, "Subject"=>$subject);
    $smtp    = @Mail::factory("smtp", array("host"=>$host, "port"=>$port, "auth"=> true, "username"=>$user, "password"=>$pass));
    $mail    = @$smtp->send($to, $headers, $message);
    $mail2   = @$smtp->send($from, $headers, $message);
    
        if (PEAR::isError($mail)){
            echo "error: {$mail->getMessage()}";
            if (PEAR::isError($mail2)){
                echo "error: {$mail2->getMessage()}";
            }
        } else {
            echo "Message sent";
        }
    }
    ?>
    

    The nesting between the ifs and the else is important as the script may send the message/mail successfully but not execute the echo, or worse. Going back to check that code structure with the regular PHP Mail again appeared to produce no change in outcome as described in OP. For anyone having persistent authentication errors with gmail with all other avenues failing here is a link that worked for me but I have to use it regularly along with occasionally clearing the caches and resetting the servers. https://g.co/allowaccess Hope that helps!


  2. Your logic is incorrect. The mail function returns a true already so your

    if (true) {echo "SUCCESS";
    

    is not needed and the return true should be removed so your script doesn’t terminate.

    Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

    https://www.php.net/manual/en/function.mail.php

    if (mail($to,$subject,$message,$headers)) {
        //check second mail sent
        if (mail($from,$subject2,$message2,$headers2)) {
            echo "SUCCESS";
        } else {
            echo "ERROR";
        }
    

    You also need an else on your first if because you’ll never enter the second if the first fails.

    if (mail($to,$subject,$message,$headers)) {
            //check second mail sent
            if (mail($from,$subject2,$message2,$headers2)) {
                echo "SUCCESS";
            } else {
                echo "ERROR";
            }
    } else {
        echo "Error from first mail call";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search