Search code examples
socketsemailsslsmtpgmail

Connecting to Gmail SMTP via socket returns different responses per server


I am trying to understand why opening a socket to Gmail's server via SSL provides different responses for different servers. This is stopping me from connecting to Gmail for sending out emails.

Below is a very simple script:

<?php

$smtp_connect = fsockopen('ssl://smtp.googlemail.com', 465,
    $errno,
    $errstr,
    300);

var_dump(fgets($smtp_connect, 512));

?>

On server 1 [no ssl cert installed], the output of the above code is: string(57) "220 smtp.googlemail.com ESMTP s89sm726209qkl.44 - gsmtp "

On server 2 [ssl cert installed], the output of the above code is: string(71) "220-mycompany.pro.com ESMTP Exim 4.87 #1 Wed, 26 Oct 2016 07:42:49 -0400 "

Can anyone explain why this happens, and how can I make server 2 have the same behavior as server 1?

Update: I have also tried smtp.gmail.com, the output is same as using smtp.googlemail.com.


Solution

  • The line you get the the SMTP welcome message. It is perfectly normal that different servers give a different welcome message. It is even normal that the response to commands you send is different. This means that when communicating with an SMTP server you should not assume fixed strings as response but implement proper parsing of SMTP. See the SMTP standard (RFC 2821) for details about this protocol.