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已能正常发送邮件,我的想法已经实现了。

Tags: ,

7 Responses to “Send mail using Gmail SMTP in Magento”

  1. johne c-cn says:

    I still could not send my mails out.
    Can you help me?

  2. admin c-gb says:

    It sounds to me your network is likely blocking the smtp port

  3. gantao c-cn says:

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

  4. admin c-unknown says:

    先对比一下phpinfo吧,我给不出什么建设性的建议。

  5. leo c-fr says:

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

    系统是win2k3+apache2 + magento1.2.1

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

  6. leo c-fr says:

    这个在1.2.1下面好像不能用,1.1.8下能发送邮件,

  7. leo c-fr says:

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

Leave a Reply