johnsmith153 Posted March 17, 2009 Share Posted March 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149797-connecting-to-smtp-fputs/ Share on other sites More sharing options...
MadTechie Posted March 17, 2009 Share Posted March 17, 2009 try something like this while($str = fgets($handle,515)) { if(substr($str,3,1) == " ") { break; } } Quote Link to comment https://forums.phpfreaks.com/topic/149797-connecting-to-smtp-fputs/#findComment-786616 Share on other sites More sharing options...
johnsmith153 Posted March 17, 2009 Author Share Posted March 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149797-connecting-to-smtp-fputs/#findComment-786655 Share on other sites More sharing options...
MadTechie Posted March 17, 2009 Share Posted March 17, 2009 do you try the code i posted ? Quote Link to comment https://forums.phpfreaks.com/topic/149797-connecting-to-smtp-fputs/#findComment-786665 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.