RieqyNS13 Posted May 19, 2013 Share Posted May 19, 2013 I send EHLO command to gmail stmp. if response was success, it will send code 250. I try to catch the response code of gmail smtp. when my code like this $fp = fsockopen("ssl://smtp.gmail.com", 465, $errNo, $errStr, 15); if(empty($fp)){ echo $errNo.":".$errStr; }else{ fputs($fp, "EHLO"."\r\n"); $rp = get_lines($fp); $code = substr($rp,0,3); echo $code; fclose($fp); } function get_lines($fp){ while($str = fgets($fp)){ return $str; } } why it always produce code 220 ? but when I append the php code with echo fgets($fp); like this $fp = fsockopen("ssl://smtp.gmail.com", 465, $errNo, $errStr, 15); if(empty($fp)){ echo $errNo.":".$errStr; }else{ echo fgets($fp); //additional code fputs($fp, "EHLO"."\r\n"); $rp = get_lines($fp); $code = substr($rp,0,3); echo $code; fclose($fp); } function get_lines($fp){ while($str = fgets($fp)){ return $str; } } the output like this: 220 mx.google.com ESMTP vu10sm20075869pbc.27 - gsmtp <<don't need this 250 code 250 has caught. but how to produce code 250 without additional command like echo fgets($fp); ? Quote Link to comment Share on other sites More sharing options...
kicken Posted May 19, 2013 Share Posted May 19, 2013 (edited) Your code only attempts to read a single line from the socket, but a command could generate several response lines. EHLO typically will generate a few as it lists out various extensions that are available. If you want to properly manage an SMTP session and read/validate the responses to your commands then you need to spend a lot more time on your socket handling. A simple fgets/fputs will not suffice. Use the streams api to handle your socket connection. You'll want to toggle on non-blocking mode and then use a loop around steam_select for reading the socket data. It is possible you'll only read a partial line on any given socket read so you need to append data to a buffer, not process it directly For processing, extract a full line from that buffer and parse it's contents. Determine what actions need to be taken for the given response and do them. If you don't want to go through all that hassle, use an existing project like PHPMailer or SwiftMail. If you do want to do it, for learning or whatever, make an attempt w/ the above and post back if you need more help. Edited May 19, 2013 by kicken Quote Link to comment 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.