我已经复制粘贴了一个我正在尝试修改的函数。我想让它正确处理错误。现在,$smtpResponse
不会返回任何值。当我在函数中回显值时,它们会显示,但$smtpResponse
不会产生任何结果,也不会发送true或false值。
我也试过回复“失败”和“成功”,但他们没有表现出来。 $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);
}
答案 0 :(得分:2)
:)你有一个错字
//Send e-mail
$repsonse = authSendEmail($from, $namefrom, $sendto,"", $subject,$message);
//Report success/failure
echo $response;
您返回$ repsonse 并回显$ 回复
并记录在案。复制/粘贴别人的代码将不会带你到任何地方。如果你想学习自己编写程序代码,或者你真的需要复制/粘贴,试着去理解你复制/粘贴的内容。我不是故意只是给你一个忠告。祝你好运!
答案 1 :(得分:0)
也许这太简单了,但你真的有这样一句话:
//Report success/failure echo $response;
回声只是评论的一部分,你需要把它放在一个新的界限上。
此外,您应该在退出函数之前关闭该套接字连接。
此外,成功时返回布尔值true,失败时返回false可能更好,恕我直言。