Jump to content

Recommended Posts

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); ?

 

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 by kicken
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.