Send mail using Gmail SMTP in Magento

我能想到的也是大部分人能想到的——我想用gmail smtp server代替magento内置的sendmail,这么做的好处是用gmail sent mail log all emails sent out by magento。gmail smtp server是我所知的唯一一个能记录发送邮件的smtp服务器(感谢google)。

Magento默认使用MTA发送邮件,后台configuration可以设置使用smtp发送邮件,但仅限于未使用ssl的smtp服务器,看来只能修改程序才能让Magento用上gmail ssl smtp server了。

似乎有很多人有这个想法但无法成功完成程序的修改。我参照了Use any smtp to send email (even gmail)的代码,但发现它无法连接gmail smtp server (可能以前可以,但gmail smtp server改了设置?)。

经我修改,在magento 1.1.8版测试可行的代码方案是:

修改/app/code/core/Mage/Core/Model/Email/Template.php, 找到public function send($email, $name=null, array $variables = array()),删掉此函数中最后几句:

try {
$mail->send(); // Zend_Mail warning..
$this->_mail = null;
}
catch (Exception $e) {
return false;
}
return true;

替换为:

$config = array(
'ssl' => 'ssl',
'port' => 465,
'auth' => 'login',
'username' => 'your_email',
'password' => 'your_password');

$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);

try {
$mail->send($transport); //add $transport object as parameter
$this->_mail = null;
}
catch (Exception $e) {
return false;
}
return true;

除此之外无需任何其他修改。赶紧试一下吧。

原贴主要的误导之处是一个错误的$config:

$config = array(
'ssl' => 'tls',
'port' => 465,
'auth' => 'login',
'username' => 'your_email',
'password' => 'your_password');

其实,如果非要使用tls的话,相应的port应该是587。tls+587的搭配虽然能发邮件,但编码不对,我想应该在程序其他地方相应调整。这个我就没去研究了,ssl+465已能正常发送邮件,我的想法已经实现了。

15 comments

  1. 你好!今天设置magento的邮件功能时在google看到您的文章对我帮助很大
    我感觉magento有时比较奇怪,在家的一台机子上能安装1.2.03版,但是在办公室却只能装1.1.8版,而且在办公室的这台发送邮件总提示Unable to submit your request. Please, try again later
    虽然这样还是看好magento,希望能找到志同道合的人

  2. 你好,我用了你的方法,但发不出邮件啊!

    系统是win2k3+apache2 + magento1.2.1

    不知道是不是 要什么特殊设置?

  3. 不好意思,老大的这个在1.2.1下面是能用的,但是需要开启主机的openssl模块才行,
    汗,花了好几个小时找这个原因,终于是找到问题所在了

  4. 博主,你好。我现在用magento1.5.1 去装这个插件,出现了错误。你尝试过在1.5.1装这个插件吗?

  5. 芳草老大,我1.7的用了connect的ASchroder发布的google app/Gmail插件,Test email 的确发成功了,但是除此之外,发不了如何信件。能否帮我分析一下?谢谢!
    ASchroder.com SMTP Pro Self-test results
    Testing outbound connectivity to Server:
    Using Google Apps/Gmail configuration options
    Failed to connect to SMTP server. Reason: ()
    This extension requires an outbound SMTP connection on port: 587
    Sending test email to your contact form address info@e***silk.com:
    Test email was sent successfully.
    Contact Form test email did not use SMTPPro to send.
    Testing failed, please review the reported problems and if you need further help visit ASchroder.com to contact me.

  6. 如果你听我一句话,对完成用 Gmail SMTP 来发邮件这么个简单任务,就别装什么插件了,别人写的代码不知道质量如何,还是自己修改一下代码方便、牢靠。

  7. 博主,你好,首先感谢你的博文,我的magento版本是1.6 按照你说的把
    try {
    $mail->send();
    $this->_mail = null;
    }
    catch (Exception $e) {
    $this->_mail = null;
    Mage::logException($e);
    return false;
    }

    return true;

    这块改成

    $config = array(
    ‘ssl’=>’ssl’,
    ‘port’=>465,
    ‘auth’=>’login’,
    ‘username’=>’pengzhenlu89@gmail.com’,
    ‘password’=>’***********’
    );

    $transport = new Zend_Mail_Transport_smtp(‘smtp.gmail.com’,$config);

    try {
    $mail->send($transport);
    $this->_mail = null;
    }
    catch (Exception $e) {
    $this->_mail = null;
    Mage::logException($e);
    return false;
    }

    return true;

    还是发送不了邮件,我在本地找回密码,没有收到发送的邮件,望博主指教,

  8. 可乐,你太可爱了,你怎么能把密码粘给我呢?幸好我 Approve 之前扫了一眼,mask 了。至于你的问题,解答不了,您慢慢研究,对不起

  9. 可乐 ,我的也是1.6版本,也发送不了邮件。
    请问你找到解决方法了吗?
    如找到解决方法,方便告知吗?
    谢谢

Leave a comment

Your email address will not be published. Required fields are marked *