Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 连接到Gmail mx记录_Php_Email_Gmail - Fatal编程技术网

Php 连接到Gmail mx记录

Php 连接到Gmail mx记录,php,email,gmail,Php,Email,Gmail,我正在尝试连接到gmail mx记录以验证用户是否存在。下面的内容可以从我的dev box中使用,但不能从我的生产服务器中使用,这使我认为生产服务器上的SSL肯定与此有关 为了清晰起见:我不需要发送电子邮件或使用gmail作为smtp服务器。我只想确认用户名的存在 $email = 'someuser@gmail.com' $t = explode('@',$email); $host = end($t); $smtp = new SMTP(); $mx_records = []; if (!

我正在尝试连接到gmail mx记录以验证用户是否存在。下面的内容可以从我的dev box中使用,但不能从我的生产服务器中使用,这使我认为生产服务器上的SSL肯定与此有关

为了清晰起见:我不需要发送电子邮件或使用gmail作为smtp服务器。我只想确认用户名的存在

$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来自phpMailer SMTP类。为了让生产服务器正常工作,我一直在竭尽全力。以下是我从调试中得到的错误输出:

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
ssl加载的检查来自:

我已经尝试将这些选项添加到php connect调用中,以及从我看到的其他故障排除中添加这些选项,但运气不好。我并没有抱太大的希望,因为我相信,使用这种方法的人正试图连接到gmail SMTP服务器作为他们的服务提供商

$SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
$smtp->connect($mx_records[0], 25, 15, $SMTPOptions);
我还尝试了端口连接:
25
465
587

我想一定有一件简单的事情我遗漏了,但到目前为止,一切都没起作用

编辑_1

对防火墙检查的评论我去探险了。我的实例是一个aws lightsail实例,据我所知,这意味着在实例上没有安装防火墙,但在实例上安装了aws虚拟防火墙。在服务器的unbuntu实例中检查防火墙会产生:

bitnami@******:~$ sudo ufw
sudo: ufw: command not found
不过,我确实在25号港口为入境车辆开放了港口,只是为了混日子,不过运气不好

编辑_2

我刚刚设置了telnet,并尝试使用以下方式连接到其中一个mx记录:

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

确保您的生产服务器允许端口25出站连接您是否可以从生产服务器通过telnet加入此服务器?@KenLee--我刚刚根据您的评论进行了更新。绝对是个好主意。“不过还没有成功。@Tuckbros-只是尝试了telnet,但没有成功。”。你知道下一步该怎么做吗?我在这里被难住了。我不知道接下来会发生什么,但你的问题似乎更多地与网络有关,而不是与php或电子邮件有关。服务器应答ping。所以你可以尝试ping它。但对于生产服务器来说,由于防火墙的存在,它可能无法连接到internet,或者由于安全原因(无论是传入还是传出),只能连接到很少几个精心选择的端口上。。。您应该与管理生产服务器所在网络的团队进行核实。