问题描述
我通过Gmail SMTP服务器从PHP发送电子邮件。我一直在使用设置了SMTP设置的CakePHP电子邮件组件。我原来有一切工作正常使用SSL通过端口465,但发现我的web主机不允许传出流量超过465.他们告诉我,允许通过端口587的传出连接。
阅读我认为它会像更改端口号和协议一样简单,但我不能让它工作。 / p>
冒犯行代码似乎是fsockopen调用:
fsockopen(ssl://smtp.gmail.com,465,$ errNum,$ errStr,30); // WORKS
fsockopen(tls://smtp.gmail.com,587,$ errNum,$ errStr,30); // FAILS
出现的错误是:
警告:fsockopen():SSL操作失败与代码1. OpenSSL错误消息:
错误:1408F10B:SSL例程:SSL3_GET_RECORD:错误的版本号在命令行代码1
警告:fsockopen():无法在第1行的命令行代码中启用crypto
警告:fsockopen():无法连接到tls:// smtp。 gmail.com:587(未知错误)在第1行的命令行代码
,phpinfo显示OpenSSL已启用。任何帮助将非常感激。
不知道如果你还在寻找它,但要开始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\\\
,$ length);
}
if(!($ smtp = fsockopen(smtp.gmail.com,587,$ errno,$ errstr,15))){
die(Unable to connect );
}
echo< pre> \\\
;
echo get($ smtp); // should return a 220 if you want to check
$ cmd =EHLO $ {_ SERVER ['HTTP_HOST']};
echo $ cmd。\r\\\
;
put($ smtp,$ cmd);
echo get($ smtp); // 250
$ cmd =STARTTLS;
echo $ cmd。\r\\\
;
put($ smtp,$ cmd);
echo get($ smtp); // 220
if(false == stream_socket_enable_crypto($ smtp,true,STREAM_CRYPTO_METHOD_TLS_CLIENT)){
// fclose($ smtp); //不确定是否需要关闭,因为我还没有遇到安全失败在这一点
die(无法启动tls加密);
}
$ cmd =EHLO。$ _ SERVER ['HTTP_HOST'];
echo $ cmd;
put($ smtp,$ cmd);
echo get($ smtp); // 250
$ cmd =QUIT;
echo $ cmd。\r\\\
;
put($ smtp,$ cmd);
echo get($ smtp);
echo< / pre>;
fclose($ smtp);
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.
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);
这篇关于使用Gmails使用TLS从PHP发送SMTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!