我已经复制粘贴了一个我正在尝试修改的函数。我想让它正确处理错误。现在,$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);
}
:)您有错别字
//Send e-mail
$repsonse = authSendEmail($from, $namefrom, $sendto,"", $subject,$message);
//Report success/failure
echo $response;
你返回 $repsonse 并回显 $response
并记录在案。复制/粘贴别人的代码不会带你去任何地方。如果您想学习自己编写代码,或者如果您确实需要复制/粘贴,请尝试了解您正在复制/粘贴的内容。我不是想刻薄,只是给你一句忠告。祝你好运!
也许这太简单了,但是,你真的有这样一行吗:
//Report success/failure echo $response;
该回声只是评论的一部分,您需要将其放在新行上。
此外,您应该在退出函数之前关闭该套接字连接。
此外,恕我直言,在成功时返回布尔值 true 并在失败时返回 false 可能会更好。