1

我已经复制粘贴了一个我正在尝试修改的函数。我想让它正确处理错误。现在,$smtpResponse不返回任何值。当我在函数中回显值时,它们会显示,但$smtpResponse不会产生任何东西,也不会发送真值或假值。

我也尝试返回“失败”和“成功”,但它们没有显示。$response没有显示,但“它起作用了!” 做。为什么函数没有返回任何东西?

我试图输出如下:

//Send e-mail  
$repsonse = authSendEmail($from, $namefrom, $sendto,"", $subject,$message);

//Report success/failure
echo $response;

The culprit: 

function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message) {
    //Connect to the host on the specified port  
    $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, 60);  
    $smtpResponse = fgets($smtpConnect, 515);  

    if(empty($smtpConnect)) {  
        $output = "Failed to connect: $smtpResponse";  
        echo $output;
        return "Fail";
         //return false; 
         //used to read 'return $output' 
    }  

    else {  
        $logArray['connection'] = "Connected: $smtpResponse";  
    }  

    //Request Auth Login  
    fputs($smtpConnect,"AUTH LOGIN" . $newLine);  
    $smtpResponse = fgets($smtpConnect, 515);  
    $logArray['authrequest'] = "$smtpResponse";  

    if(!$smtpResponse) {
        return "Fail";
    }

    //Send username  
    fputs($smtpConnect, base64_encode($username) . $newLine);  
    $smtpResponse = fgets($smtpConnect, 515);  
    $logArray['authusername'] = "$smtpResponse";  

    if(!$smtpResponse) {
        return "Fail";
    }

    //Send password  
    fputs($smtpConnect, base64_encode($password) . $newLine);  
    $smtpResponse = fgets($smtpConnect, 515);  
    $logArray['authpassword'] = "$smtpResponse";  

    if(!$smtpResponse) {
        return "Fail";
    }

    //Say Hello to SMTP  
    fputs($smtpConnect, "HELO $localhost" . $newLine);  
    $smtpResponse = fgets($smtpConnect, 515);  
    $logArray['heloresponse'] = "$smtpResponse";  

    if(!$smtpResponse) {
        return "Fail";
    }

    //Email From  
    fputs($smtpConnect, "MAIL FROM: $from" . $newLine);  
    $smtpResponse = fgets($smtpConnect, 515);  
    $logArray['mailfromresponse'] = "$smtpResponse";  

    if(!$smtpResponse) {
        return "Fail";
    }

    //Email To  
    fputs($smtpConnect, "RCPT TO: $to" . $newLine);  
    $smtpResponse = fgets($smtpConnect, 515);  
    $logArray['mailtoresponse'] = "$smtpResponse";  

    if(!$smtpResponse) {
        return "Fail";
    }

    //The Email  
    fputs($smtpConnect, "DATA" . $newLine);  
    $smtpResponse = fgets($smtpConnect, 515);  
    $logArray['data1response'] = "$smtpResponse";  

    if(!$smtpResponse) {
        return "Fail";
    }

    //Construct Headers  
    $headers = "MIME-Version: 1.0" . $newLine;  
    $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;  
    $headers .= "To: ".$nameto." <".$to.">" . $newLine;  
    $headers .= "From: ".$namefrom." <".$from.">" . $newLine;  
    fputs($smtpConnect, "Subject: $subject\n$headers\n\n $message \n.\n");  
    $smtpResponse = fgets($smtpConnect, 515);  
    $logArray['data2response'] = "$smtpResponse";  

    if(!$smtpResponse) {
        return "Fail";
    }

    // Say Bye to SMTP  
    fputs($smtpConnect,"QUIT" . $newLine);   
    $smtpResponse = fgets($smtpConnect, 515);  
    $logArray['quitresponse'] = "$smtpResponse";   

    if(!$smtpResponse) {
        return "Fail";
    }

    echo "It worked!";
    return "Success"; 
    //insert var_dump here -- uncomment out the next line for debug info
    //var_dump($logArray);
}  
4

2 回答 2

2

:) 你有错字

//Send e-mail  
$repsonse = authSendEmail($from, $namefrom, $sendto,"", $subject,$message);

//Report success/failure
echo $response;

您返回 $ repsonse并回显 $ response

并记录在案。复制/粘贴别人的代码不会带你去任何地方。如果您想学习自己编写代码,或者如果您真的需要复制/粘贴,请尝试了解您正在复制/粘贴的内容。我不想刻薄,只是给你一个建议。祝你好运!

于 2012-05-16T08:44:55.413 回答
0

也许这太简单了,但是,你真的有这样的一行:

//Report success/failure echo $response;

该回声只是评论的一部分,您需要将其放在新行上。

此外,您应该在退出函数之前真正关闭该套接字连接。

此外,恕我直言,在成功时返回布尔值 true 并在失败时返回 false 可能会更好。

于 2012-05-16T07:26:58.627 回答