我已经复制粘贴了一个我正在尝试修改的函数。我想让它正确处理错误。现在,$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);
}