Question

Here is the code:

$fp = stream_socket_client("smtp.mail.yahoo.com:587", $errno, $errstr, 10);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "EHLO my.hostname\r\n");
    echo fgets($fp, 1024).'<br>';
    echo fgets($fp, 1024).'<br>';
    echo fgets($fp, 1024).'<br>';
    echo fgets($fp, 1024).'<br>';
    echo fgets($fp, 1024).'<br>';
    echo fgets($fp, 1024).'<br>';

    fwrite($fp, "STARTTLS\r\n");
    fwrite($fp, "EHLO my.hostname\r\n");

    echo fgets($fp, 1024).'<br>';
    echo fgets($fp, 101024).'<br>';
}

the problem is that when i run this in browser i just get these:

220 smtp.mail.yahoo.com ESMTP ready   
250-smtp.mail.yahoo.com   
250-PIPELINING   
250-SIZE 41697280   
250-8 BITMIME   
250 STARTTLS   
220 2.0.0 Start TLS   

So why the lines after "220 2.0.0 Start TLS" does not show? is there any problem with the code or what?

just one more thing, instead of fwrite($fp, "EHLO my.hostname\r\n"); in the last line, i used other commands like QUIT but the same result!!

Was it helpful?

Solution

After you send STARTTLS, the communication goes into encrypted mode, which is why no further unencrypted commands work. You probably don't want to be getting stuck into encrypting your message - if you just want to send a mail you can use PHPMailer

https://github.com/Synchro/PHPMailer

OTHER TIPS

If you want to rebuild existing functionality yourself please read the documentation or a library which already implements this and don't just guess and ask if it not works. SMTP is described in RFC 5321 and the behavior with STARTTLS in RFC3207.

Just a few hints: you need to do a TLS upgrade after you got the response to STARTTLS and before sending any other command. Also, there is no guarantee that the response to EHLO is exactly 5 lines.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top