I’ve copy-pasted a function that I’m trying to modify. I’d like to make it handle errors properly. Right now, $smtpResponse doesn’t return any values. When I echo values within the function, they DO show, but $smtpResponse doesn’t yield anything, and neither does sending true or false values.
I tried returning “Fail” and “Success” too, but they’re not showing. $response doesn’t show, but “It worked!” does. Why isn’t the function returning anything?
I’m trying to output as follows:
//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);
}
🙂 you have a typo
you return $repsonse and you echo $response
And for the record. Copy/pasting somebody elses code won’t take you anywhere. If you want to learn to program write the code yourself or if you really need to copy / paste try to understand what you are copy/pasting. I’m not trying to be mean just giving you a word of advice. Good luck!