2 Replies - 299 Views - Last Post: 13 November 2013 - 02:35 AM Rate Topic: -----

#1 d0nni  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 13-December 10

SMTP verification. fread() problem.

Posted 11 November 2013 - 02:47 AM

Hi there,


Im trying to make an email verification method for a captive portal.

at the moment im just running tests to make sure the code works before integrating it into the final project. however at the moment i am having a problem with a few lines of code. i keep getting this warning: fread(): length parameter must be greater than 0.

the code i have at the moment is this:

<?php

$em='xxxxxx@xxxx-int.net';
$message='please work!';

	$ar = explode('@', $em);
	$name=$ar[0];
	$domain=$ar[1];

	$hosts = array();
	getmxrr($domain, $hosts);
	

	foreach($hosts as $host)
		{
			$handle = smtp_connect($host, 25, 30, 1, 1, 1);
    			echo smtp_command($handle, "EHLO $host\r\n", 1, 1);
    			echo smtp_command($handle, "MAIL FROM:xxxx@hotmail.com\r\n", 1, 1);
    			echo smtp_command($handle, "RCPT TO:$em\r\n", 1, 1);
    			echo smtp_command($handle, "DATA\r\n", 1, 1);
    			echo smtp_command($handle, "$message\r\n.\r\n", 1, 1);


    			// don't do it like this - it will hang up
    			// echo smtp_command($handle, "$message", 1, 1);
    			// echo smtp_command($handle, "\r\n.\r\n", 1, 1);
    			echo smtp_command($handle, "QUIT\r\n", 1, 1);
    			smtp_close($handle); 
		}

    function smtp_connect($host, $port, $timeout=30, $echo_command=False, $echo_response=False, $nl2br=False)
    {
        $errno = 0;
        $errstr = 0; 
        if($echo_command)
        {
            if($nl2br) { echo nl2br("CONNECTING TO $host\r\n"); }
            else { echo "CONNECTING TO $host\r\n"; }
        }

        $handle = fsockopen($host, $port, $errno, $errstr, $timeout);

        if(!$handle)
        {
            if($echo_command)
            {
                if($nl2br) { echo nl2br("CONNECTION FAILED\r\n"); }
                else { echo "CONNECTION FAILED\r\n"; }
            }
            return False;
        }

        if($echo_command)
        {
            if($nl2br) { echo nl2br("SUCCESS\r\n"); }
            else { echo "SUCCESS\r\n"; }
        }

        $response = fgets($handle,1);
        $bytes_left = socket_get_status($handle);

        if ($bytes_left > 0) {
		 $response .= fread($handle, $bytes_left["unread_bytes"]); }

        if($echo_response)
        {
            if($nl2br) { echo nl2br($response); }
            else { echo $response; }
        }
        return $handle;
    }

    function smtp_command($handle, $command, $echo_command=False, $nl2br=False)
    {
        if($echo_command)
        {
            if($nl2br) { echo nl2br($command); }
            else { echo $command; }
        }

        fputs($handle, $command);
        $response = fgets($handle,1);
        $bytes_left = socket_get_status($handle);


        if ($bytes_left > 0) { 
		$response .= fread($handle, $bytes_left["unread_bytes"]); }



        if($nl2br) { return nl2br($response); }
        else { return $response; }
    }
	return $handle;
    
    function smtp_close($handle)
    {
        fclose($handle);
    }

?>


Would anyone be able to shed some light as to why this would be saying this and how i would go about fixing it?

Thanks in advance,
Andrew

This post has been edited by d0nni: 11 November 2013 - 02:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: SMTP verification. fread() problem.

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 5,979
  • Joined: 08-June 10

Re: SMTP verification. fread() problem.

Posted 11 November 2013 - 04:22 PM

Hey.

Please provide the full message, including the line numbers. That way we know where to start looking for the problem.

That said, this seems to be done in at least two places I can see:
$bytes_left = socket_get_status($handle);

if ($bytes_left > 0) { 
    $response .= fread($handle, $bytes_left["unread_bytes"]); 
}


Your use of the $bytes_left return value seems somewhat contradictory. First you compare it to an integer, and then you use it as an array. A look through the manual entry for the socket_get_status function (or rather it's alias: stream_get_meta_data) tells us that it does in fact return an array, one element of which is an integer called unread_bytes.

So, your condition seems to be the problem. You need to check $bytes_left["unread_bytes"] instead of just $bytes_left. - I'd suggest renaming the variable. It's kind of misleading, which no doubt contributed to this.
Was This Post Helpful? 0
  • +
  • -

#3 d0nni  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 13-December 10

Re: SMTP verification. fread() problem.

Posted 13 November 2013 - 02:35 AM

Hi, thanks for the reply.

i ended up getting around this problem. however it just led to further problems, so i set up a different method to do this, with a better structure for more control over each action. but thanks for getting back to me.

Andrew
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1