PHP默认对SMTP验证不支持,可是Zend有这个功能而且根本用不到php.ini中的smtp配置。
对照手册写了个测试,代码如下:
require_once ‘Zend/Mail.php’;
require_once ‘Zend/Mail/Transport/Smtp.php’;
$mail = new Zend_Mail(“UTF-8″); //设置邮件编码
$config = array(‘auth’ => ‘login’,
‘username’ => “tysina.tian@php8848.com”, //电子件用户名
‘password’ => “********”,
‘ssl’ => “ssl”
);
$transport = new Zend_Mail_Transport_Smtp(‘smtp.ym.163.com’, $config);
$mail->setDefaultTransport($transport);
$mailcontent = ‘欢迎您的到来!
您的注册名为:’.$config ['username']
.’
您的密码为:’.$config ['password']
.’
请您点击这里的地址:
您的帐号!
请尽快删除此邮件,以免别人偷看到您的密码
如果忘了密码,可以到社区写信请管理员重新设定
’;
$mail->setBodyHtml($mailcontent);
$mail->setFrom(‘tysina.tian@php8848.com’, ‘tysina’);
$mail->addTo(‘tysina.tian@php8848.com’, ‘tysina’);
$title = $config ['username'] . ‘,用户您好,这是网站激活验证邮件!’;
$mail->setSubject(“=?UTF-8?B?” . base64_encode($title) . “?=”);
//$mail->send();
if($mail->send()){
echo “Sent!”;
}else{
echo “Not sent!”;
}
?>
运行,报错。
错误提示如下:
( ! ) Fatal error: Uncaught exception ‘Zend_Mail_Protocol_Exception’ with message ‘Could not open socket’ in C:Program FilesZendFrameworkClilibraryZendMailProtocolAbstract.php on line 277
( ! ) Zend_Mail_Protocol_Exception: Could not open socket in C:Program FilesZendFrameworkClilibraryZendMailProtocolAbstract.php on line 277
Call Stack
# Time Memory Function Location
1 0.0023 72976 {main}( ) ..19-0.php:0
2 0.1056 1137304 Zend_Mail->send( ??? ) ..19-0.php:23
3 0.1059 1138528 Zend_Mail_Transport_Abstract->send( object(Zend_Mail)[1] ) ..Mail.php:1194
4 0.1072 1168240 Zend_Mail_Transport_Smtp->_sendMail( ) ..Abstract.php:348
5 0.1312 1281048 Zend_Mail_Protocol_Smtp->connect( ) ..Smtp.php:199
6 0.1312 1281048 Zend_Mail_Protocol_Abstract->_connect( string(25) ) ..Smtp.php:167
查看ZendMailProtocolAbstract.php代码,发现265~278行代码如下:
$errorNum = 0;
$errorStr = ”;
// open connection
$this->_socket = @stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
if ($this->_socket === false) {
if ($errorNum == 0) {
$errorStr = ‘Could not open socket’;
}
/**
* @see Zend_Mail_Protocol_Exception
*/
require_once ‘Zend/Mail/Protocol/Exception.php’;
throw new Zend_Mail_Protocol_Exception($errorStr);
}
即@stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION)返回false。
原因,安装appserv时默认的openssl是关闭的,自然不会创建成功。
解决方法很简单,在php.ini文件中找到
;extension=php_openssl.dll
把前面的分号去掉,保存然后重启apache,然后就通过了。