PHP 嵌套 'if' 代码故障排除以检查收件人和发件人 message/email 转发

PHP nested 'if' code troubleshooting to check recipient and sender message/email forwarding

我正在处理从表单(MACOS 上的 XAMPP)发送电子邮件,并在使用嵌套 'if' 检查转发给收件人和发件人时遇到障碍。我从其他类似线程借用了一些代码,谢谢。 运行 程序在浏览器中没有描述 PHP 错误,但也没有 'echo' 执行并且表单只是重置。我怀疑我只是在嵌套方面犯了错误?此外,我没有在 PHP 代码末尾使用 'else' HEREDOC,但这不是文件的单个 message/email 版本的问题。任何节省时间的建议表示赞赏!

<?php
//check form submit
if (filter_has_var(INPUT_POST, "Submit")){
  $submit = filter_input(INPUT_POST, "Submit");
  $to  = "example@gmail.com";
  $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:" . "\n\n" . filter_input(INPUT_POST, "Message");
  $message2 = "Here is a copy of your message " . $first_name . "\n\n" . 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
}
?> 

使用下面答案中建议的代码似乎对上述结果没有影响。

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
}
?> 

这正是促使搜索主题的原因,'return true' 是处理嵌套 'if' 语句的更常见建议。对于从单独的 HTML 文件(下面的代码)发送 INPUT_POST 的类似文件,它似乎工作正常,但也可能有错误吗?我还尝试了其他一些策略,例如“&&”、'and' 和两个 'mail' 调用,但这些策略始终导致解析错误。

<?php
$to  = "example@gmail.com";
$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:" . "\n\n" . filter_input(INPUT_POST, "Message");
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . 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>";
                 }
?> 

你的逻辑不正确。邮件功能 returns 已经 true 所以你的

if (true) {echo "SUCCESS";

不需要,return true 应该被删除,这样你的脚本就不会终止。

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";
    }

您的第一个 if 还需要一个 else,因为如果第一个失败,您将永远不会进入第二个。

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";
}

PEAR Mail 似乎提供了一个有效的解决方案:

if (filter_has_var(INPUT_POST, "Submit")){
$submit = filter_input(INPUT_POST, "Submit");
require "Mail.php";
$to      = "example@hotmail.com";
$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:" . "\n\n" . filter_input(INPUT_POST, "Message");  
$host    = "smtp.gmail.com";
$port    =  "587";
$user    = "example@gmail.com";
$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";
    }
}
?>

ifselse 之间的嵌套很重要,因为脚本可能会成功发送 message/mail 但不会执行回声,或者更糟。返回检查带有常规 PHP Mail 的代码结构似乎再次没有产生如 OP 中所述的结果变化。对于任何在 gmail 上存在持续身份验证错误且所有其他途径都失败的人来说,这里有一个 link 对我有用,但我必须定期使用它以及偶尔清除缓存和重置服务器。 https://g.co/allowaccess 希望对您有所帮助!