您好,欢迎访问一九零五行业门户网

调试php没法使用smtp发送邮件的问题

把开发平台转移到ubuntu后,本地开发时遇到了一个问题,magento默认的发邮件方式是用匿名smtp,在window下架匿名smtp服务器很简单,可以使用mail_direct_pro这个软件,简单的几步就能架个匿名smtp,但ubuntu下就不一样了,当时考虑了几个方案,最后决定使用修
把开发平台转移到ubuntu后,本地开发时遇到了一个问题,magento默认的发邮件方式是用匿名smtp,在window下架匿名smtp服务器很简单,可以使用mail_direct_pro这个软件,简单的几步就能架个匿名smtp,但ubuntu下就不一样了,当时考虑了几个方案,最后决定使用修改magento源码来使用某些邮件服务商提供的smtp服务,于是在网络上找了方法 http://blog.csdn.net/newjueqi/article/details/7085207  ,但无论是google还是126的smtp服务都不成功。为了测试网上的代码有没有错误,于是写下了以下的测试代码:
$config = array ( 'ssl' => 'ssl', 'port' => 465, 'auth' => 'login', 'username' => 'myemailaccount@126.com', 'password' => 'myemailpassword' );$transport = new zend_mail_transport_smtp ( 'smtp.126.com', $config );$mail = new zend_mail ();$mail->setbodytext ( content );$mail->setfrom ( myemailaccount@126.com, 'webmaster' );$mail->addto ( h6k65@126.com, '' );$mail->setsubject ( 'import attribute logs' );$mail->send ( $transport );
测试成功,可以发送邮件。
很奇怪,为啥magento下就没法发送邮件。查了一下系统的日志,发现了以下的错误:2011-12-22t06:19:58+00:00 err (3): exception 'zend_mail_protocol_exception' with message 'mail from must equal authorized user' in /var/www/buymedesign/trunk/lib/zend/mail/protocol/abstract.php:408哦,原来是smpt使用的email地址和from的email地址不一样
查找 app/code/core/mage/core/model/email/template.php中的 send function, 找到如下的代码:
$mail->setsubject('=?utf-8?b?'.base64_encode($this->getprocessedtemplatesubject($variables)).'?='); $mail->setfrom($this->getsenderemail(), $this->getsendername()); try { $mail->send(); $this->_mail = null; } catch (exception $e) { $this->_mail = null; mage::logexception($e); return false; }
替换为
$mail->setsubject('=?utf-8?b?'.base64_encode($this->getprocessedtemplatesubject($variables)).'?='); $mail->setfrom($this->getsenderemail(), $this->getsendername()); // try { $mail->setfrom(myemailaccount@126.com, 'webmaster');    //设置新的from信息     $config = array( 'ssl' => 'ssl', 'port' => 465, 'auth' => 'login', 'username' => 'myemailaccount@126.com', 'password' => 'myemailpassword'); $transport = new zend_mail_transport_smtp('smtp.126.com', $config); $mail->send($transport); //add $transport object as parameter $this->_mail = null; } catch (exception $e) { $this->_mail = null; mage::logexception($e); return false; } return true; }
修改后发现还是原来的错误提示,会不会是setfrom的函数的问题,于是看了一下setfrom函数
lib/zend/mail.php public function setfrom($email, $name = null) { if (null !== $this->_from) { /** * @see zend_mail_exception */ #require_once 'zend/mail/exception.php'; throw new zend_mail_exception('from header set twice'); } $email = $this->_filteremail($email); $name = $this->_filtername($name); $this->_from = $email; $this->_storeheader('from', $this->_formataddress($email, $name), true); return $this; } protected function _storeheader($headername, $value, $append = false) { if (isset($this->_headers[$headername])) { $this->_headers[$headername][] = $value; } else { $this->_headers[$headername] = array($value); } if ($append) { $this->_headers[$headername]['append'] = true; } }
从 if (isset($this->_headers[$headername])) { $this->_headers[$headername][] = $value; } else { $this->_headers[$headername] = array($value); }
可看出,当key值有重复的时候,是才用添加子数组而不是覆盖的方法,所以新设置的setfrom信息没效,把代码改成如下即发送成功:
$mail->setsubject('=?utf-8?b?'.base64_encode($this->getprocessedtemplatesubject($variables)).'?='); //$mail->setfrom($this->getsenderemail(), $this->getsendername()); // try { $mail->setfrom(myemailaccount@126.com, 'webmaster');    //设置新的from信息     $config = array( 'ssl' => 'ssl', 'port' => 465, 'auth' => 'login', 'username' => 'myemailaccount@126.com', 'password' => 'myemailpassword'); $transport = new zend_mail_transport_smtp('smtp.126.com', $config); $mail->send($transport); //add $transport object as parameter $this->_mail = null; } catch (exception $e) { $this->_mail = null; mage::logexception($e); return false; } return true; }
【文章标题】调试php没法使用smtp发送邮件的问题
【文章作者】曾健生
【作者邮箱】zengjiansheng1@126.com
【作者qq】190678908
【作者博客】blog.csdn.net/newjueqi

其它类似信息

推荐信息