Home > php教程 > php手册 > 调试php没法使用smtp发送邮件的问题

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

WBOY
Release: 2016-06-06 19:54:12
Original
1122 people have browsed it

把开发平台转移到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 );
Copy after login

测试成功,可以发送邮件。

很奇怪,为啥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;
        }
Copy after login

替换为


        $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;
    }
Copy after login

修改后发现还是原来的错误提示,会不会是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;
        }

    }
Copy after login

        if (isset($this->_headers[$headerName])) {
            $this->_headers[$headerName][] = $value;
        } else {
            $this->_headers[$headerName] = array($value);
        }
Copy after login

可看出,当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;
    }
Copy after login

【文章标题】调试php没法使用smtp发送邮件的问题
【文章作者】曾健生
【作者邮箱】zengjiansheng1@126.com
【作者QQ】190678908
【作者博客】blog.csdn.net/newjueqi







Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template