使用 Gmail 使用 TLS 从 PHP 传出 SMTP

发布于 2024-10-22 10:08:59 字数 1003 浏览 4 评论 0原文

我正在通过 Gmail SMTP 服务器从 PHP 发送电子邮件。我一直在使用 CakePHP 电子邮件组件并设置了 SMTP 设置。我最初在端口 465 上使用 SSL 一切正常,但发现我的网络主机不允许通过 465 的传出流量。不过,他们确实告诉我允许通过端口 587 的传出连接。

阅读 http://mail.google.com/support/bin/answer 后.py?answer=13287 我认为这就像更改端口号和协议一样简单,但我无法让它工作。

有问题的代码行似乎是 fsockopen 调用:

fsockopen("ssl://smtp.gmail.com", 465, $errNum, $errStr, 30); // WORKS
fsockopen("tls://smtp.gmail.com", 587, $errNum, $errStr, 30); // FAILS

给出的错误是:

Warning: fsockopen(): SSL operation failed with code 1. OpenSSL Error messages:
error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number in Command line code on line 1

Warning: fsockopen(): Failed to enable crypto in Command line code on line 1

Warning: fsockopen(): unable to connect to tls://smtp.gmail.com:587 (Unknown error) in Command line code on line 1

这是 PHP 5.3,phpinfo 显示 OpenSSL 已启用。任何帮助将不胜感激。

I'm sending email from PHP through the Gmail SMTP server. I've been using the CakePHP email component with SMTP settings set. I originally had it all working fine using SSL over port 465 but have found my web host doesn't allow outgoing traffic over 465. They did however tell me that outgoing connections over port 587 is allowed.

After reading http://mail.google.com/support/bin/answer.py?answer=13287 I thought it would be as easy as changing the port number and protocol but I can't get it to work.

The offending line of code seems to be a fsockopen call:

fsockopen("ssl://smtp.gmail.com", 465, $errNum, $errStr, 30); // WORKS
fsockopen("tls://smtp.gmail.com", 587, $errNum, $errStr, 30); // FAILS

The errors given are:

Warning: fsockopen(): SSL operation failed with code 1. OpenSSL Error messages:
error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number in Command line code on line 1

Warning: fsockopen(): Failed to enable crypto in Command line code on line 1

Warning: fsockopen(): unable to connect to tls://smtp.gmail.com:587 (Unknown error) in Command line code on line 1

This is with PHP 5.3, phpinfo shows OpenSSL is enabled. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生一个溪 2024-10-29 10:08:59

不确定您是否仍在寻找它,但要启动 tls,您必须通过服务器的命令来执行此操作。这是一个适用于 tls 和 gmail 的简单设置(如果您需要更多帮助,而不仅仅是通过 tls 连接,请开始另一个问题):

<?php
function get($socket,$length=1024){
    $send = '';
    $sr = fgets($socket,$length);
    while( $sr ){
        $send .= $sr;
        if( $sr[3] != '-' ){ break; }
        $sr = fgets($socket,$length);
    }
    return $send;
}
function put($socket,$cmd,$length=1024){
    fputs($socket,$cmd."\r\n",$length);
}
if (!($smtp = fsockopen("smtp.gmail.com", 587, $errno, $errstr, 15))) {
    die("Unable to connect");
}
echo "<pre>\n";
echo get($smtp); // should return a 220 if you want to check

$cmd = "EHLO ${_SERVER['HTTP_HOST']}";
echo $cmd."\r\n";
put($smtp,$cmd);
echo get($smtp); // 250

$cmd = "STARTTLS";
echo $cmd."\r\n";
put($smtp,$cmd);
echo get($smtp); // 220
if(false == stream_socket_enable_crypto($smtp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)){
    // fclose($smtp); // unsure if you need to close as I haven't run into a security fail at this point
    die("unable to start tls encryption");
}

$cmd = "EHLO ".$_SERVER['HTTP_HOST'];
echo $cmd;
put($smtp,$cmd);
echo get($smtp); // 250

$cmd = "QUIT";
echo $cmd."\r\n";
put($smtp,$cmd);
echo get($smtp);

echo "</pre>";

fclose($smtp);

Not sure if you are still looking for it, but to start tls, you have to do it from the commands with the server. Here is a simple set up that works for tls with gmail (if you want more help beyond just connecting via tls, start another question):

<?php
function get($socket,$length=1024){
    $send = '';
    $sr = fgets($socket,$length);
    while( $sr ){
        $send .= $sr;
        if( $sr[3] != '-' ){ break; }
        $sr = fgets($socket,$length);
    }
    return $send;
}
function put($socket,$cmd,$length=1024){
    fputs($socket,$cmd."\r\n",$length);
}
if (!($smtp = fsockopen("smtp.gmail.com", 587, $errno, $errstr, 15))) {
    die("Unable to connect");
}
echo "<pre>\n";
echo get($smtp); // should return a 220 if you want to check

$cmd = "EHLO ${_SERVER['HTTP_HOST']}";
echo $cmd."\r\n";
put($smtp,$cmd);
echo get($smtp); // 250

$cmd = "STARTTLS";
echo $cmd."\r\n";
put($smtp,$cmd);
echo get($smtp); // 220
if(false == stream_socket_enable_crypto($smtp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)){
    // fclose($smtp); // unsure if you need to close as I haven't run into a security fail at this point
    die("unable to start tls encryption");
}

$cmd = "EHLO ".$_SERVER['HTTP_HOST'];
echo $cmd;
put($smtp,$cmd);
echo get($smtp); // 250

$cmd = "QUIT";
echo $cmd."\r\n";
put($smtp,$cmd);
echo get($smtp);

echo "</pre>";

fclose($smtp);
大海や 2024-10-29 10:08:59

为什么不使用梨呢?

http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm

更改第二个示例中的端口。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文