我正在通过Gmail SMTP服务器从
PHP发送电子邮件.我一直在使用带有SMTP设置的CakePHP电子邮件组件.我最初使用SSL通过端口465工作正常,但发现我的网站主机不允许超过465的传出流量.但他们确实告诉我允许通过端口587的传出连接.
@H_502_16@
不确定你是否还在寻找它,但是要开始tls,你必须从服务器的命令中做到这一点.这是一个简单的设置,适用于使用gmail(如果你想要更多的帮助,只需通过tls连接,启动另一个问题):
阅读http://mail.google.com/support/bin/answer.py?answer=13287之后,我认为这就像更改端口号和协议一样简单,但我无法让它工作.
fsockopen("ssl://smtp.gmail.com",465,$errNum,$errStr,30); // WORKS fsockopen("tls://smtp.gmail.com",587,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 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",$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);