Jump to content

Connecting to SMTP / fputs


johnsmith153

Recommended Posts

I am connecting direct to SMTP

 


  // SMTP connection  
  $handle = fsockopen($smtp_server, $port);  
  fputs($handle, "EHLO " . $mydomain . "\r\n");


    //check if able to connect to the server
    $output = fgets($handle, 256);
if(($output[0].$output[1].$output[2])!="220")
{
echo "Can not connect to SMTP server (".$output.")";sendErrorEmail($output);fclose($handle);exit;
}

 

The above works and does what I want to (connects to SMTP and checks ok)

 

However, the SMTP server then has loads of ruibbish to spit out. And what I need to do is:

fgets($handle, 256);
fgets($handle, 256);
fgets($handle, 256);
fgets($handle, 256);
fgets($handle, 256);
fgets($handle, 256);
fgets($handle, 256);

 

Because if I don't do the above, then when I try the code below it will not work as it is still spitting rubbish out.

 

// SMTP authorization  
  fputs($handle, "AUTH LOGIN\r\n");fgets($handle, 256);
fputs($handle, base64_encode($username)."\r\n");
  
  fgets($handle, 256);
  
  fputs($handle, base64_encode($password)."\r\n"); 
  
    $output = fgets($handle, 256);
if(($output[0].$output[1].$output[2])!="235")
{
echo "Can not connect to SMTP server (Authentication error) (".$output.")";sendErrorEmail($output);fclose($handle);exit;
}

 

The problem is different SMTP servers require a different number of multiple "fgets($handle, 256);" commands to clear it.

 

I have tried:

if(substr($str,3,1) == " ") { break; }

..but it doesn't seem to work.

Link to comment
https://forums.phpfreaks.com/topic/149797-connecting-to-smtp-fputs/
Share on other sites

Actually I found this, and it works:

 

$result=fgets($handle, 256);

while($result = @fgets($handle, 1024)){if(substr($result,3,1) == " ") { break; }};

 

(which I am sure is probably the same as MadTechie's response)

 

BUT.. Only it only works on one of my email servers. The other one doesn't seem to pick up all the messages.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.