connecting to gmails mx record

Viewed 156

I'm trying to connect to a gmail mx record to verify that a user exists. The below works from my dev box but not my production server, this makes me think the SSL that is on the production server must have something to do with it.

for clarity: I do not need to send and email or want to use gmail as an smtp server. I simply want to confirm the existence of a username

$email = 'someuser@gmail.com'
$t = explode('@',$email);
$host = end($t);
$smtp = new SMTP();

$mx_records = [];
if (!getmxrr($host,$mx_records)){
    throw new Exception('No Mx records for domain ' . $host);
};

//Connect to an SMTP server
$can_connect = false;
foreach ($mx_records as $msrs){
    if ($smtp->connect($msrs, 25)) {
        $can_connect = true;
        break;
    }    
}
if (!$can_connect) {
    throw new Exception('Connect failed');
}

SMTP here is from the phpMailer SMTP class. I've been throwing everything at the wall trying to get the production server to work. Here is the error output I've gotten from debugging:

2020-12-19 19:01:11 Connection: opening to alt4.gmail-smtp-in.l.google.com:25, timeout=15, options=array (),)
2020-12-19 19:01:26 Connection failed. Error #2: stream_socket_client(): unable to connect to alt4.gmail-smtp-in.l.google.com:25 (Connection timed out) [/opt/bitnami/apps/wordpress/htdocs/wp-includes/PHPMailer/SMTP.php line 375]
2020-12-19 19:01:26 SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP error: Connect failed 
SSL loaded

The ssl loaded check comes from: <?php echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."\n"; ?>

I've tried added these options to the php connect call as well from other troubleshooting I've seen, but not luck. I didn't have high hopes because the people who used this method were, I believe, trying to connect to the gmail SMTP server as their service provider.

$SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
$smtp->connect($mx_records[0], 25, 15, $SMTPOptions);

I've also tried connection on ports: 25, 465, 587

I'm thinking there has to be 1 simple thing I'm missing, but so far nothing is working.

edit_1

Do to the comment of firewall checking I have gone spelunking. My instance is an aws lightsail instance, which as far as I know means, there is no firewall installed on the instance, but an aws virtual firewall is installed on the instance. Checking for a firewall in the unbuntu instance of the server yields:

bitnami@******:~$ sudo ufw
sudo: ufw: command not found

I did however open up the port for incoming traffic on port 25, just to muck around, however no luck.

edit_2

I just set up telnet and tried to connect to one of the mx records using that:

telnet> open  alt4.gmail-smtp-in.l.google.com 25
Trying 142.250.96.26...
Trying 2607:f8b0:4023:403::1a...
telnet: Unable to connect to remote host: Network is unreachable
0 Answers
Related