PHP嵌套';如果';检查收件人和发件人邮件/电子邮件转发的代码疑难解答

PHP嵌套';如果';检查收件人和发件人邮件/电子邮件转发的代码疑难解答,php,email,if-statement,nested,xampp,Php,Email,If Statement,Nested,Xampp,我只是从表单(MACOS上的XAMPP)发送电子邮件,在使用嵌套的“如果”检查转发给收件人和发件人时遇到了问题。我从其他类似的线程借用了一些代码,谢谢。运行程序时,浏览器中没有描述PHP错误,但也没有执行“echo”,表单只是重置。我怀疑我只是在嵌套时出错了?另外,我在PHP代码末尾没有使用“else”herdoc,但这不是文件的单一消息/电子邮件版本的问题。任何节省时间的建议,谢谢 <?php //check form submit if (filter_has_var(INPUT_PO

我只是从表单(MACOS上的XAMPP)发送电子邮件,在使用嵌套的“如果”检查转发给收件人和发件人时遇到了问题。我从其他类似的线程借用了一些代码,谢谢。运行程序时,浏览器中没有描述PHP错误,但也没有执行“echo”,表单只是重置。我怀疑我只是在嵌套时出错了?另外,我在PHP代码末尾没有使用“else”herdoc,但这不是文件的单一消息/电子邮件版本的问题。任何节省时间的建议,谢谢

<?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
}
?> 
正是这一点促使人们搜索这个主题,“return true”是处理嵌套的“if”语句的更常见的建议。它似乎可以从一个单独的HTML文件(下面的代码)发送输入文章,但也可能有错误?我还尝试了一些其他的策略,比如在两个“邮件”调用中使用“&&”、“和”,但这些策略始终会导致解析错误

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

您的逻辑不正确。mail函数返回一个
true
,因此您的

if (true) {echo "SUCCESS";
不需要,并且应删除
返回true
,以便脚本不会终止

如果邮件已成功接受传递,则返回TRUE,否则返回FALSE

-

您还需要在第一个
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";
    }
}
?>
ifs和else之间的嵌套非常重要,因为脚本可能会成功发送消息/邮件,但不会执行回显,甚至更糟。返回检查常规PHP邮件的代码结构,结果似乎没有发生任何变化,如OP中所述。对于gmail出现持续身份验证错误且所有其他途径都失败的任何人,这里有一个链接对我有效,但我必须定期使用它,偶尔清除缓存并重置服务器。
希望有帮助

谢谢,下面是新代码,结果没有变化>请编辑问题以显示新代码。无法在评论中阅读我一直在尝试在Mac上使用XAMPP配置smtp,我发现这很麻烦,关于堆栈溢出的建议主要针对windows,与我所知道的不同之处在于Mac版本没有sendmail.ini文件,而是sendmail.php和sendmail.pdf(etc文件夹)。我阅读了pdf中smtp和php mailer的说明,但由于之前的文件更改,即使在重新安装XAMP之后,这两个版本都不起作用。当时,我决定使用echo执行来衡量php脚本的性能(忽略尚未发送的电子邮件),这可能是一个错误?在第一个
if(mail($to,$subject,$message,$headers))上需要一个
else
){
也一样。否则您将不会输入第二个
if
,并且您的
错误
输出将永远不会呈现。不确定您对服务器配置的要求是什么,这是一个不同的问题,虽然我不遵循,但通常在第一个“if”之后是“else if”,您是否能够用呈现suc的正确代码编辑您的答案是否成功?服务器配置为上下文。
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";
}
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";
    }
}
?>